[#3477] Make filtering by datetimes a proper scope

This commit is contained in:
Frederic Merizen 2019-02-27 12:03:53 +01:00 committed by Frederic Merizen
parent 9096f923b1
commit a87e3ac697
3 changed files with 18 additions and 13 deletions

View file

@ -0,0 +1,15 @@
module DossierFilteringConcern
extend ActiveSupport::Concern
included do
scope :filter_by_datetimes, lambda { |column, dates|
if dates.present?
dates
.map { |date| self.where(column => date..(date + 1.day)) }
.reduce(:or)
else
none
end
}
end
end

View file

@ -1,4 +1,6 @@
class Dossier < ApplicationRecord
include DossierFilteringConcern
enum state: {
brouillon: 'brouillon',
en_construction: 'en_construction',

View file

@ -109,9 +109,7 @@ class ProcedurePresentation < ApplicationRecord
dates = values
.map { |v| Time.zone.parse(v).beginning_of_day rescue nil }
.compact
Filter.new(
dossiers
).where_datetime_matches(column, dates)
dossiers.filter_by_datetimes(column, dates)
when 'type_de_champ', 'type_de_champ_private'
relation = table == 'type_de_champ' ? :champs : :champs_private
Filter.new(
@ -174,16 +172,6 @@ class ProcedurePresentation < ApplicationRecord
@dossiers = dossiers
end
def where_datetime_matches(column, dates)
if dates.present?
dates
.map { |date| @dossiers.where(column => date..(date + 1.day)) }
.reduce(:or)
else
@dossiers.none
end
end
def where_ilike(table, column, values)
table_column = ProcedurePresentation.sanitized_column(table, column)
q = Array.new(values.count, "(#{table_column} ILIKE ?)").join(' OR ')