Class: Symphonia::ModelFilters::DateFilter

Inherits:
Base
  • Object
show all
Defined in:
lib/symphonia/model_filters/date_filter.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#name, #operator, #options, #query, #type, #value

Instance Method Summary collapse

Methods inherited from Base

#active?, #caption, #inspect

Constructor Details

#initialize(name, query, options = {}) ⇒ DateFilter

Returns a new instance of DateFilter.



6
7
8
9
# File 'lib/symphonia/model_filters/date_filter.rb', line 6

def initialize(name, query, options = {})
  super
  @operator = 'in'
end

Instance Attribute Details

#available_valuesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/symphonia/model_filters/date_filter.rb', line 34

def available_values
  {
    past: {
      last_month: -> { d = Date.today.last_month.beginning_of_month; d..d.end_of_month },
      last_year: -> { d = Date.today.last_year.beginning_of_year; d..d.end_of_year },
    },
    present: {
      today: -> { Date.today },
      this_month: -> { Date.today.beginning_of_month..Date.today.end_of_month },
      this_year: -> { Date.today.beginning_of_year..Date.today.end_of_year },
      custom: ->(data) { data[:from].to_date..data[:to].to_date },
    },
    future: {
      next_month: -> { d = Date.today.next_month.beginning_of_month; d..d.end_of_month },
      next_year: -> { d = Date.today.next_year.beginning_of_year; d..d.end_of_year },
    },
  }
end

Instance Method Details

#apply(scope) ⇒ Object

def operator=(o)

if o == '!'
  @operator = 'not_in'
end

end



28
29
30
31
32
# File 'lib/symphonia/model_filters/date_filter.rb', line 28

def apply(scope)
  super
  t = scope.arel_table
  scope.where(t[name].send(operator, value))
end

#form_field(context) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/symphonia/model_filters/date_filter.rb', line 53

def form_field(context)
  groups = available_values.each_with_object({}) do |(time, values), mem|
    mem[context.t("time.#{time}")] = values.collect do |(k, _val)|
      [context.t("query_options.filter_date.values.#{k}"), k]
    end
  end
  selected = Array(@query.active_filters[name])
  context.(:div, class: 'row') do
    context.select_tag(form_field_name.to_s, context.grouped_options_for_select(groups, selected), {
      class: 'form-control',
      include_blank: true,
      onchange: 'symphoniaFiltersForm.dateFilterCallback(this)',
    }) + context.(:div, class: 'filter-custom-date clearfix input-daterange input-group inline',
                             style: 'display: none') do
      context.date_field_tag("#{form_field_name}contextustom[to]", '', {
        placeholder: context.t('query_options.filter_date.to'),
        class: 'form-control datepicker pull-right',
      }) +
        # context.content_tag(:span, context.t('query_options.filter_date.to'), class: 'input-group-addon')+
        context.date_field_tag("#{form_field_name}_custom[from]", '', {
          placeholder: _c.t('query_options.filter_date.from'),
          class: 'form-control datepicker',
        })
    end
  end
end

#value=(to) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/symphonia/model_filters/date_filter.rb', line 11

def value=(to)
  v = available_values.values.reduce(:merge)[to.to_sym]
  @operator = 'between' unless q == 'today'
  @value = if to == 'custom'
             data = query.controller.params.require(:date_from_custom).permit(:from, :to)
             v.call(data)
           else
             v.call
           end
end