Class: Agilibox::SmallData::FilterStrategyByDateOrDatetimePeriod

Inherits:
FilterStrategyByKeyValue
  • Object
show all
Defined in:
app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb

Instance Method Summary collapse

Constructor Details

#initializeFilterStrategyByDateOrDatetimePeriod

Returns a new instance of FilterStrategyByDateOrDatetimePeriod.



2
3
4
5
6
7
8
# File 'app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb', line 2

def initialize(*)
  if self.class == Agilibox::SmallData::FilterStrategyByDateOrDatetimePeriod
    raise "please use FilterStrategyByDatePeriod or FilterStrategyByDatetimePeriod"
  end

  super
end

Instance Method Details

#apply(query, value) ⇒ Object

rubocop:disable Metrics/MethodLength



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb', line 10

def apply(query, value) # rubocop:disable Metrics/MethodLength
  value = value.to_s

  if value == "today"
    a = now
    b = now
  elsif value == "yesterday"
    a = (now - 1.day)
    b = (now - 1.day)
  elsif value == "this_week"
    a = now.beginning_of_week
    b = now.end_of_week
  elsif value == "this_month"
    a = now.beginning_of_month
    b = now.end_of_month
  elsif value == "this_year"
    a = now.beginning_of_year
    b = now.end_of_year
  elsif value == "last_week"
    a = (now - 1.week).beginning_of_week
    b = (now - 1.week).end_of_week
  elsif value == "last_month"
    a = (now - 1.month).beginning_of_month
    b = (now - 1.month).end_of_month
  elsif value == "last_year"
    a = (now - 1.year).beginning_of_year
    b = (now - 1.year).end_of_year
  elsif (m = value.match(/last\.([0-9]+)\.(days?|weeks?|months?|years?)/))
    a = now - m[1].to_i.public_send(m[2])
    b = now
  else
    return query
  end

  if now.is_a?(Time)
    a = a.beginning_of_day if a
    b = b.end_of_day       if b
  end

  column = column_for(query)

  query = query.where("#{column} >= ?", a) if a
  query = query.where("#{column} <= ?", b) if b

  query
end