Skip to content

Instantly share code, notes, and snippets.

@Ranger-X
Created April 27, 2012 17:34
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save Ranger-X/2511088 to your computer and use it in GitHub Desktop.
Save Ranger-X/2511088 to your computer and use it in GitHub Desktop.
OptionValue filter
def ProductFilters.option_with_values(option_scope, option, values)
# get values IDs for Option with name {@option} and value-names in {@values} for use in SQL below
option_values = Spree::OptionValue.where(:presentation => [values].flatten).joins(:option_type).where(OptionType.table_name => {:name => option}).pluck("#{OptionValue.table_name}.id")
return option_scope if option_values.empty?
option_scope = option_scope.where("#{Product.table_name}.id in (select product_id from #{Variant.table_name} v left join spree_option_values_variants ov on ov.variant_id = v.id where ov.option_value_id in (?))", option_values)
option_scope
end
# option scope
Spree::Product.scope :option_any,
lambda { |*opts|
option_scope = Spree::Product.includes(:variants_including_master)
opts.map { |opt|
# opt is an array => ['option-name', [value1, value2, value3, ...]]
option_scope = option_with_values(option_scope, *opt)
}
option_scope
}
# size option
def ProductFilters.size_filter
sizes = Spree::OptionValue.where(:option_type_id => Spree::OptionType.find_by_name("briefs-size")).order("position").map(&:presentation).compact.uniq
{
:name => "Size",
:scope => :option_any,
:conds => nil,
:option => 'briefs-size', # this is MANDATORY
:class => "sizes",
:labels => sizes.map { |k| [k, k] }
}
end
# model option
def ProductFilters.model_filter
models = Spree::OptionValue.where(:option_type_id => Spree::OptionType.find_by_name("briefs-model")).order("position").map(&:presentation).compact.uniq
{
:name => "Model",
:scope => :option_any,
:conds => nil,
:option => 'briefs-model', # this is MANDATORY
:class => "model",
:labels => models.map { |k| [k, k] }
}
end
# color option
def ProductFilters.color_filter
colors = Spree::OptionValue.where(:option_type_id => Spree::OptionType.find_by_name("briefs-color")).order("position").map(&:presentation).compact.uniq
{
:name => "Color",
:scope => :option_any,
:conds => nil,
:option => 'briefs-color', # this is MANDATORY
:class => "color",
:labels => colors.map { |k| [k, k] }
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment