Class: Datagrid::Filters::BaseFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/datagrid/filters/base_filter.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grid_class, name, options = {}, &block) ⇒ BaseFilter

Returns a new instance of BaseFilter.


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

def initialize(grid_class, name, options = {}, &block)
  self.grid_class = grid_class
  self.name = name.to_sym
  self.options = options
  self.block = block || default_filter_block
end

Instance Attribute Details

#blockObject

Returns the value of attribute block


6
7
8
# File 'lib/datagrid/filters/base_filter.rb', line 6

def block
  @block
end

#grid_classObject

Returns the value of attribute grid_class


6
7
8
# File 'lib/datagrid/filters/base_filter.rb', line 6

def grid_class
  @grid_class
end

#nameObject

Returns the value of attribute name


6
7
8
# File 'lib/datagrid/filters/base_filter.rb', line 6

def name
  @name
end

#optionsObject

Returns the value of attribute options


6
7
8
# File 'lib/datagrid/filters/base_filter.rb', line 6

def options
  @options
end

Class Method Details

.form_builder_helper_nameObject


91
92
93
# File 'lib/datagrid/filters/base_filter.rb', line 91

def self.form_builder_helper_name
  :"datagrid_#{self.to_s.demodulize.underscore}"
end

Instance Method Details

#allow_blank?Boolean

Returns:

  • (Boolean)

83
84
85
# File 'lib/datagrid/filters/base_filter.rb', line 83

def allow_blank?
  options[:allow_blank]
end

#allow_nil?Boolean

Returns:

  • (Boolean)

79
80
81
# File 'lib/datagrid/filters/base_filter.rb', line 79

def allow_nil?
  options.has_key?(:allow_nil) ? options[:allow_nil] : options[:allow_blank]
end

#apply(grid_object, scope, value) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/datagrid/filters/base_filter.rb', line 23

def apply(grid_object, scope, value)
  return scope if unapplicable_value?(value)

  result = execute(value, scope, grid_object)

  return scope unless result
  if result.is_a?(Datagrid::Filters::DefaultFilterScope)
    result = default_filter(value, scope, grid_object)
  end
  unless grid_object.driver.match?(result)
    raise Datagrid::FilteringError, "Can not apply #{name.inspect} filter: result #{result.inspect} no longer match #{grid_object.driver.class}."
  end

  result
end

#default(object) ⇒ Object


64
65
66
67
68
69
70
71
72
73
# File 'lib/datagrid/filters/base_filter.rb', line 64

def default(object)
  default = self.options[:default]
  if default.is_a?(Symbol)
    object.send(default)
  elsif default.respond_to?(:call)
    Datagrid::Utils.apply_args(object, &default)
  else
    default
  end
end

#default_filter_blockObject


95
96
97
98
99
100
# File 'lib/datagrid/filters/base_filter.rb', line 95

def default_filter_block
  filter = self
  lambda do |value, scope, grid|
    filter.default_filter(value, scope, grid)
  end
end

#dummy?Boolean

Returns:

  • (Boolean)

110
111
112
# File 'lib/datagrid/filters/base_filter.rb', line 110

def dummy?
  options[:dummy]
end

#enabled?(grid) ⇒ Boolean

Returns:

  • (Boolean)

123
124
125
# File 'lib/datagrid/filters/base_filter.rb', line 123

def enabled?(grid)
  ::Datagrid::Utils.process_availability(grid, options[:if], options[:unless])
end

#form_builder_helper_nameObject


87
88
89
# File 'lib/datagrid/filters/base_filter.rb', line 87

def form_builder_helper_name
  self.class.form_builder_helper_name
end

#format(value) ⇒ Object


106
107
108
# File 'lib/datagrid/filters/base_filter.rb', line 106

def format(value)
  value.nil? ? nil : value.to_s
end

#headerObject


56
57
58
59
60
61
62
# File 'lib/datagrid/filters/base_filter.rb', line 56

def header
  if header = options[:header]
    Datagrid::Utils.callable(header)
  else
    Datagrid::Utils.translate_from_namespace(:filters, grid_class, name)
  end
end

#multiple?Boolean

Returns:

  • (Boolean)

75
76
77
# File 'lib/datagrid/filters/base_filter.rb', line 75

def multiple?
  self.options[:multiple]
end

#parse(value) ⇒ Object

Raises:

  • (NotImplementedError)

15
16
17
# File 'lib/datagrid/filters/base_filter.rb', line 15

def parse(value)
  raise NotImplementedError, "#parse(value) suppose to be overwritten"
end

#parse_values(value) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/datagrid/filters/base_filter.rb', line 39

def parse_values(value)
  if multiple?
    return nil if value.nil?
    normalize_multiple_value(value).map do |v|
      parse(v)
    end
  elsif value.is_a?(Array)
    raise Datagrid::ArgumentError, "#{grid_class}##{name} filter can not accept Array argument. Use :multiple option."
  else
    parse(value)
  end
end

#separatorObject


52
53
54
# File 'lib/datagrid/filters/base_filter.rb', line 52

def separator
  options[:multiple].is_a?(String) ? options[:multiple] : default_separator
end

#supports_range?Boolean

Returns:

  • (Boolean)

102
103
104
# File 'lib/datagrid/filters/base_filter.rb', line 102

def supports_range?
  self.class.ancestors.include?(::Datagrid::Filters::RangedFilter)
end

#typeObject


114
115
116
117
118
119
120
121
# File 'lib/datagrid/filters/base_filter.rb', line 114

def type
  Datagrid::Filters::FILTER_TYPES.each do |type, klass|
    if is_a?(klass)
      return type
    end
  end
  raise "wtf is #{inspect}"
end

#unapplicable_value?(value) ⇒ Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/datagrid/filters/base_filter.rb', line 19

def unapplicable_value?(value)
  value.nil? ? !allow_nil? : value.blank? && !allow_blank?
end