Skip to content

Commit

Permalink
Add filter by goal
Browse files Browse the repository at this point in the history
  • Loading branch information
javierm committed Dec 18, 2020
1 parent f4f42da commit 3c75879
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 14 deletions.
23 changes: 19 additions & 4 deletions app/assets/stylesheets/admin/search.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
.admin [role=search] {
display: flex;

&.complex {
@include breakpoint(small only) {
flex-direction: column;
}

@include breakpoint(medium) {
select {
height: $line-height * 2;
margin: 0 rem-calc(12);
}
}
}

&:not(.complex) {
@include breakpoint(medium) {
width: 50%;
}
}

[type="submit"] {
@include button($background: $link);
border-radius: 0;
Expand All @@ -10,8 +29,4 @@
@include button-disabled;
}
}

@include breakpoint(medium) {
width: 50%;
}
}
1 change: 1 addition & 0 deletions app/components/admin/search_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%= form_tag(url, options) do |f| %>
<%= text_field_tag :search, search_terms.to_s, placeholder: label, "aria-label": label %>
<%= content %>
<%= submit_tag t("admin.shared.search.search") %>
<% end %>
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<%= header %>
<%= render Admin::SearchComponent.new(label: search_label) %>
<%= render Admin::SearchComponent.new(label: search_label, class: "complex") do |component| %>
<%= component.select_tag :goal_code, goal_options,
include_blank: goal_blank_option,
"aria-label": goal_label %>
<% end %>

<table>
<thead>
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 @@ -29,4 +29,16 @@ def edit_path_for(record)
def search_label
t("admin.shared.search.label.#{model_class.table_name}")
end

def goal_label
t("admin.shared.search.advanced_filters.sdg_goals.label")
end

def goal_blank_option
t("admin.shared.search.advanced_filters.sdg_goals.all")
end

def goal_options
options_from_collection_for_select(SDG::Goal.all, :code, :code_and_title, params[:goal_code])
end
end
6 changes: 5 additions & 1 deletion app/controllers/sdg_management/relations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ class SDGManagement::RelationsController < SDGManagement::BaseController
before_action :load_record, only: [:edit, :update]

def index
@records = relatable_class.accessible_by(current_ability).order(:id).page(params[:page])
@records = relatable_class
.accessible_by(current_ability)
.by_goal(params[:goal_code])
.order(:id)
.page(params[:page])

@records = @records.search(params[:search]) if params[:search].present?
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/concerns/sdg/relatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ module SDG::Relatable
end
end

class_methods do
def by_goal(code)
return all if code.blank?

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

def related_sdgs
sdg_relations.map(&:related_sdg)
end
Expand Down
4 changes: 4 additions & 0 deletions config/locales/en/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,10 @@ en:
true_value: "Yes"
false_value: "No"
search:
advanced_filters:
sdg_goals:
all: "All goals"
label: "By goal"
label:
booths: "Search booth by name or location"
budget_investments: "Search investments by title, description or heading"
Expand Down
4 changes: 4 additions & 0 deletions config/locales/es/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,10 @@ es:
true_value: ""
false_value: "No"
search:
advanced_filters:
sdg_goals:
all: "Todos los objetivos"
label: "Por objetivo"
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 @@ -100,4 +100,25 @@
expect(relatable.reload.sdg_goals).to match_array [SDG::Goal[1], SDG::Goal[2]]
end
end

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

it "returns records associated with that goal" do
same_association = create(:proposal, sdg_goals: [goal])
both_associations = create(:proposal, sdg_goals: [goal, another_goal])

expect(relatable.class.by_goal(goal.code)).to match_array [same_association, both_associations]
end

it "does not return records not associated with that goal" do
create(:proposal)
create(:proposal, sdg_goals: [another_goal])

expect(relatable.class.by_goal(goal.code)).to be_empty
end
end
end
30 changes: 22 additions & 8 deletions spec/system/sdg_management/relations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,31 @@
expect(page).to have_css "h2", exact_text: "Build a hospital"
end

scenario "search" do
create(:poll, name: "Internet speech freedom")
create(:poll, name: "SDG interest")
describe "search" do
scenario "search by terms" do
create(:poll, name: "Internet speech freedom")
create(:poll, name: "SDG interest")

visit sdg_management_polls_path
visit sdg_management_polls_path

fill_in "search", with: "speech"
click_button "Search"
fill_in "search", with: "speech"
click_button "Search"

expect(page).to have_content "Internet speech freedom"
expect(page).not_to have_content "SDG interest"
expect(page).to have_content "Internet speech freedom"
expect(page).not_to have_content "SDG interest"
end

scenario "goal filter" do
create(:budget_investment, title: "School", sdg_goals: [SDG::Goal[4]])
create(:budget_investment, title: "Hospital", sdg_goals: [SDG::Goal[3]])

visit sdg_management_budget_investments_path
select "4. Quality Education", from: "goal_code"
click_button "Search"

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

Expand Down

0 comments on commit 3c75879

Please sign in to comment.