Class: Symphonia::ModelFilters::SelectFilter

Inherits:
StringFilter show all
Defined in:
lib/symphonia/model_filters/select_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 = {}) ⇒ SelectFilter

Returns a new instance of SelectFilter.



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

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

Instance Attribute Details

#available_valuesObject



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

def available_values
  return @available_values.call(context, self) if @available_values.is_a? Proc

  @available_values || case @attribute
                       when Symphonia::ModelAttributes::EnumAttribute
                         @query.model.send(@attribute.name.to_s.pluralize).keys
                       end
end

Instance Method Details

#apply(scope) ⇒ Object



32
33
34
35
36
# File 'lib/symphonia/model_filters/select_filter.rb', line 32

def apply(scope)
  Rails.logger.debug("Apply filter #{name} '#{operator}' #{value}")
  t = scope.arel_table
  scope.where(t[name].send(operator, value))
end

#form_field(context) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/symphonia/model_filters/select_filter.rb', line 38

def form_field(context)
  selected = Array(@query.active_filters[name])
  context.(:div, class: 'input-group') do
    context.(:div, class: 'input-group-prepend') do
      context.(:span,
                          context.check_box_tag("o[#{form_field_name}]", '!', operator == 'not_in', title: 'Not In'), class: "input-group-text")
    end +
      context.select_tag("#{form_field_name}[]", context.options_for_select(available_values, selected),
                         class: 'form-control', include_blank: true, multiple: selected.size > 1) +
      context.(:div,
                          context.link_to(context.fa_icon('plus-square-o'), 'javascript:void(0)', onclick: "toggleMultiSelect($(this).closest('.input-group').find('select'))", title: I18n.t(:title_toggle_multiselect), class: "input-group-text"), class: 'input-group-append')
  end
end

#operator=(to) ⇒ Object



28
29
30
# File 'lib/symphonia/model_filters/select_filter.rb', line 28

def operator=(to)
  @operator = 'not_in' if to == '!'
end

#value=(to) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/symphonia/model_filters/select_filter.rb', line 20

def value=(to)
  @value = if to.is_a?(Array)
             to
           else
             to.to_s.split("|")
           end
end