Class: ClarkKent::ReportDateFilter

Inherits:
ReportFilter
  • Object
show all
Defined in:
app/models/clark_kent/report_date_filter.rb

Constant Summary collapse

WeekDayOptions =
['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
MonthDayOptions =
['beginning of month']
DayOptions =
WeekDayOptions + MonthDayOptions
WeekPeriodOptions =
{
  'previous week' => 'last_week',
  'same week' => 'this_week',
  'following week' => 'next_week'
}
MonthPeriodOptions =
{
  'previous month' => 'last_month',
  'same month' => 'this_month',
  'following month' => 'next_month'
}
PeriodOptions =
WeekPeriodOptions.merge MonthPeriodOptions
Durations =
['day','week','two weeks','month']

Instance Attribute Summary

Attributes inherited from ReportFilter

#filter_value_1, #filter_value_2

Instance Method Summary collapse

Methods inherited from ReportFilter

#display_name, #filter_match_param, #filter_match_value

Methods included from Cloneable

#cloneable_attributes, #reset_timestamps

Instance Method Details

#begin_dateObject



35
36
37
38
39
40
41
# File 'app/models/clark_kent/report_date_filter.rb', line 35

def begin_date
  @begin_date = Date.today
  direction, period = self.period_offset
  @begin_date = @begin_date.send(direction, 1.send(period)) if direction
  @begin_date = @begin_date.find_day(self.day_offset) if self.day_offset
  @begin_date
end

#begin_param_nameObject



27
28
29
# File 'app/models/clark_kent/report_date_filter.rb', line 27

def begin_param_name
  "#{self.filter_name}_from"
end

#date_displayObject



75
76
77
# File 'app/models/clark_kent/report_date_filter.rb', line 75

def date_display
  "starts on #{self.kind_of_day} #{self.offset_pretty} for 1 #{self.duration}"
end

#day_offsetObject



43
44
45
46
# File 'app/models/clark_kent/report_date_filter.rb', line 43

def day_offset
  return false if 'today' == self.kind_of_day
  self.kind_of_day.gsub(/ /,'_')
end

#end_dateObject



54
55
56
57
58
59
60
61
62
63
# File 'app/models/clark_kent/report_date_filter.rb', line 54

def end_date
  return self.begin_date if 'day' == self.duration
  return self.begin_date + 6.days if 'week' == self.duration
  return self.begin_date + 13.days if 'two weeks' == self.duration
  if Date::DAYNAMES.include? self.kind_of_day.capitalize
    return self.begin_date + 1.month
  else
    return self.begin_date.end_of_month
  end
end

#end_param_nameObject



31
32
33
# File 'app/models/clark_kent/report_date_filter.rb', line 31

def end_param_name
  "#{self.filter_name}_until"
end

#filter_match_paramsObject



23
24
25
# File 'app/models/clark_kent/report_date_filter.rb', line 23

def filter_match_params
  [[self.begin_param_name,self.begin_date],[self.end_param_name,self.end_date]]
end

#handle_filter_valueObject



65
66
67
68
69
# File 'app/models/clark_kent/report_date_filter.rb', line 65

def handle_filter_value
  if self.filter_value_1.present? and self.filter_value_2.present?
    self.filter_value = [filter_value_1, self.filter_value_2].join(' ')
  end
end

#offset_prettyObject



71
72
73
# File 'app/models/clark_kent/report_date_filter.rb', line 71

def offset_pretty
  self.class::PeriodOptions.rassoc(self.offset).first
end

#period_offsetObject



48
49
50
51
52
# File 'app/models/clark_kent/report_date_filter.rb', line 48

def period_offset
  direction, period = self.offset.split('_')
  direction = {'last' => '-', 'next' => '+', 'this' => false, '' => false}[direction.to_s]
  return [direction, period]
end