Module: Datagrid::Filters::ClassMethods

Defined in:
lib/datagrid/filters.rb

Overview

self.included

Instance Method Summary collapse

Instance Method Details

#filter(name, type = :default, options = {}, &block) ⇒ Object

Defines new datagrid filter. This method automatically generates attr_accessor for filter name and adds it to the list of datagrid attributes.

Arguments:

  • name - filter name

  • type - filter type that defines type case and GUI representation of a filter

  • options - hash of options

  • block - proc to apply the filter

Available options:

  • :header - determines the header of the filter

  • :default - the default filter value. Able to accept a Proc in case default should be recalculated

  • :multiple - if true multiple values can be assigned to this filter. By default multiple values are parsed from string using ‘,` separator. But you can specify a different separator as option value. Default: false.

  • :allow_nil - determines if the value can be nil

  • :allow_blank - determines if the value can be blank

  • :before - determines the position of this filter, by adding it before the filter passed here (when using datagrid_form_for helper)

  • :after - determines the position of this filter, by adding it after the filter passed here (when using datagrid_form_for helper)

  • :dummy - if true, this filter will not be applied automatically and will be just displayed in form. In case you may want to apply it manually.

See: github.com/bogdan/datagrid/wiki/Filters for examples

Raises:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/datagrid/filters.rb', line 85

def filter(name, type = :default, options = {}, &block)
  if type.is_a?(Hash)
    options = type
    type = :default
  end

  klass = type.is_a?(Class) ? type : FILTER_TYPES[type]
  raise ConfigurationError, "filter class #{type.inspect} not found" unless klass

  position = Datagrid::Utils.extract_position_from_options(self.filters, options)
  filter = klass.new(self, name, options, &block)
  self.filters.insert(position, filter)

  datagrid_attribute(name) do |value|
    filter.parse_values(value)
  end

end

#filter_by_name(attribute) ⇒ Object

Returns filter definition object by name



50
51
52
53
54
55
# File 'lib/datagrid/filters.rb', line 50

def filter_by_name(attribute)
  return attribute if attribute.is_a?(Datagrid::Filters::BaseFilter)
  self.filters.find do |filter|
    filter.name.to_sym == attribute.to_sym
  end
end