Class: BMC::Filter::ByDateOrDatetimePeriod

Inherits:
ByKeyValue
  • Object
show all
Defined in:
app/filters/bmc/filter/by_date_or_datetime_period.rb

Direct Known Subclasses

ByDatePeriod, ByDatetimePeriod

Instance Attribute Summary

Attributes inherited from ByKeyValue

#key

Instance Method Summary collapse

Methods inherited from ByKeyValue

#column_for

Constructor Details

#initializeByDateOrDatetimePeriod

Returns a new instance of ByDateOrDatetimePeriod.



2
3
4
5
6
7
8
# File 'app/filters/bmc/filter/by_date_or_datetime_period.rb', line 2

def initialize(*)
  if instance_of?(BMC::Filter::ByDateOrDatetimePeriod)
    raise "please use FilterStrategyByDatePeriod or FilterStrategyByDatetimePeriod"
  end

  super
end

Instance Method Details

#call(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/bmc/filter/by_date_or_datetime_period.rb', line 10

def call(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