[#3477] Extract helper

This commit is contained in:
Frederic Merizen 2019-02-14 15:03:56 +01:00
parent 1d5f219303
commit 54cb65b8a7

View file

@ -114,38 +114,61 @@ class ProcedurePresentation < ApplicationRecord
case table case table
when 'self' when 'self'
date = Time.zone.parse(filter['value']).beginning_of_day rescue nil date = Time.zone.parse(filter['value']).beginning_of_day rescue nil
if date.present? Filter.new(
dossiers.where("#{column} BETWEEN ? AND ?", date, date + 1.day) dossiers
else ).where_datetime_matches(column, date)
[]
end
when 'type_de_champ', 'type_de_champ_private' when 'type_de_champ', 'type_de_champ_private'
relation = table == 'type_de_champ' ? :champs : :champs_private relation = table == 'type_de_champ' ? :champs : :champs_private
dossiers Filter.new(
.includes(relation) dossiers
.where("champs.type_de_champ_id = ?", filter['column'].to_i) .includes(relation)
.where("champs.value ILIKE ?", "%#{filter['value']}%") .where("champs.type_de_champ_id = ?", filter['column'].to_i)
).where_ilike('champs.value', filter['value'])
when 'etablissement' when 'etablissement'
if filter['column'] == 'entreprise_date_creation' if filter['column'] == 'entreprise_date_creation'
date = filter['value'].to_date rescue nil date = filter['value'].to_date rescue nil
dossiers Filter.new(
.includes(table) dossiers.includes(table)
.where("#{column} = ?", date) ).where_equals(column, date)
else else
dossiers Filter.new(
.includes(table) dossiers
.where("#{column} ILIKE ?", "%#{filter['value']}%") .includes(table)
).where_ilike(column, filter['value'])
end end
when 'user', 'individual' when 'user', 'individual'
dossiers Filter.new(
.includes(table) dossiers
.where("#{column} ILIKE ?", "%#{filter['value']}%") .includes(table)
).where_ilike(column, filter['value'])
end.pluck(:id) end.pluck(:id)
end.reduce(:&) end.reduce(:&)
end end
private private
class Filter
def initialize(dossiers)
@dossiers = dossiers
end
def where_datetime_matches(column, date)
if date.present?
@dossiers.where("#{column} BETWEEN ? AND ?", date, date + 1.day)
else
[]
end
end
def where_ilike(column, value)
@dossiers.where("#{column} ILIKE ?", "%#{value}%")
end
def where_equals(column, value)
@dossiers.where("#{column} = ?", value)
end
end
def check_allowed_displayed_fields def check_allowed_displayed_fields
displayed_fields.each do |field| displayed_fields.each do |field|
table = field['table'] table = field['table']