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, type: nil) ⇒ 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_scope_types_for(model) ⇒ Hash{String => Object}
Argument types declared with #add_scope.
-
#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 18 |
# 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] = [] } @custom_scope_types = Hash.new { |h, k| h[k] = {} } end |
Instance Method Details
#add_association(model, association) ⇒ Array<String>
Add a custom ransackable association to a model.
34 35 36 |
# File 'lib/spree/core/ransack_configuration.rb', line 34 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.
25 26 27 |
# File 'lib/spree/core/ransack_configuration.rb', line 25 def add_attribute(model, attribute) @custom_attributes[model.name.to_sym] |= [attribute.to_s] end |
#add_scope(model, scope, type: nil) ⇒ Array<String>
Add a custom ransackable scope to a model.
45 46 47 48 |
# File 'lib/spree/core/ransack_configuration.rb', line 45 def add_scope(model, scope, type: nil) @custom_scope_types[model.name.to_sym][scope.to_s] = type unless type.nil? @custom_scopes[model.name.to_sym] |= [scope.to_s] end |
#custom_associations_for(model) ⇒ Array<String>
Get custom ransackable associations for a model.
62 63 64 |
# File 'lib/spree/core/ransack_configuration.rb', line 62 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.
54 55 56 |
# File 'lib/spree/core/ransack_configuration.rb', line 54 def custom_attributes_for(model) @custom_attributes[model.name.to_sym] end |
#custom_scope_types_for(model) ⇒ Hash{String => Object}
Returns argument types declared with #add_scope.
76 77 78 |
# File 'lib/spree/core/ransack_configuration.rb', line 76 def custom_scope_types_for(model) @custom_scope_types[model.name.to_sym] end |
#custom_scopes_for(model) ⇒ Array<String>
Get custom ransackable scopes for a model.
70 71 72 |
# File 'lib/spree/core/ransack_configuration.rb', line 70 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.
83 84 85 86 87 88 |
# File 'lib/spree/core/ransack_configuration.rb', line 83 def reset! @custom_attributes.clear @custom_associations.clear @custom_scopes.clear @custom_scope_types.clear end |