Module: Datagrid::Filters::ClassMethods

Defined in:
lib/datagrid/filters.rb

Overview

Grid class methods related to filters

Instance Method Summary collapse

Instance Method Details

#filter(name, type = :default, **options) {|value, scope, grid| ... } ⇒ Datagrid::Filters::BaseFilter

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

Parameters:

  • name (Symbol)

    filter name

  • type (Symbol) (defaults to: :default)

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

  • options (Hash)

    hash of options

Options Hash (**options):

  • header (String)

    Determines the header of the filter.

  • default (Object, Proc)

    The default filter value. Accepts a Proc to allow dynamic calculation.

  • range (Boolean)

    Whether the filter accepts two values to define a range. Supported by types: :integer, :float, :date, :datetime, and :string.

  • multiple (Boolean, String)

    If true, allows multiple values for the filter. Strings are parsed using a separator (default: ,). Can accept a custom separator. Default: false.

  • allow_nil (Boolean)

    Whether the filter value can be nil. Default: false.

  • allow_blank (Boolean)

    Whether the filter value can be blank. Default: false.

  • before (Symbol)

    Specifies the position of this filter by placing it before another filter. Used with the datagrid_form_for helper.

  • after (Symbol)

    Specifies the position of this filter by placing it after another filter. Used with the datagrid_form_for helper.

  • dummy (Boolean)

    If true, the filter is not applied automatically and is only displayed in the form. Useful for manual application.

  • if (Proc, Symbol)

    Specifies a condition under which the filter is displayed and used. Accepts a block or the name of an instance method.

  • unless (Proc, Symbol)

    Specifies a condition under which the filter is NOT displayed or used. Accepts a block or the name of an instance method.

  • input_options (Hash)

    Options passed to the HTML input tag for rendering attributes. Use input_options[:type] to control the input type (e.g., textarea).

  • label_options (Hash)

    Options passed to the HTML label tag for rendering attributes.

Yields:

  • (value, scope, grid)

    Block to apply the filter.

Yield Parameters:

  • value (Object)

    The value assigned to the filter.

  • scope (Object)

    The current ORM scope being filtered.

  • grid (Datagrid::Base)

    The datagrid instance.

Returns:

Raises:

See Also:



242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/datagrid/filters.rb', line 242

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
  filter
end

#filter_by_name(attribute) ⇒ Datagrid::Filters::BaseFilter?

Returns filter definition object by name.

Returns:



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/datagrid/filters.rb', line 195

def filter_by_name(attribute)
  if attribute.is_a?(Datagrid::Filters::BaseFilter)
    unless ancestors.include?(attribute.grid_class)
      raise ArgumentError, "#{attribute.grid_class}##{attribute.name} filter doen't belong to #{self.class}"
    end

    return attribute
  end
  filters.find do |filter|
    filter.name == attribute.to_sym
  end
end

#filtersArray<Datagrid::Filters::BaseFilter>

Returns all defined filters.

Returns:



267
268
269
# File 'lib/datagrid/filters.rb', line 267

def filters
  filters_array
end