Extract budget executions filters to a component
This commit is contained in:
23
app/components/budgets/executions/filters_component.html.erb
Normal file
23
app/components/budgets/executions/filters_component.html.erb
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<%= form_tag(budget_executions_path(budget), method: :get) do %>
|
||||||
|
<div class="small-12 medium-3 column">
|
||||||
|
<%= label_tag :milestone_tag, t("budgets.executions.filters.milestone_tag.label") %>
|
||||||
|
<%= select_tag :milestone_tag,
|
||||||
|
options_for_select(
|
||||||
|
options_for_milestone_tags,
|
||||||
|
params[:milestone_tag]
|
||||||
|
),
|
||||||
|
class: "js-submit-on-change",
|
||||||
|
prompt: t("budgets.executions.filters.milestone_tag.all",
|
||||||
|
count: budget.investments.winners.with_milestones.count) %>
|
||||||
|
</div>
|
||||||
|
<div class="small-12 medium-3 column">
|
||||||
|
<%= label_tag :status, t("budgets.executions.filters.status.label") %>
|
||||||
|
<%= select_tag :status,
|
||||||
|
options_from_collection_for_select(statuses,
|
||||||
|
:id, lambda { |s| "#{s.name} (#{filters_select_counts(s.id)})" },
|
||||||
|
params[:status]),
|
||||||
|
class: "js-submit-on-change",
|
||||||
|
prompt: t("budgets.executions.filters.status.all",
|
||||||
|
count: budget.investments.winners.with_milestones.count) %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
20
app/components/budgets/executions/filters_component.rb
Normal file
20
app/components/budgets/executions/filters_component.rb
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
class Budgets::Executions::FiltersComponent < ApplicationComponent
|
||||||
|
attr_reader :budget, :statuses
|
||||||
|
|
||||||
|
def initialize(budget, statuses)
|
||||||
|
@budget = budget
|
||||||
|
@statuses = statuses
|
||||||
|
end
|
||||||
|
|
||||||
|
def options_for_milestone_tags
|
||||||
|
budget.investments_milestone_tags.map do |tag|
|
||||||
|
["#{tag} (#{budget.investments.winners.by_tag(tag).count})", tag]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def filters_select_counts(status)
|
||||||
|
budget.investments.winners.with_milestone_status_id(status).count
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,14 +1,4 @@
|
|||||||
module BudgetExecutionsHelper
|
module BudgetExecutionsHelper
|
||||||
def filters_select_counts(status)
|
|
||||||
@budget.investments.winners.with_milestone_status_id(status).count
|
|
||||||
end
|
|
||||||
|
|
||||||
def options_for_milestone_tags
|
|
||||||
@budget.investments_milestone_tags.map do |tag|
|
|
||||||
["#{tag} (#{@budget.investments.winners.by_tag(tag).count})", tag]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def first_milestone_with_image(investment)
|
def first_milestone_with_image(investment)
|
||||||
investment.milestones.order_by_publication_date
|
investment.milestones.order_by_publication_date
|
||||||
.select { |milestone| milestone.image.present? }.last
|
.select { |milestone| milestone.image.present? }.last
|
||||||
|
|||||||
@@ -42,29 +42,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="small-12 medium-9 large-10 column">
|
<div class="small-12 medium-9 large-10 column">
|
||||||
<%= form_tag(budget_executions_path(@budget), method: :get) do %>
|
<%= render Budgets::Executions::FiltersComponent.new(@budget, @statuses) %>
|
||||||
<div class="small-12 medium-3 column">
|
|
||||||
<%= label_tag :milestone_tag, t("budgets.executions.filters.milestone_tag.label") %>
|
|
||||||
<%= select_tag :milestone_tag,
|
|
||||||
options_for_select(
|
|
||||||
options_for_milestone_tags,
|
|
||||||
params[:milestone_tag]
|
|
||||||
),
|
|
||||||
class: "js-submit-on-change",
|
|
||||||
prompt: t("budgets.executions.filters.milestone_tag.all",
|
|
||||||
count: @budget.investments.winners.with_milestones.count) %>
|
|
||||||
</div>
|
|
||||||
<div class="small-12 medium-3 column">
|
|
||||||
<%= label_tag :status, t("budgets.executions.filters.status.label") %>
|
|
||||||
<%= select_tag :status,
|
|
||||||
options_from_collection_for_select(@statuses,
|
|
||||||
:id, lambda { |s| "#{s.name} (#{filters_select_counts(s.id)})" },
|
|
||||||
params[:status]),
|
|
||||||
class: "js-submit-on-change",
|
|
||||||
prompt: t("budgets.executions.filters.status.all",
|
|
||||||
count: @budget.investments.winners.with_milestones.count) %>
|
|
||||||
</div>
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
<% if @investments_by_heading.any? %>
|
<% if @investments_by_heading.any? %>
|
||||||
<%= render "budgets/executions/investments" %>
|
<%= render "budgets/executions/investments" %>
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
require "rails_helper"
|
require "rails_helper"
|
||||||
|
|
||||||
describe BudgetExecutionsHelper do
|
describe Budgets::Executions::FiltersComponent do
|
||||||
describe "#options_for_milestone_tags" do
|
let(:budget) { create(:budget) }
|
||||||
let(:budget) { create(:budget) }
|
let(:component) { Budgets::Executions::FiltersComponent.new(budget, []) }
|
||||||
|
|
||||||
|
describe "#options_for_milestone_tags" do
|
||||||
it "does not return duplicate records for tags in different contexts" do
|
it "does not return duplicate records for tags in different contexts" do
|
||||||
create(:budget_investment, :winner, budget: budget, milestone_tag_list: ["Multiple"])
|
create(:budget_investment, :winner, budget: budget, milestone_tag_list: ["Multiple"])
|
||||||
create(:budget_investment, :winner, budget: budget, milestone_tag_list: ["Multiple"])
|
create(:budget_investment, :winner, budget: budget, milestone_tag_list: ["Multiple"])
|
||||||
create(:budget_investment, :winner, budget: budget, milestone_tag_list: ["Dup"], tag_list: ["Dup"])
|
create(:budget_investment, :winner, budget: budget, milestone_tag_list: ["Dup"], tag_list: ["Dup"])
|
||||||
|
|
||||||
@budget = budget
|
expect(component.options_for_milestone_tags).to eq [["Dup (1)", "Dup"], ["Multiple (2)", "Multiple"]]
|
||||||
|
|
||||||
expect(options_for_milestone_tags).to eq [["Dup (1)", "Dup"], ["Multiple (2)", "Multiple"]]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user