Class: Spree::RansackConfiguration

Inherits:
Object
  • Object
show all
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.

Examples:

Adding custom searchable fields

Spree.ransack.add_attribute(Spree::Product, :vendor_id)
Spree.ransack.add_scope(Spree::Product, :by_vendor)
Spree.ransack.add_association(Spree::Product, :vendor)

Instance Method Summary collapse

Constructor Details

#initializeRansackConfiguration

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.

Parameters:

  • model (Class)

    the model class to configure (e.g., Spree::Product)

  • association (String, Symbol)

    the association to add

Returns:

  • (Array<String>)

    the updated list of custom associations



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.

Parameters:

  • model (Class)

    the model class to configure (e.g., Spree::Product)

  • attribute (String, Symbol)

    the attribute to add

Returns:

  • (Array<String>)

    the updated list of custom attributes



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.

Parameters:

  • model (Class)

    the model class to configure (e.g., Spree::Product)

  • scope (String, Symbol)

    the scope to add

Returns:

  • (Array<String>)

    the updated list of custom scopes



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.

Parameters:

  • model (Class)

    the model class to query

Returns:

  • (Array<String>)

    the custom associations



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.

Parameters:

  • model (Class)

    the model class to query

Returns:

  • (Array<String>)

    the custom attributes



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.

Parameters:

  • model (Class)

    the model class to query

Returns:

  • (Array<String>)

    the custom scopes



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