Class: Datagrid::Filters::DynamicFilter

Inherits:
BaseFilter
  • Object
show all
Includes:
SelectOptions
Defined in:
lib/datagrid/filters/dynamic_filter.rb

Instance Attribute Summary

Attributes inherited from BaseFilter

#block, #grid_class, #name, #options

Instance Method Summary collapse

Methods included from SelectOptions

#include_blank, #prompt, #select

Methods inherited from BaseFilter

#allow_blank?, #allow_nil?, #apply, #default, #default_filter, #default_filter_block, #dummy?, #form_builder_helper_name, form_builder_helper_name, #format, #header, #multiple, #multiple?, #parse, #separator

Constructor Details

#initializeDynamicFilter

Returns a new instance of DynamicFilter.



7
8
9
10
11
12
13
# File 'lib/datagrid/filters/dynamic_filter.rb', line 7

def initialize(*)
  super
  options[:select] ||= default_select
  unless options.has_key?(:include_blank)
    options[:include_blank] = false
  end
end

Instance Method Details

#default_filter_where(driver, scope, filter) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/datagrid/filters/dynamic_filter.rb', line 26

def default_filter_where(driver, scope, filter)
  field, operation, value = filter
  date_conversion = value.is_a?(Date) && driver.is_timestamp?(scope, field)
  case operation
  when '='
    if date_conversion
      value = Datagrid::Utils.format_date_as_timestamp(value)
    end
    driver.where(scope, field, value)
  when '=~'
    if column_type(field) == :string
      driver.contains(scope, field, value)
    else
      if date_conversion
        value = Datagrid::Utils.format_date_as_timestamp(value)
      end
      driver.where(scope, field, value)
    end
  when '>='
    if date_conversion
      value = value.beginning_of_day
    end
    driver.greater_equal(scope, field, value)
  when '<='
    if date_conversion
      value = value.end_of_day
    end
    driver.less_equal(scope, field, value)
  else
    raise "unknown operation: #{operation.inspect}"
  end
end

#operations_selectObject



59
60
61
62
63
# File 'lib/datagrid/filters/dynamic_filter.rb', line 59

def operations_select
  %w(= =~ >= <=).map do |operation|
    [I18n.t(operation, :scope => "datagrid.filters.dynamic.operations").html_safe, operation]
  end
end

#parse_values(filter) ⇒ Object



15
16
17
18
19
# File 'lib/datagrid/filters/dynamic_filter.rb', line 15

def parse_values(filter)
  field, operation, value = filter

  [field, operation, type_cast(field, value)]
end

#unapplicable_value?(filter) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/datagrid/filters/dynamic_filter.rb', line 21

def unapplicable_value?(filter)
  field, operation, value = filter
  field.blank? || operation.blank? || super(value)
end