Skip to content

Commit

Permalink
Add filter by target
Browse files Browse the repository at this point in the history
  • Loading branch information
javierm committed Dec 18, 2020
1 parent 3c75879 commit fcc530e
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<%= component.select_tag :goal_code, goal_options,
include_blank: goal_blank_option,
"aria-label": goal_label %>
<%= component.select_tag :target_code, target_options,
include_blank: target_blank_option,
"aria-label": target_label %>
<% end %>

<table>
Expand Down
12 changes: 12 additions & 0 deletions app/components/sdg_management/relations/index_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,19 @@ def goal_blank_option
t("admin.shared.search.advanced_filters.sdg_goals.all")
end

def target_label
t("admin.shared.search.advanced_filters.sdg_targets.label")
end

def target_blank_option
t("admin.shared.search.advanced_filters.sdg_targets.all")
end

def goal_options
options_from_collection_for_select(SDG::Goal.all, :code, :code_and_title, params[:goal_code])
end

def target_options
options_from_collection_for_select(SDG::Target.all.sort, :code, :code, params[:target_code])
end
end
1 change: 1 addition & 0 deletions app/controllers/sdg_management/relations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def index
@records = relatable_class
.accessible_by(current_ability)
.by_goal(params[:goal_code])
.by_target(params[:target_code])
.order(:id)
.page(params[:page])

Expand Down
10 changes: 9 additions & 1 deletion app/models/concerns/sdg/relatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ module SDG::Relatable

class_methods do
def by_goal(code)
by_sdg_related(SDG::Goal, code)
end

def by_target(code)
by_sdg_related(SDG::Target, code)
end

def by_sdg_related(sdg_class, code)
return all if code.blank?

joins(:sdg_goals).merge(SDG::Goal.where(code: code))
joins(sdg_class.table_name.to_sym).merge(sdg_class.where(code: code))
end
end

Expand Down
3 changes: 3 additions & 0 deletions config/locales/en/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,9 @@ en:
sdg_goals:
all: "All goals"
label: "By goal"
sdg_targets:
all: "All targets"
label: "By target"
label:
booths: "Search booth by name or location"
budget_investments: "Search investments by title, description or heading"
Expand Down
3 changes: 3 additions & 0 deletions config/locales/es/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,9 @@ es:
sdg_goals:
all: "Todos los objetivos"
label: "Por objetivo"
sdg_targets:
all: "Todas las metas"
label: "Por meta"
label:
booths: "Buscar urna por nombre"
budget_investments: "Buscar proyectos por título, descripción o partida"
Expand Down
21 changes: 21 additions & 0 deletions spec/models/sdg/relatable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,25 @@
expect(relatable.class.by_goal(goal.code)).to be_empty
end
end

describe ".by_target" do
it "returns everything if no code is provided" do
expect(relatable.class.by_target("")).to eq [relatable]
expect(relatable.class.by_target(nil)).to eq [relatable]
end

it "returns records associated with that target" do
same_association = create(:proposal, sdg_targets: [target])
both_associations = create(:proposal, sdg_targets: [target, another_target])

expect(relatable.class.by_target(target.code)).to match_array [same_association, both_associations]
end

it "does not return records not associated with that target" do
create(:proposal)
create(:proposal, sdg_targets: [another_target])

expect(relatable.class.by_target(target.code)).to be_empty
end
end
end
12 changes: 12 additions & 0 deletions spec/system/sdg_management/relations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@
expect(page).not_to have_content "Hospital"
end
end

scenario "target filter" do
create(:budget_investment, title: "School", sdg_targets: [SDG::Target[4.1]])
create(:budget_investment, title: "Preschool", sdg_targets: [SDG::Target[4.2]])

visit sdg_management_budget_investments_path
select "4.1", from: "target_code"
click_button "Search"

expect(page).to have_content "School"
expect(page).not_to have_content "Preschool"
end
end

describe "Edit" do
Expand Down

0 comments on commit fcc530e

Please sign in to comment.