Class: Dorsale::SmallData::FilterStrategyByTimePeriod

Inherits:
FilterStrategyByKeyValue show all
Defined in:
app/filters/dorsale/small_data/filter_strategy_by_time_period.rb

Instance Attribute Summary

Attributes inherited from FilterStrategyByKeyValue

#key

Instance Method Summary collapse

Methods inherited from FilterStrategyByKeyValue

#initialize

Constructor Details

This class inherits a constructor from Dorsale::SmallData::FilterStrategyByKeyValue

Instance Method Details

#apply(query, value) ⇒ Object



2
3
4
5
6
7
8
9
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
# File 'app/filters/dorsale/small_data/filter_strategy_by_time_period.rb', line 2

def apply(query, value)
  if value == "today"
    a = Time.zone.now.beginning_of_day
    b = Time.zone.now.end_of_day
  elsif value == "yesterday"
    a = (Time.zone.now - 1.day).beginning_of_day
    b = (Time.zone.now - 1.day).end_of_day
  elsif value == "this_week"
    a = Time.zone.now.beginning_of_week
    b = Time.zone.now.end_of_week
  elsif value == "this_month"
    a = Time.zone.now.beginning_of_month
    b = Time.zone.now.end_of_month
  elsif value == "this_year"
    a = Time.zone.now.beginning_of_year
    b = Time.zone.now.end_of_year
  elsif value == "last_week"
    a = (Time.zone.now - 1.week).beginning_of_week
    b = (Time.zone.now - 1.week).end_of_week
  elsif value == "last_month"
    a = (Time.zone.now - 1.month).beginning_of_month
    b = (Time.zone.now - 1.month).end_of_month
  elsif value == "last_year"
    a = (Time.zone.now - 1.year).beginning_of_year
    b = (Time.zone.now - 1.year).end_of_year
  else
    return query
  end

  criteria = "#{query.model.table_name}.#{key}"

  query
    .where("#{criteria} >= ?", a.to_date)
    .where("#{criteria} <= ?", b.to_date)
end