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, :seller_id)
Spree.ransack.add_scope(Spree::Product, :by_seller, type: 'id')
Spree.ransack.add_association(Spree::Product, :seller)

Instance Method Summary collapse

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.

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



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.

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



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.

Parameters:

  • model (Class) —

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

  • scope (String, Symbol) —

    the scope to add

  • type (String, Array<String>, Hash, nil) (defaults to: nil) —

    the arguments it takes, as in ransackable_scope_types — 'boolean' for a scope taking none

Returns:

  • (Array<String>) —

    the updated list of custom scopes



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.

Parameters:

  • model (Class) —

    the model class to query

Returns:

  • (Array<String>) —

    the custom associations



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.

Parameters:

  • model (Class) —

    the model class to query

Returns:

  • (Array<String>) —

    the custom attributes



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.

Parameters:

  • model (Class) —

    the model class to query

Returns:

  • (Hash{String => Object}) —

    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.

Parameters:

  • model (Class) —

    the model class to query

Returns:

  • (Array<String>) —

    the custom scopes



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