module SearchMe::Filters
Public Instance Methods
filter_by_range(range)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
build_between_query_for(duration)
click to toggle source
# File lib/search_me/filters.rb, line 78 def build_between_query_for(duration) field = arel_table[search_me_config.time_field] arel = field.eq(duration[0]) .or( field.gt(duration[0]).and(field.lt(duration[1])) ) .or( field.eq(duration[1]) ) where( arel ) end
month_for_date(date)
click to toggle source
# File lib/search_me/filters.rb, line 86 def month_for_date(date) [date.beginning_of_month, date.end_of_month] end
quarter_for_date(date)
click to toggle source
# File lib/search_me/filters.rb, line 94 def quarter_for_date(date) [date.beginning_of_quarter, date.end_of_quarter] end
sanitize_params(*params)
click to toggle source
# File lib/search_me/filters.rb, line 98 def sanitize_params(*params) # i.e. Integer or nil. Also no zero year, month, day params.map { |p| p.to_i if p.present? && !p.to_i.zero? } end
year_for_date(date)
click to toggle source
# File lib/search_me/filters.rb, line 90 def year_for_date(date) [date.beginning_of_year, date.end_of_year] end