Method: Spree::Variant.has_option
- Defined in:
- app/models/spree/variant/scopes.rb
.has_option(option_type, *option_values) ⇒ Object Also known as: has_options
Returns variants that match a given option value
Example:
product.variants_including_master.has_option(OptionType.find_by(name: ‘shoe-size’), OptionValue.find_by(name: ‘8’))
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/models/spree/variant/scopes.rb', line 16 def has_option(option_type, *option_values) option_types = Spree::OptionType.table_name option_type_conditions = case option_type when OptionType then { "#{option_types}.name" => option_type.name } when String then { "#{option_types}.name" => option_type } else { "#{option_types}.id" => option_type } end relation = joins(option_values: :option_type).where(option_type_conditions) option_values.each do |option_value| option_value_conditions = case option_value when OptionValue then { "#{Spree::OptionValue.table_name}.name" => option_value.name } when String then { "#{Spree::OptionValue.table_name}.name" => option_value } else { "#{Spree::OptionValue.table_name}.id" => option_value } end relation = relation.where(option_value_conditions) end relation end |