Class: Datagrid::Filters::DynamicFilter
Constant Summary
collapse
- AVAILABLE_OPERATIONS =
%w(= =~ >= <=)
Instance Attribute Summary
Attributes inherited from BaseFilter
#block, #grid_class, #name, #options
Instance Method Summary
collapse
#include_blank, #prompt, #select
Methods inherited from BaseFilter
#allow_blank?, #allow_nil?, #apply, #default, #default_filter_block, #dummy?, form_builder_helper_name, #form_builder_helper_name, #format, #header, #multiple, #multiple?, #parse, #separator, #supports_range?, #type
Constructor Details
Returns a new instance of DynamicFilter.
9
10
11
12
13
14
15
16
|
# File 'lib/datagrid/filters/dynamic_filter.rb', line 9
def initialize(*)
super
options[:select] ||= default_select
options[:operations] ||= AVAILABLE_OPERATIONS
unless options.has_key?(:include_blank)
options[:include_blank] = false
end
end
|
Instance Method Details
#default_filter_where(scope, filter) ⇒ Object
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
58
59
60
61
62
63
64
65
|
# File 'lib/datagrid/filters/dynamic_filter.rb', line 29
def default_filter_where(scope, filter)
field, operation, value = filter
date_conversion = value.is_a?(Date) && driver.is_timestamp?(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 '='
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 Datagrid::FilteringError, "Unknown operation: #{operation.inspect}. Use filter block argument to implement operation"
end
end
|
#operations ⇒ Object
67
68
69
|
# File 'lib/datagrid/filters/dynamic_filter.rb', line 67
def operations
options[:operations]
end
|
#operations_select ⇒ Object
71
72
73
74
75
|
# File 'lib/datagrid/filters/dynamic_filter.rb', line 71
def operations_select
operations.map do |operation|
[I18n.t(operation, :scope => "datagrid.filters.dynamic.operations").html_safe, operation]
end
end
|
#parse_values(filter) ⇒ Object
18
19
20
21
22
|
# File 'lib/datagrid/filters/dynamic_filter.rb', line 18
def parse_values(filter)
field, operation, value = filter
[field, operation, type_cast(field, value)]
end
|
#unapplicable_value?(filter) ⇒ Boolean
24
25
26
27
|
# File 'lib/datagrid/filters/dynamic_filter.rb', line 24
def unapplicable_value?(filter)
_, _, value = filter
super(value)
end
|