Class: SolidusAdmin::UI::Table::RansackFilter::Component
- Inherits:
-
BaseComponent
- Object
- BaseComponent
- SolidusAdmin::UI::Table::RansackFilter::Component
- Defined in:
- app/components/solidus_admin/ui/table/ransack_filter/component.rb
Defined Under Namespace
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
- #before_render ⇒ Object
-
#build(type, value, opt_index = nil) ⇒ FormAttribute
Builds form attributes for filter options.
-
#checked?(value) ⇒ Boolean
Determines if a given value should be checked based on the params.
-
#initialize(presentation:, combinator:, attribute:, predicate:, options:, form:, index:, search_param: :q) ⇒ Component
constructor
A new instance of Component.
Constructor Details
#initialize(presentation:, combinator:, attribute:, predicate:, options:, form:, index:, search_param: :q) ⇒ Component
Returns a new instance of Component.
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 = @form = form @index = index end |
Instance Method Details
#before_render ⇒ Object
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.
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.
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 |