improve filter
This commit is contained in:
parent
0d2e10d243
commit
351dc53029
3 changed files with 25 additions and 8 deletions
|
|
@ -69,7 +69,7 @@ class ChecksController < ApplicationController
|
|||
end
|
||||
|
||||
def filter_params
|
||||
@filter_params ||= params.permit(filter: [ :s, priority: [], conformity_level: [], standard_ids: [], principle_id: [] ])[:filter] || {}
|
||||
@filter_params ||= params.permit(filter: [ :s, priority: [], conformity_level: [], standard_ids: [], principle_id: [], target_disabilities: []])[:filter] || {}
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
|||
|
|
@ -54,19 +54,18 @@ class Check < ApplicationRecord
|
|||
|
||||
before_validation { self.number = self.class.maximum(:id).to_i + 1 unless self.number.present? }
|
||||
|
||||
scope(:search, lambda { |term|
|
||||
scope(:search, lambda { |terms|
|
||||
# TODO: Search only fields for current locale.
|
||||
joined = joins("INNER JOIN action_text_rich_texts ON action_text_rich_texts.record_id = checks.id AND record_type = 'Check'")
|
||||
if term.to_i > 0
|
||||
joined.where("checks.name_de LIKE :term OR checks.name_de LIKE :term OR checks.number = :number OR action_text_rich_texts.body LIKE :term", term: "%#{term}%", number: term.to_i).distinct
|
||||
else
|
||||
joined.where("checks.name_de LIKE :term OR checks.name_de LIKE :term OR action_text_rich_texts.body LIKE :term", term: "%#{term}%").distinct
|
||||
terms.split(" ").uniq.each_with_index do |term, i|
|
||||
joined = joined.where("checks.name_de LIKE :term OR action_text_rich_texts.body LIKE :term", term: "%#{term}%")
|
||||
end
|
||||
joined.distinct
|
||||
})
|
||||
|
||||
scope(:filter_by, lambda { |filter|
|
||||
relation = where(nil)
|
||||
filter.slice(:s, :principle_id, :priorities, :standard_ids, :conformity_level).each do |key, value|
|
||||
filter.slice(:s, :principle_id, :priorities, :standard_ids, :conformity_level, :target_disabilities).each do |key, value|
|
||||
next unless value.present?
|
||||
|
||||
case key
|
||||
|
|
@ -74,6 +73,16 @@ class Check < ApplicationRecord
|
|||
relation = relation.search(value)
|
||||
when "standard_ids"
|
||||
relation = relation.joins(:standards).where(standards: { id: value })
|
||||
when "target_disabilities"
|
||||
cond = String.new
|
||||
value.intersection(%w(auditory visual physical cognitive)).each_with_index do |v, i|
|
||||
if i == 0
|
||||
cond << "checks.#{v} = 1"
|
||||
else
|
||||
cond << " OR checks.#{v} = 1"
|
||||
end
|
||||
end
|
||||
relation = relation.where("(#{cond})")
|
||||
else
|
||||
relation = relation.where(key => value)
|
||||
end
|
||||
|
|
@ -90,6 +99,10 @@ class Check < ApplicationRecord
|
|||
[ "links", "principle", "rich_text_comment", "rich_text_conformity_notice_de", "rich_text_conformity_notice_en", "rich_text_criterion_de", "rich_text_criterion_details_de", "rich_text_criterion_details_en", "rich_text_criterion_en", "rich_text_example_de", "rich_text_example_en", "rich_text_exemption_details_de", "rich_text_exemption_details_en", "rich_text_powerpoint_text_de", "rich_text_powerpoint_text_en", "rich_text_quick_criterion_de", "rich_text_quick_criterion_en", "rich_text_quick_fail_de", "rich_text_quick_fail_en", "rich_text_quick_fix_de", "rich_text_quick_fix_en", "rich_text_standard_text_de", "rich_text_standard_text_en", "rich_text_test_instructions", "standards" ]
|
||||
end
|
||||
|
||||
def self.target_disabilities
|
||||
%i[visual auditory physical cognitive].index_with { self }
|
||||
end
|
||||
|
||||
def display_target_disabilities
|
||||
%i[visual auditory physical cognitive].select { |d| send(:"#{d}?") }.map { |d| I18n.t("disability.#{d}") }.join(", ")
|
||||
end
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ h1
|
|||
= bootstrap_form_with(url: checks_path, method: :get, scope: :filter) do |form|
|
||||
= form.hidden_field :page, value: params[:page] if params[:page]
|
||||
.row
|
||||
.col-lg-5
|
||||
.col-lg-12
|
||||
= form.text_field(:s, placeholder: "suchen...", hide_label: true, value: filter_params[:s])
|
||||
.col-lg-2
|
||||
- Principle.all.sort_by(&:name_de).each do |principle|
|
||||
|
|
@ -37,6 +37,10 @@ h1
|
|||
- Check.priorities.each do |priority, value|
|
||||
.form-check.form-check-inline
|
||||
= form.check_box :priority, { label: t("priority.#{priority}"), multiple: true, checked: (filter_params[:priority] || []).include?(value.to_s) }, value, nil
|
||||
.col-lg-1
|
||||
- Check.target_disabilities.map { |d, _v| [I18n.t("disability.#{d}"), d] }.each do |target_disability, value|
|
||||
.form-check.form-check-inline
|
||||
= form.check_box :target_disabilities, { label: target_disability, multiple: true, checked: (filter_params[:target_disabilities] || []).include?(value.to_s) }, value, nil
|
||||
.row
|
||||
.col
|
||||
= form.submit name: "", value: "Filtern"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue