Create scope by_proposal

Add to active_for class method and to active_resources controller method the new scope by_proposal.
 - Published proposal: display all actions.
 - Draft proposal: only display actions for draft proposals.
This commit is contained in:
taitus
2019-01-08 16:04:49 +01:00
parent 082cc41a1e
commit 9fac661f0c
4 changed files with 137 additions and 31 deletions

View File

@@ -23,7 +23,10 @@ class DashboardController < Dashboard::BaseController
private private
def active_resources def active_resources
@active_resources ||= Dashboard::Action.active.resources.order(required_supports: :asc, day_offset: :asc) @active_resources ||= Dashboard::Action.active
.resources
.by_proposal(proposal)
.order(required_supports: :asc, day_offset: :asc)
end end
def course def course

View File

@@ -39,13 +39,16 @@ class Dashboard::Action < ActiveRecord::Base
scope :inactive, -> { where(active: false) } scope :inactive, -> { where(active: false) }
scope :resources, -> { where(action_type: 1) } scope :resources, -> { where(action_type: 1) }
scope :proposed_actions, -> { where(action_type: 0) } scope :proposed_actions, -> { where(action_type: 0) }
scope :by_proposal, lambda { |proposal|
return where(published_proposal: false) if proposal.draft?
}
def self.active_for(proposal) def self.active_for(proposal)
published_at = proposal.published_at&.to_date || Date.today published_at = proposal.published_at&.to_date || Date.today
active active.where('required_supports <= ?', proposal.cached_votes_up)
.where('required_supports <= ?', proposal.cached_votes_up)
.where('day_offset <= ?', (Date.today - published_at).to_i) .where('day_offset <= ?', (Date.today - published_at).to_i)
.by_proposal(proposal)
end end
def self.course_for(proposal) def self.course_for(proposal)

View File

@@ -56,47 +56,120 @@ feature "Proposal's dashboard" do
expect(page).not_to have_selector(:css, "#dashboard_action_#{action.id}_execute") expect(page).not_to have_selector(:css, "#dashboard_action_#{action.id}_execute")
end end
scenario 'Dashboard progress show available resources' do scenario "Dashboard progress dont show proposed actions with published_proposal: true" do
action = create(:dashboard_action, :proposed_action, :active, published_proposal: true)
visit progress_proposal_dashboard_path(proposal)
expect(page).not_to have_content(action.title)
end
scenario "Dashboard progress show available resources for proposal draft" do
available = create(:dashboard_action, :resource, :active) available = create(:dashboard_action, :resource, :active)
requested = create(:dashboard_action, :resource, :admin_request, :active) requested = create(:dashboard_action, :resource, :admin_request, :active)
executed_action = create(:dashboard_executed_action, action: requested, proposal: proposal, executed_at: Time.current) executed_action = create(:dashboard_executed_action, action: requested,
proposal: proposal, executed_at: Time.current)
_task = create(:dashboard_administrator_task, :pending, source: executed_action) _task = create(:dashboard_administrator_task, :pending, source: executed_action)
solved = create(:dashboard_action, :resource, :admin_request, :active) solved = create(:dashboard_action, :resource, :admin_request, :active)
executed_solved_action = create(:dashboard_executed_action, action: solved, proposal: proposal, executed_at: Time.current) executed_solved_action = create(:dashboard_executed_action, action: solved,
proposal: proposal, executed_at: Time.current)
_solved_task = create(:dashboard_administrator_task, :done, source: executed_solved_action) _solved_task = create(:dashboard_administrator_task, :done, source: executed_solved_action)
unavailable = create(:dashboard_action, :resource, :active, required_supports: proposal.votes_for.size + 1_000) unavailable = create(:dashboard_action, :resource, :active,
required_supports: proposal.votes_for.size + 1_000)
visit progress_proposal_dashboard_path(proposal) visit progress_proposal_dashboard_path(proposal)
within 'div#available-resources-section' do within "div#available-resources-section" do
expect(page).to have_content('Polls') expect(page).to have_content("Polls")
expect(page).to have_content('E-mail') expect(page).to have_content("E-mail")
expect(page).to have_content('Poster') expect(page).to have_content("Poster")
expect(page).to have_content(available.title) expect(page).to have_content(available.title)
expect(page).to have_content(unavailable.title) expect(page).to have_content(unavailable.title)
expect(page).to have_content(requested.title) expect(page).to have_content(requested.title)
expect(page).to have_content(solved.title) expect(page).to have_content(solved.title)
within "div#dashboard_action_#{available.id}" do within "div#dashboard_action_#{available.id}" do
expect(page).to have_link('Request resource') expect(page).to have_link("Request resource")
end end
within "div#dashboard_action_#{requested.id}" do within "div#dashboard_action_#{requested.id}" do
expect(page).to have_content('Resource already requested') expect(page).to have_content("Resource already requested")
end end
within "div#dashboard_action_#{unavailable.id}" do within "div#dashboard_action_#{unavailable.id}" do
expect(page).to have_content('1.000 supports required') expect(page).to have_content("1.000 supports required")
end end
within "div#dashboard_action_#{solved.id}" do within "div#dashboard_action_#{solved.id}" do
expect(page).to have_link('See resource') expect(page).to have_link("See resource")
end end
end end
end end
scenario "Dashboard progress show available resources for published proposal" do
proposal.update(published_at: Date.today)
available = create(:dashboard_action, :resource, :active)
requested = create(:dashboard_action, :resource, :admin_request, :active)
executed_action = create(:dashboard_executed_action, action: requested,
proposal: proposal, executed_at: Time.current)
_task = create(:dashboard_administrator_task, :pending, source: executed_action)
solved = create(:dashboard_action, :resource, :admin_request, :active)
executed_solved_action = create(:dashboard_executed_action, action: solved,
proposal: proposal, executed_at: Time.current)
_solved_task = create(:dashboard_administrator_task, :done, source: executed_solved_action)
unavailable = create(:dashboard_action, :resource, :active,
required_supports: proposal.votes_for.size + 1_000)
visit progress_proposal_dashboard_path(proposal)
within "div#available-resources-section" do
expect(page).to have_content("Polls")
expect(page).to have_content("E-mail")
expect(page).to have_content("Poster")
expect(page).to have_content(available.title)
expect(page).to have_content(unavailable.title)
expect(page).to have_content(requested.title)
expect(page).to have_content(solved.title)
within "div#dashboard_action_#{available.id}" do
expect(page).to have_link("Request resource")
end
within "div#dashboard_action_#{requested.id}" do
expect(page).to have_content("Resource already requested")
end
within "div#dashboard_action_#{unavailable.id}" do
expect(page).to have_content("1.000 supports required")
end
within "div#dashboard_action_#{solved.id}" do
expect(page).to have_link("See resource")
end
end
end
scenario "Dashboard progress dont show resources with published_proposal: true" do
available = create(:dashboard_action, :resource, :active, published_proposal: true)
unavailable = create(:dashboard_action, :resource, :active,
required_supports: proposal.votes_for.size + 1_000,
published_proposal: true)
visit progress_proposal_dashboard_path(proposal)
within "div#available-resources-section" do
expect(page).to have_content("Polls")
expect(page).to have_content("E-mail")
expect(page).to have_content("Poster")
expect(page).not_to have_content(available.title)
expect(page).not_to have_content(unavailable.title)
end
end
scenario 'Dashboard has a link to polls feature' do scenario 'Dashboard has a link to polls feature' do
expect(page).to have_link('Polls') expect(page).to have_link('Polls')
end end

