Scope refactoring

complex scopes in Dashboard::Action have been refactored into static
methods. Specs for refactored scopes have been added as well.
This commit is contained in:
Juan Salvador Pérez García
2018-09-18 10:59:32 +02:00
parent e8e01b184f
commit ee83238e32
2 changed files with 55 additions and 6 deletions

View File

@@ -39,7 +39,8 @@ class Dashboard::Action < ActiveRecord::Base
scope :inactive, -> { where(active: false) }
scope :resources, -> { where(action_type: 1) }
scope :proposed_actions, -> { where(action_type: 0) }
scope :active_for, ->(proposal) do
def self.active_for(proposal)
published_at = proposal.published_at&.to_date || Date.today
active
@@ -47,12 +48,12 @@ class Dashboard::Action < ActiveRecord::Base
.where('day_offset <= ?', (Date.today - published_at).to_i)
end
scope :course_for, lambda { |proposal|
def self.course_for(proposal)
active
.resources
.where('required_supports > ?', proposal.cached_votes_up)
.order(required_supports: :asc)
}
end
def active_for?(proposal)
published_at = proposal.published_at&.to_date || Date.today

View File

@@ -68,7 +68,7 @@ describe Dashboard::Action do
expect(action).not_to be_valid
end
context 'active_for?' do
context '#active_for?' do
it 'is active when required supports is 0 and day_offset is 0' do
action = build(:dashboard_action, required_supports: 0, day_offset: 0)
proposal = build(:proposal)
@@ -105,7 +105,7 @@ describe Dashboard::Action do
end
end
context 'requested_for?' do
context '#requested_for?' do
it 'is not requested when no administrator task' do
proposal = create(:proposal)
action = create(:dashboard_action, :active, :admin_request, :resource)
@@ -123,7 +123,7 @@ describe Dashboard::Action do
end
end
context 'executed_for?' do
context '#executed_for?' do
it 'is not executed when no administrator task' do
proposal = create(:proposal)
action = create(:dashboard_action, :active, :admin_request, :resource)
@@ -149,4 +149,52 @@ describe Dashboard::Action do
expect(action).to be_executed_for(proposal)
end
end
context '#active_for' do
let!(:active_action) { create :dashboard_action, :active, day_offset: 0, required_supports: 0 }
let!(:not_enough_supports_action) { create :dashboard_action, :active, day_offset: 0, required_supports: 10_000 }
let!(:inactive_action) { create :dashboard_action, :inactive }
let!(:future_action) { create :dashboard_action, :active, day_offset: 300, required_supports: 0 }
let(:proposal) { create :proposal }
it 'actions with enough supports or days are active' do
expect(described_class.active_for(proposal)).to include(active_action)
end
it 'inactive actions are not included' do
expect(described_class.active_for(proposal)).not_to include(inactive_action)
end
it 'actions without enough supports are not active' do
expect(described_class.active_for(proposal)).not_to include(not_enough_supports_action)
end
it 'actions planned to be active in the future are not active' do
expect(described_class.active_for(proposal)).not_to include(future_action)
end
end
context '#course_for' do
let!(:proposed_action) { create :dashboard_action, :active, required_supports: 0 }
let!(:inactive_resource) { create :dashboard_action, :inactive, :resource, required_supports: 0 }
let!(:resource) { create :dashboard_action, :active, :resource, required_supports: 10_000 }
let!(:achieved_resource) { create :dashboard_action, :active, :resource, required_supports: 0 }
let(:proposal) { create :proposal }
it "proposed actions are not part of proposal's course" do
expect(described_class.course_for(proposal)).not_to include(proposed_action)
end
it "inactive resources are not part of proposal's course" do
expect(described_class.course_for(proposal)).not_to include(inactive_resource)
end
it "achievements are not part of the proposal's course" do
expect(described_class.course_for(proposal)).not_to include(achieved_resource)
end
it "active resources are part of proposal's course" do
expect(described_class.course_for(proposal)).to include(resource)
end
end
end