Add filter by goal

This commit is contained in:
Javi Martín
2020-12-17 19:13:48 +01:00
parent 791ba73e12
commit c9362ffeb4
10 changed files with 101 additions and 14 deletions

View File

@@ -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;
@@ -10,8 +29,4 @@
@include button-disabled;
}
}
@include breakpoint(medium) {
width: 50%;
}
}

View File

@@ -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 %>

View File

@@ -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>

View File

@@ -29,4 +29,16 @@ class SDGManagement::Relations::IndexComponent < ApplicationComponent
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

View File

@@ -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

View File

@@ -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