Class: Datagrid::Filters::DynamicFilter
- Inherits:
-
BaseFilter
- Object
- BaseFilter
- Datagrid::Filters::DynamicFilter
- Includes:
- SelectOptions
- Defined in:
- lib/datagrid/filters/dynamic_filter.rb
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, ]
- AVAILABLE_OPERATIONS =
%w(= =~ >= <=)
Instance Method Summary collapse
- #default_filter_where(scope, filter) ⇒ Object
-
#initialize ⇒ DynamicFilter
constructor
A new instance of DynamicFilter.
- #operations ⇒ Object
- #operations_select ⇒ Object
- #parse_values(filter) ⇒ Object
- #unapplicable_value?(filter) ⇒ Boolean
Methods included from SelectOptions
#include_blank, #prompt, #select, #select_values
Constructor Details
#initialize ⇒ DynamicFilter
Returns a new instance of DynamicFilter.
19 20 21 22 23 24 25 26 |
# File 'lib/datagrid/filters/dynamic_filter.rb', line 19 def initialize(*) super [:select] ||= default_select [:operations] ||= DEFAULT_OPERATIONS unless .has_key?(:include_blank) [:include_blank] = false end end |
Instance Method Details
#default_filter_where(scope, filter) ⇒ Object
39 40 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 39 def default_filter_where(scope, filter) field, operation, value = filter date_conversion = value.is_a?(Date) && driver.(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 if date_conversion value = Datagrid::Utils.(value) end driver.where(scope, field, value) when LIKE_OPERATION if column_type(field) == :string driver.contains(scope, field, value) else if date_conversion value = Datagrid::Utils.(value) end driver.where(scope, field, value) end when MORE_EQUAL_OPERATION if date_conversion value = value.beginning_of_day end driver.greater_equal(scope, field, value) when LESS_EQUAL_OPERATION if date_conversion value = value.end_of_day end driver.less_equal(scope, field, value) else raise Datagrid::FilteringError, "Unknown operation: #{operation.inspect}. Use filter block argument to implement operation" end end |
#operations ⇒ Object
77 78 79 |
# File 'lib/datagrid/filters/dynamic_filter.rb', line 77 def operations [:operations] end |
#operations_select ⇒ Object
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").html_safe, operation] end end |
#parse_values(filter) ⇒ Object
28 29 30 31 32 |
# File 'lib/datagrid/filters/dynamic_filter.rb', line 28 def parse_values(filter) field, operation, value = filter [field, operation, type_cast(field, value)] end |
#unapplicable_value?(filter) ⇒ Boolean
34 35 36 37 |
# File 'lib/datagrid/filters/dynamic_filter.rb', line 34 def unapplicable_value?(filter) _, _, value = filter super(value) end |