Module: Datagrid::Filters::ClassMethods

Defined in:
lib/datagrid/filters.rb

Instance Method Summary collapse

Instance Method Details

#default_filterObject



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

def default_filter
  DEFAULT_FILTER_BLOCK
end

#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

  • :range - if true, filter can accept two values that are treated as a range that will be used for filtering Not all of the filter types support this option. Here are the list of types that do: :integer, :float, :date, :datetime, :string

  • :multiple - if true multiple values can be assigned to this filter. If String is assigned as a filter value, it is parsed from string using a separator symbol (‘,` by default). 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.

  • :if - specify the condition when the filter can be dislayed and used. Accepts a block or a symbol with an instance method name

  • :unless - specify the reverse condition when the filter can be dislayed and used. Accepts a block or a symbol with an instance method name

  • :input_options - options passed when rendering html input tag attributes

  • :label_options - options passed when rendering html label tag attributes

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

Raises:



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/datagrid/filters.rb', line 102

def filter(name, type = :default, **options, &block)
  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(filters_array, options)
  filter = klass.new(self, name, options, &block)
  filters_array.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



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/datagrid/filters.rb', line 53

def filter_by_name(attribute)
  if attribute.is_a?(Datagrid::Filters::BaseFilter)
    unless ancestors.include?(attribute.grid_class)
      raise "#{attribute.grid_class}##{attribute.name} filter doen't belong to #{self.class}"
    end
    return attribute
  end
  self.filters.find do |filter|
    filter.name == attribute.to_sym
  end
end

#filtersObject



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

def filters
  filters_array
end

#inspectObject



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

def inspect
  "#{super}(#{filters_inspection})"
end