Class: Datagrid::Filters::DynamicFilter

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

Defined Under Namespace

Classes: FilterValue

Constant Summary collapse

EQUAL_OPERATION =
"="
LIKE_OPERATION =
"=~"
MORE_EQUAL_OPERATION =
">="
LESS_EQUAL_OPERATION =
"<="
DEFAULT_OPERATIONS =
[
  EQUAL_OPERATION,
  LIKE_OPERATION,
  MORE_EQUAL_OPERATION,
  LESS_EQUAL_OPERATION,
].freeze
AVAILABLE_OPERATIONS =
%w[= =~ >= <=].freeze

Instance Attribute Summary

Attributes inherited from BaseFilter

#block, #grid_class, #name, #options

Instance Method Summary collapse

Methods included from SelectOptions

#include_blank, #prompt, #select, #select_values

Methods inherited from BaseFilter

#allow_blank?, #allow_nil?, #apply, #default, #default_scope?, #dummy?, #enabled?, #enum_checkboxes?, form_builder_helper_name, #form_builder_helper_name, #format, #header, #input_options, #label_options, #multiple?, #parse, #range?, #separator, #supports_range?, #type

Constructor Details

#initialize(grid, name, **options, &block) ⇒ DynamicFilter

Returns a new instance of DynamicFilter.



22
23
24
25
26
27
# File 'lib/datagrid/filters/dynamic_filter.rb', line 22

def initialize(grid, name, **options, &block)
  options[:select] ||= default_select
  options[:operations] ||= DEFAULT_OPERATIONS
  options[:include_blank] = false unless options.key?(:include_blank)
  super
end

Instance Method Details

#default_filter_where(scope, filter) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/datagrid/filters/dynamic_filter.rb', line 41

def default_filter_where(scope, filter)
  field = filter.field
  operation = filter.operation
  value = filter.value
  date_conversion = value.is_a?(Date) && driver.timestamp_column?(scope, field)

  return scope if field.blank? || operation.blank?

  unless operations.include?(operation)
    raise Datagrid::FilteringError,
      "Unknown operation: #{operation.inspect}. Available operations: #{operations.join(' ')}"
  end

  case operation
  when EQUAL_OPERATION
    value = Datagrid::Utils.format_date_as_timestamp(value) if date_conversion
    driver.where(scope, field, value)
  when LIKE_OPERATION
    if column_type(field) == :string
      driver.contains(scope, field, value)
    else
      value = Datagrid::Utils.format_date_as_timestamp(value) if date_conversion
      driver.where(scope, field, value)
    end
  when MORE_EQUAL_OPERATION
    value = value.beginning_of_day if date_conversion
    driver.greater_equal(scope, field, value)
  when LESS_EQUAL_OPERATION
    value = value.end_of_day if date_conversion
    driver.less_equal(scope, field, value)
  else
    raise Datagrid::FilteringError,
      "Unknown operation: #{operation.inspect}. Use filter block argument to implement operation"
  end
end

#default_input_optionsObject



29
30
31
# File 'lib/datagrid/filters/dynamic_filter.rb', line 29

def default_input_options
  { **super, type: nil }
end

#operationsObject



77
78
79
# File 'lib/datagrid/filters/dynamic_filter.rb', line 77

def operations
  options[:operations]
end

#operations_selectObject



81
82
83
84
85
# File 'lib/datagrid/filters/dynamic_filter.rb', line 81

def operations_select
  operations.map do |operation|
    [I18n.t(operation, scope: "datagrid.filters.dynamic.operations"), operation]
  end
end

#parse_values(filter) ⇒ Object



33
34
35
# File 'lib/datagrid/filters/dynamic_filter.rb', line 33

def parse_values(filter)
  filter ? FilterValue.new(grid_class, filter) : nil
end

#unapplicable_value?(filter) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/datagrid/filters/dynamic_filter.rb', line 37

def unapplicable_value?(filter)
  super(filter&.value)
end