module Datagrid::Filters::ClassMethods

Public Instance Methods

default_filter() click to toggle source
# File lib/datagrid/filters.rb, line 112
def default_filter
  DEFAULT_FILTER_BLOCK
end
filter(name, type = :default, **options, &block) click to toggle source

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. Use input_options.type to control input type including textarea.

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

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

# File lib/datagrid/filters.rb, line 99
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) click to toggle source

Returns filter definition object by name

# File lib/datagrid/filters.rb, line 49
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
filters() click to toggle source
# File lib/datagrid/filters.rb, line 120
def filters
  filters_array
end
inspect() click to toggle source
# File lib/datagrid/filters.rb, line 116
def inspect
  "#{super}(#{filters_inspection})"
end

Protected Instance Methods

filters_inspection() click to toggle source
# File lib/datagrid/filters.rb, line 131
def filters_inspection
  return "no filters" if filters.empty?
  filters.map do |filter|
    "#{filter.name}: #{filter.type}"
  end.join(", ")
end
inherited(child_class) click to toggle source
Calls superclass method
# File lib/datagrid/filters.rb, line 126
def inherited(child_class)
  super(child_class)
  child_class.filters_array = self.filters_array.clone
end