Class: Spree::RansackConfiguration
- Inherits:
-
Object
- Object
- Spree::RansackConfiguration
- Defined in:
- lib/spree/core/ransack_configuration.rb
Overview
Centralized configuration for Ransack searchable attributes, associations, and scopes.
This class allows developers to extend Spree models with custom ransackable configurations without using decorators.
Instance Method Summary collapse
-
#add_association(model, association) ⇒ Array<String>
Add a custom ransackable association to a model.
-
#add_attribute(model, attribute) ⇒ Array<String>
Add a custom ransackable attribute to a model.
-
#add_scope(model, scope) ⇒ Array<String>
Add a custom ransackable scope to a model.
-
#custom_associations_for(model) ⇒ Array<String>
Get custom ransackable associations for a model.
-
#custom_attributes_for(model) ⇒ Array<String>
Get custom ransackable attributes for a model.
-
#custom_scopes_for(model) ⇒ Array<String>
Get custom ransackable scopes for a model.
-
#initialize ⇒ RansackConfiguration
constructor
A new instance of RansackConfiguration.
-
#reset! ⇒ void
Reset all custom configurations.
Constructor Details
#initialize ⇒ RansackConfiguration
Returns a new instance of RansackConfiguration.
13 14 15 16 17 |
# File 'lib/spree/core/ransack_configuration.rb', line 13 def initialize @custom_attributes = Hash.new { |h, k| h[k] = [] } @custom_associations = Hash.new { |h, k| h[k] = [] } @custom_scopes = Hash.new { |h, k| h[k] = [] } end |
Instance Method Details
#add_association(model, association) ⇒ Array<String>
Add a custom ransackable association to a model.
33 34 35 |
# File 'lib/spree/core/ransack_configuration.rb', line 33 def add_association(model, association) @custom_associations[model.name.to_sym] |= [association.to_s] end |
#add_attribute(model, attribute) ⇒ Array<String>
Add a custom ransackable attribute to a model.
24 25 26 |
# File 'lib/spree/core/ransack_configuration.rb', line 24 def add_attribute(model, attribute) @custom_attributes[model.name.to_sym] |= [attribute.to_s] end |
#add_scope(model, scope) ⇒ Array<String>
Add a custom ransackable scope to a model.
42 43 44 |
# File 'lib/spree/core/ransack_configuration.rb', line 42 def add_scope(model, scope) @custom_scopes[model.name.to_sym] |= [scope.to_s] end |
#custom_associations_for(model) ⇒ Array<String>
Get custom ransackable associations for a model.
58 59 60 |
# File 'lib/spree/core/ransack_configuration.rb', line 58 def custom_associations_for(model) @custom_associations[model.name.to_sym] end |
#custom_attributes_for(model) ⇒ Array<String>
Get custom ransackable attributes for a model.
50 51 52 |
# File 'lib/spree/core/ransack_configuration.rb', line 50 def custom_attributes_for(model) @custom_attributes[model.name.to_sym] end |
#custom_scopes_for(model) ⇒ Array<String>
Get custom ransackable scopes for a model.
66 67 68 |
# File 'lib/spree/core/ransack_configuration.rb', line 66 def custom_scopes_for(model) @custom_scopes[model.name.to_sym] end |
#reset! ⇒ void
This method returns an undefined value.
Reset all custom configurations. Useful for testing.
73 74 75 76 77 |
# File 'lib/spree/core/ransack_configuration.rb', line 73 def reset! @custom_attributes.clear @custom_associations.clear @custom_scopes.clear end |