Module: SearchMe::Filters

Defined in:
lib/search_me/filters.rb

Instance Method Summary collapse

Instance Method Details

#filter_by_range(range) ⇒ Object



73
74
75
# File 'lib/search_me/filters.rb', line 73

def filter_by_range(range)
  build_between_query_for([range.first, range.last])
end

#filter_month(month = nil, year = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/search_me/filters.rb', line 23

def filter_month(month = nil, year = nil)
  month, year = sanitize_params(month, year)

  month ||= Date.today.month
  year  ||= Date.today.year
  date    = Date.new(year, month, 1)

  build_between_query_for(month_for_date(date))
end

#filter_month_quarter_or_year(month = nil, quarter = nil, year = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/search_me/filters.rb', line 3

def filter_month_quarter_or_year(month=nil, quarter=nil, year=nil)
  month, quarter, year = sanitize_params(month, quarter, year)
  if month
    filter_month(month, year)
  elsif quarter
    filter_quarter(quarter, year)
  else
    filter_year(year)
  end
end

#filter_named_duration(duration) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/search_me/filters.rb', line 45

def filter_named_duration(duration)
  today = Date.today

  duration = case duration
  when "current_month"
    month_for_date(today)
  when "last_month"
    month_for_date(1.month.ago)
  when "f_last_month"
    month_for_date(2.month.ago)
  when "last_quarter"
    quarter_for_date(today.beginning_of_quarter - 1)
  when "current_year"
    year_for_date(today)
  when "last_year"
    year_for_date(1.year.ago)
  when "f_last_year"
    year_for_date(2.year.ago)
  else
    nil # do nothing
  end
  if duration
    build_between_query_for(duration)
  else
    self.all
  end
end

#filter_quarter(quarter = nil, year = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/search_me/filters.rb', line 33

def filter_quarter(quarter = nil, year = nil)
  quarter, year = sanitize_params(quarter, year)
  quarter ||= (Date.today.month - 1) / 3 + 1
  return self if quarter > 4

  year    ||= Date.today.year
  month     = (quarter - 1) * 3 + 1
  date      = Date.new(year, month, 1)
  
  build_between_query_for(quarter_for_date(date))
end

#filter_year(year = nil) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/search_me/filters.rb', line 14

def filter_year(year = nil)
  year = sanitize_params(year).first

  year ||= Date.today.year
  date   = Date.new(year, 1, 1)

  build_between_query_for(year_for_date(date))
end