Last active
December 16, 2015 08:39
-
-
Save kenton/5407598 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Spree::Sunspot::Setup.configure do | |
searchable auto_index: true, | |
auto_remove: true, | |
include: [:variants], | |
if: ->(prod) { prod.in_active_flash_sale_now? } do | |
text :name, boost: 2.0 | |
text :description, boost: 1.2 | |
text :taxon_labels, boost: 4.0 do | |
taxons.collect{ |t| t.self_and_ancestors.map(&:name) }.flatten | |
end | |
text :clothing_size_labels do | |
option_names_for_types("clothing-size") | |
end | |
text :clothing_size_presentation do | |
option_values.select{ |ov| ["clothing-size"].include?(ov.option_type.name) }.map(&:presentation) | |
end | |
# have to gsub this to remove the hyphens because solr | |
# doesn't tokenize text that includes numbers the way we'd want it to | |
# e.g., "shoes-size-8" tokenizes to [shoes, size-8] not [shoes, size, 8] | |
text :shoe_size_labels do | |
shoe_size_names = option_names_for_types("shoes-size") | |
shoe_size_names.map { |name| name.gsub("-", " ") } | |
end | |
text :color_labels do | |
option_names_for_types("color") | |
end | |
time :available_on | |
integer :taxon_ids, references: Spree::Taxon, multiple: true do | |
taxons.collect{ |t| t.self_and_ancestors.map(&:id) }.flatten | |
end | |
string :taxon_names, multiple: true do | |
taxons.collect{ |t| t.self_and_ancestors.map(&:name) }.flatten | |
end | |
integer :position, references: FlashSaleVariant, multiple: true do | |
variants.collect do |variant| | |
variant.flash_sale_variants.map(&:position) | |
end.flatten | |
end | |
integer :variant_ids, references: Spree::Variant, multiple: true do | |
variants.map(&:id).flatten | |
end | |
integer :flash_sale_ids, references: FlashSale, multiple: true do | |
variants.collect { |variant| variant.flash_sales.map(&:id) }.flatten | |
end | |
time :flash_sale_starts_at, multiple: true do | |
variants.collect { |variant| variant.flash_sales.map(&:starts_at) }.flatten | |
end | |
time :flash_sale_ends_at, multiple: true do | |
variants.collect { |variant| variant.flash_sales.map(&:ends_at) }.flatten | |
end | |
integer :designer_id, references: Designer do | |
designer.id if designer | |
end | |
string :designer_name do | |
designer.name if designer | |
end | |
integer :size, references: Spree::OptionValue, multiple: true do | |
option_ids_for_types(["clothing-size","shoes-size"]) | |
end | |
integer :color, references: Spree::OptionValue, multiple: true do | |
option_ids_for_types(["color"]) | |
end | |
integer :clothing_size, references: Spree::OptionValue, multiple: true do | |
option_ids_for_types(["clothing-size"]) | |
end | |
integer :shoes_size, references: Spree::OptionValue, multiple: true do | |
option_ids_for_types(["shoes-size"]) | |
end | |
integer :condition, references: Spree::OptionValue, multiple: true do | |
option_ids_for_types(["condition"]) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# other code above here | |
........ | |
private | |
def option_ids_for_types(type_array) | |
return [] if type_array.nil? || type_array.empty? | |
variants.includes(:option_values).collect do |variant| | |
variant.option_values.select do |ov| | |
type_array.include?(ov.option_type.name) | |
end.collect{ |ov| ov.id } | |
end.flatten | |
end | |
def option_names_for_types(type_array) | |
return [] if type_array.nil? || type_array.empty? | |
variants.includes(:option_values).collect do |variant| | |
variant.option_values.select do |ov| | |
type_array.include?(ov.option_type.name) | |
end.collect{ |ov| ov.name } | |
end.flatten | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment