Class: SolidusAdmin::UI::Table::RansackFilter::Component

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/solidus_admin/ui/table/ransack_filter/component.rb

Defined Under Namespace

Classes: Attribute, Selection

Constant Summary collapse

SUFFIXES =
{
  combinator: '[m]',
  attribute: '[c][%<index>s][a][]',
  predicate: '[c][%<index>s][p]',
  option: '[c][%<index>s][v][]'
}

Instance Method Summary collapse

Constructor Details

#initialize(presentation:, combinator:, attribute:, predicate:, options:, form:, index:, search_param: :q) ⇒ Component

Returns a new instance of Component.

Parameters:

  • presentation (String)

    The label for the filter.

  • search_param (String) (defaults to: :q)

    The search parameter for the filter query.

  • combinator (String)

    The combining logic for filter options.

  • attribute (String)

    The database attribute the filter is based on.

  • predicate (String)

    The comparison logic for the filter (e.g., “eq” for equals).

  • options (Proc)

    A callable that returns filter options.

  • index (Integer)

    The index of the filter.

  • form (String)

    The form in which the filter resides.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/components/solidus_admin/ui/table/ransack_filter/component.rb', line 12

def initialize(
  presentation:,
  combinator:, attribute:, predicate:, options:, form:, index:, search_param: :q
)
  @presentation = presentation
  @group = "#{search_param}[g][#{index}]"
  @combinator = build(:combinator, combinator)
  @attribute = attribute
  @predicate = predicate
  @options = options
  @form = form
  @index = index
end

Instance Method Details

#before_renderObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/components/solidus_admin/ui/table/ransack_filter/component.rb', line 26

def before_render
  @selections = @options.map.with_index do |(label, value), opt_index|
    Selection.new(
      "#{stimulus_id}--#{label}-#{value}".parameterize,
      label,
      build(:attribute, @attribute, opt_index),
      build(:predicate, @predicate, opt_index),
      build(:option, value, opt_index),
      checked?(value)
    )
  end
end

#build(type, value, opt_index = nil) ⇒ FormAttribute

Builds form attributes for filter options.

Parameters:

  • type (Symbol)

    The type of the form attribute.

  • value (String)

    The value of the form attribute.

  • opt_index (Integer) (defaults to: nil)

    The index of the option, if applicable.

Returns:

  • (FormAttribute)

    The built form attribute.



45
46
47
48
# File 'app/components/solidus_admin/ui/table/ransack_filter/component.rb', line 45

def build(type, value, opt_index = nil)
  suffix = SUFFIXES[type] % { index: opt_index || @index }
  Attribute.new("#{@group}#{suffix}", value)
end

#checked?(value) ⇒ Boolean

Determines if a given value should be checked based on the params.

Parameters:

  • value (String)

    The value of the checkbox.

Returns:

  • (Boolean)

    Returns true if the checkbox should be checked, false otherwise.



54
55
56
57
# File 'app/components/solidus_admin/ui/table/ransack_filter/component.rb', line 54

def checked?(value)
  conditions = params.dig(:q, :g, @index.to_s, :c)
  conditions && conditions.values.any? { |c| c[:v]&.include?(value.to_s) }
end