Class: Datagrid::Filters::BaseFilter

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

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.



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

def initialize(grid_class, name, **options, &block)
  self.grid_class = grid_class
  self.name = name.to_sym
  self.options = Datagrid::Utils.callable(grid_class.default_filter_options, self).merge(options)
  self.block = block
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



13
14
15
# File 'lib/datagrid/filters/base_filter.rb', line 13

def block
  @block
end

#grid_classObject

Returns the value of attribute grid_class.



13
14
15
# File 'lib/datagrid/filters/base_filter.rb', line 13

def grid_class
  @grid_class
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/datagrid/filters/base_filter.rb', line 13

def name
  @name
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/datagrid/filters/base_filter.rb', line 13

def options
  @options
end

Class Method Details

.form_builder_helper_nameObject



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

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

Instance Method Details

#allow_blank?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/datagrid/filters/base_filter.rb', line 108

def allow_blank?
  options[:allow_blank]
end

#allow_nil?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/datagrid/filters/base_filter.rb', line 104

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

#apply(grid_object, scope, value) ⇒ Object



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

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

  result = execute(value, scope, grid_object)

  return scope unless result

  result = default_filter(value, scope) if result == Datagrid::Filters::DEFAULT_FILTER_BLOCK
  unless grid_object.driver.match?(result)
    raise(
      Datagrid::FilteringError,
      "Filter #{name.inspect} unapplicable: result no longer match #{grid_object.driver.class}.",
    )
  end

  result
end

#default(object) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/datagrid/filters/base_filter.rb', line 85

def default(object)
  default = 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_input_optionsObject



26
27
28
# File 'lib/datagrid/filters/base_filter.rb', line 26

def default_input_options
  { type: "text" }
end

#default_scope?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/datagrid/filters/base_filter.rb', line 155

def default_scope?
  !block
end

#dummy?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/datagrid/filters/base_filter.rb', line 136

def dummy?
  options[:dummy]
end

#enabled?(grid) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/datagrid/filters/base_filter.rb', line 147

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

#enum_checkboxes?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/datagrid/filters/base_filter.rb', line 151

def enum_checkboxes?
  false
end

#form_builder_helper_nameObject



120
121
122
# File 'lib/datagrid/filters/base_filter.rb', line 120

def form_builder_helper_name
  self.class.form_builder_helper_name
end

#format(value) ⇒ Object



132
133
134
# File 'lib/datagrid/filters/base_filter.rb', line 132

def format(value)
  value&.to_s
end

#headerObject



77
78
79
80
81
82
83
# File 'lib/datagrid/filters/base_filter.rb', line 77

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

#input_optionsObject



112
113
114
# File 'lib/datagrid/filters/base_filter.rb', line 112

def input_options
  options[:input_options] || {}
end

#label_optionsObject



116
117
118
# File 'lib/datagrid/filters/base_filter.rb', line 116

def label_options
  options[:label_options] || {}
end

#multiple?Boolean

Returns:

  • (Boolean)


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

def multiple?
  options[:multiple]
end

#parse(value) ⇒ Object

Raises:

  • (NotImplementedError)


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

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

#parse_values(value) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/datagrid/filters/base_filter.rb', line 52

def parse_values(value)
  if multiple?
    return nil if value.nil?

    return normalize_multiple_value(value).map do |v|
      parse(v)
    end
  end

  case value
  when Array
    raise Datagrid::ArgumentError,
      "#{grid_class}##{name} filter can not accept Array argument. Use :multiple option."
  when Range
    raise Datagrid::ArgumentError,
      "#{grid_class}##{name} filter can not accept Range argument. Use :range option."
  else
    parse(value)
  end
end

#range?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/datagrid/filters/base_filter.rb', line 100

def range?
  false
end

#separatorObject



73
74
75
# File 'lib/datagrid/filters/base_filter.rb', line 73

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

#supports_range?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/datagrid/filters/base_filter.rb', line 128

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

#typeObject



140
141
142
143
144
145
# File 'lib/datagrid/filters/base_filter.rb', line 140

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

#unapplicable_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/datagrid/filters/base_filter.rb', line 30

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