View File

@@ -155,23 +155,50 @@ describe Dashboard::Action do
let!(:not_enough_supports_action) { create :dashboard_action, :active, day_offset: 0, required_supports: 10_000 } let!(:not_enough_supports_action) { create :dashboard_action, :active, day_offset: 0, required_supports: 10_000 }
let!(:inactive_action) { create :dashboard_action, :inactive } let!(:inactive_action) { create :dashboard_action, :inactive }
let!(:future_action) { create :dashboard_action, :active, day_offset: 300, required_supports: 0 } let!(:future_action) { create :dashboard_action, :active, day_offset: 300, required_supports: 0 }
let!(:action_for_published_proposal) { create :dashboard_action,
:active,
day_offset: 0,
required_supports: 0,
published_proposal: true }
let!(:action_for_draft_proposal) { create :dashboard_action,
:active,
day_offset: 0,
required_supports: 0,
published_proposal: false }
let(:proposal) { create :proposal } let(:proposal) { create :proposal }
let(:draft_proposal) { create :proposal, :draft }
it 'actions with enough supports or days are active' do it "actions with enough supports or days are active" do
expect(described_class.active_for(proposal)).to include(active_action) expect(described_class.active_for(proposal)).to include(active_action)
end end
it 'inactive actions are not included' do it "inactive actions are not included" do
expect(described_class.active_for(proposal)).not_to include(inactive_action) expect(described_class.active_for(proposal)).not_to include(inactive_action)
end end
it 'actions without enough supports are not active' do it "actions without enough supports are not active" do
expect(described_class.active_for(proposal)).not_to include(not_enough_supports_action) expect(described_class.active_for(proposal)).not_to include(not_enough_supports_action)
end end
it 'actions planned to be active in the future are not active' do it "actions planned to be active in the future are not active" do
expect(described_class.active_for(proposal)).not_to include(future_action) expect(described_class.active_for(proposal)).not_to include(future_action)
end end
it "actions with published_proposal: true, are not included on draft proposal" do
expect(described_class.active_for(draft_proposal)).not_to include(action_for_published_proposal)
end
it "actions with published_proposal: true, are included on published proposal" do
expect(described_class.active_for(proposal)).to include(action_for_published_proposal)
end
it "actions with published_proposal: false, are included on draft proposal" do
expect(described_class.active_for(draft_proposal)).to include(action_for_draft_proposal)
end
it "actions with published_proposal: false, are included on published proposal" do
expect(described_class.active_for(proposal)).to include(action_for_draft_proposal)
end
end end
context '#course_for' do context '#course_for' do