module Datagrid::Filters::ClassMethods
Grid class methods related to filters
Public Instance Methods
Source
# File lib/datagrid/filters.rb, line 257 def default_filter DEFAULT_FILTER_BLOCK end
@!visibility private
Source
# 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
Defines new datagrid filter. This method automatically generates attr_accessor
for filter name and adds it to the list of datagrid attributes.
@param [Symbol] name filter name @param [Symbol] type filter type that defines type case and GUI representation of a filter @param [Hash] options hash of options @yield [value, scope, grid] Block to apply the filter. @yieldparam [Object] value The value assigned to the filter. @yieldparam [Object] scope The current ORM scope being filtered. @yieldparam [Datagrid::Base] grid The datagrid instance. @return [Datagrid::Filters::BaseFilter] Filter definition object @option options [String] header Determines the header of the filter. @option options [Object, Proc] default The default filter value. Accepts a ‘Proc` to allow dynamic calculation. @option options [Boolean] range Whether the filter accepts two values to define a range.
Supported by types: `:integer`, `:float`, `:date`, `:datetime`, and `:string`.
@option options [Boolean, String] multiple If true, allows multiple values for the filter.
Strings are parsed using a separator (default: `,`). Can accept a custom separator. Default: `false`.
@option options [Boolean] allow_nil Whether the filter value can be ‘nil`. Default: `false`. @option options [Boolean] allow_blank Whether the filter value can be blank. Default: `false`. @option options [Symbol] before Specifies the position of this filter by placing it before another filter.
Used with the `datagrid_form_for` helper.
@option options [Symbol] after Specifies the position of this filter by placing it after another filter.
Used with the `datagrid_form_for` helper.
@option options [Boolean] dummy If true, the filter is not applied automatically and
is only displayed in the form. Useful for manual application.
@option options [Proc, Symbol] if Specifies a condition under which the filter is displayed and used.
Accepts a block or the name of an instance method.
@option options [Proc, Symbol] unless Specifies a condition under which the filter is NOT displayed or used.
Accepts a block or the name of an instance method.
@option options [Hash] input_options Options passed to the HTML input tag for rendering attributes.
Use `input_options[:type]` to control the input type (e.g., `textarea`).
@option options [Hash] label_options Options passed to the HTML label tag for rendering attributes. @see Datagrid::Filters
Source
# 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
@return [Datagrid::Filters::BaseFilter, nil] filter definition object by name
Source
# File lib/datagrid/filters.rb, line 267 def filters filters_array end
@return [Array<Datagrid::Filters::BaseFilter>] all defined filters
Source
# File lib/datagrid/filters.rb, line 262 def inspect "#{super}(#{filters_inspection})" end
@!visibility private
Protected Instance Methods
Source
# File lib/datagrid/filters.rb, line 278 def filters_inspection return "no filters" if filters.empty? filters.map do |filter| "#{filter.name}: #{filter.type}" end.join(", ") end
Source
# File lib/datagrid/filters.rb, line 273 def inherited(child_class) super child_class.filters_array = filters_array.clone end