Merge pull request #2578 from consul/fix_valuation_heading_filters

Fix valuation heading filters
This commit is contained in:
Alberto Calderón Queimadelos
2018-04-06 12:02:27 +02:00
committed by GitHub
3 changed files with 81 additions and 35 deletions

View File

@@ -73,19 +73,25 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController
end end
def heading_filters def heading_filters
investments = @budget.investments.by_valuator(current_user.valuator.try(:id)) investments = @budget.investments.by_valuator(current_user.valuator.try(:id)).distinct
.valuation_open.select(:heading_id).all.to_a investment_headings = Budget::Heading.where(id: investments.pluck(:heading_id).uniq)
.order(name: :asc)
[ { name: t('valuation.budget_investments.index.headings_filter_all'), all_headings_filter = [
id: nil, {
pending_count: investments.size name: t('valuation.budget_investments.index.headings_filter_all'),
} id: nil,
] + Budget::Heading.where(id: investments.map(&:heading_id).uniq).order(name: :asc).collect do |h| count: investments.size
{ name: h.name, }
id: h.id, ]
pending_count: investments.count{|x| x.heading_id == h.id}
} filters = investment_headings.inject(all_headings_filter) do |filters, heading|
end filters << {
name: heading.name,
id: heading.id,
count: investments.select{|i| i.heading_id == heading.id}.size
}
end
end end
def params_for_current_valuator def params_for_current_valuator

View File

@@ -9,7 +9,7 @@
<% slice.each do |filter| %> <% slice.each do |filter| %>
<%= link_to valuation_budget_budget_investments_path(budget_id: @budget.id, heading_id: filter[:id]), <%= link_to valuation_budget_budget_investments_path(budget_id: @budget.id, heading_id: filter[:id]),
class: "#{'active' if params[:heading_id].to_s == filter[:id].to_s}" do %> class: "#{'active' if params[:heading_id].to_s == filter[:id].to_s}" do %>
<%= filter[:name] %>&nbsp;(<%= filter[:pending_count] %>) <%= filter[:name] %> (<%= filter[:count] %>)
<% end %> <% end %>
<% end %> <% end %>
</div> </div>

View File

@@ -68,37 +68,77 @@ feature 'Valuation budget investments' do
scenario "Index filtering by heading", :js do scenario "Index filtering by heading", :js do
group = create(:budget_group, budget: budget) group = create(:budget_group, budget: budget)
heading1 = create(:budget_heading, name: "District 9", group: group) valuating_heading = create(:budget_heading, name: "Only Valuating", group: group)
heading2 = create(:budget_heading, name: "Down to the river", group: group) valuating_finished_heading = create(:budget_heading, name: "Valuating&Finished", group: group)
investment1 = create(:budget_investment, title: "Realocate visitors", heading: heading1, finished_heading = create(:budget_heading, name: "Only Finished", group: group)
group: group, budget: budget) create(:budget_investment, title: "Valuating Investment ONE",
investment2 = create(:budget_investment, title: "Destroy the city", heading: heading2, heading: valuating_heading,
group: group, budget: budget) group: group,
investment1.valuators << valuator budget: budget,
investment2.valuators << valuator valuators: [valuator])
create(:budget_investment, title: "Valuating Investment TWO",
heading: valuating_finished_heading,
group: group,
budget: budget,
valuators: [valuator])
create(:budget_investment, :finished, title: "Finished ONE",
heading: valuating_finished_heading,
group: group,
budget: budget,
valuators: [valuator])
create(:budget_investment, :finished, title: "Finished TWO",
heading: finished_heading,
group: group,
budget: budget,
valuators: [valuator])
visit valuation_budget_budget_investments_path(budget) visit valuation_budget_budget_investments_path(budget)
expect(page).to have_link("Realocate visitors") expect(page).to have_link("Valuating Investment ONE")
expect(page).to have_link("Destroy the city") expect(page).to have_link("Valuating Investment TWO")
expect(page).not_to have_link("Finished ONE")
expect(page).not_to have_link("Finished TWO")
expect(page).to have_content "All headings (2)" expect(page).to have_link('All headings (4)')
expect(page).to have_content "District 9 (1)" expect(page).to have_link('Only Valuating (1)')
expect(page).to have_content "Down to the river (1)" expect(page).to have_link('Valuating&Finished (2)')
expect(page).to have_link('Only Finished (1)')
click_link "District 9", exact: false click_link "Only Valuating (1)", exact: false
expect(page).to have_link("Valuating Investment ONE")
expect(page).not_to have_link("Valuating Investment TWO")
expect(page).not_to have_link("Finished ONE")
expect(page).not_to have_link("Finished TWO")
expect(page).to have_link("Realocate visitors") click_link 'Valuation finished'
expect(page).not_to have_link("Destroy the city") expect(page).not_to have_link("Valuating Investment ONE")
expect(page).not_to have_link("Valuating Investment TWO")
expect(page).not_to have_link("Finished ONE")
expect(page).not_to have_link("Finished TWO")
click_link "Down to the river", exact: false click_link "Valuating&Finished (2)", exact: false
expect(page).not_to have_link("Valuating Investment ONE")
expect(page).to have_link("Valuating Investment TWO")
expect(page).not_to have_link("Finished ONE")
expect(page).not_to have_link("Finished TWO")
expect(page).to have_link("Destroy the city") click_link 'Valuation finished'
expect(page).not_to have_link("Realocate visitors") expect(page).not_to have_link("Valuating Investment ONE")
expect(page).not_to have_link("Valuating Investment TWO")
expect(page).to have_link("Finished ONE")
expect(page).not_to have_link("Finished TWO")
click_link "All headings", exact: false click_link "Only Finished (1)", exact: false
expect(page).to have_link("Realocate visitors") expect(page).not_to have_link("Valuating Investment ONE")
expect(page).to have_link("Destroy the city") expect(page).not_to have_link("Valuating Investment TWO")
expect(page).not_to have_link("Finished ONE")
expect(page).not_to have_link("Finished TWO")
click_link 'Valuation finished'
expect(page).not_to have_link("Valuating Investment ONE")
expect(page).not_to have_link("Valuating Investment TWO")
expect(page).not_to have_link("Finished ONE")
expect(page).to have_link("Finished TWO")
end end
scenario "Current filter is properly highlighted" do scenario "Current filter is properly highlighted" do