Permalink
Browse files

Added ability to configure the list of indexable fields and facets.

  • Loading branch information...
1 parent 2dd36b8 commit 3c578ba58d99876e5c389baefc47957977838f9b @romul committed Feb 27, 2011
@@ -1,9 +1,5 @@
Product.class_eval do
- acts_as_solr :fields => [:name, :description, :is_active, {:price => :float},
- :taxon_ids, :price_range, :taxon_names, :store_ids,
- :brand_property, :color_option, :size_option],
- :facets=>[:price_range, :taxon_names,
- :brand_property, :color_option, :size_option]
+ acts_as_solr :fields => PRODUCT_SOLR_FIELDS, :facets => PRODUCT_SOLR_FACETS
def taxon_ids
taxons.map(&:id)
@@ -30,19 +26,12 @@ def taxon_names
end
def price_range
- price_ranges = YAML.load(Spree::Config[:product_price_ranges])
- case price
- when 0..25
- price_ranges[0]
- when 25..50
- price_ranges[1]
- when 50..100
- price_ranges[2]
- when 100..200
- price_ranges[3]
- else
- price_ranges[4]
+ max = 0
+ PRODUCT_PRICE_RANGES.each do |range, name|
+ return name if range.include?(price)
+ max = range.max if range.max > max
end
+ I18n.t(:price_and_above, :price => max)
end
def brand_property
@@ -1,9 +1,13 @@
-<%
+<%
facets = @searcher.facets || []
taxon_names = @taxon ? @taxon.self_and_descendants.map(&:name) : []
for facet in facets
options = facet.options
- options = options.sort{|x,y| y.count <=> x.count} unless facet.name == "price_range"
+ if facet.name == "price_range"
+ options = options.sort{|x, y| x.name <=> y.name}
+ else
+ options = options.sort{|x, y| y.count <=> x.count}
+ end
unless options.empty? %>
<h4><%= t "#{facet.name}_facet" %></h4>
<ul><%
@@ -0,0 +1,13 @@
+unless defined?(PRODUCT_PRICE_RANGES)
+ PRODUCT_PRICE_RANGES = {0..25 => " Under $25", 25..50 => " $25 to $50",
+ 50..100 => " $50 to $100", 100..200 => "$100 to $200"}
+end
+unless defined?(PRODUCT_SOLR_FIELDS)
+ PRODUCT_SOLR_FIELDS = [:name, :description, :is_active, {:price => :float},
+ :taxon_ids, :price_range, :taxon_names, :store_ids,
+ :brand_property, :color_option, :size_option]
+end
+unless defined?(PRODUCT_SOLR_FACETS)
+ PRODUCT_SOLR_FACETS = [:price_range, :taxon_names,
+ :brand_property, :color_option, :size_option]
+end
View
@@ -4,3 +4,4 @@ en:
brand_property_facet: "Brand"
color_option_facet: "Color"
size_option_facet: "Size"
+ price_and_above: "$%{price} and above"
View
@@ -4,3 +4,4 @@ ru:
brand_property_facet: "Бренд"
color_option_facet: "Цвет"
size_option_facet: "Размер"
+ price_and_above: "%{price} руб. и выше"
View
@@ -4,7 +4,7 @@ class Solr < defined?(Spree::Search::MultiDomain) ? Spree::Search::MultiDomain :
def get_products_conditions_for(base_scope, query)
facets = {
- :fields => [:price_range, :taxon_names, :brand_property, :color_option, :size_option],
+ :fields => PRODUCT_SOLR_FACETS,
:browse => @properties[:facets_hash].map{|k,v| "#{k}:#{v}"},
:zeros => false
}
@@ -50,20 +50,12 @@ def prepare(params)
def parse_facets_hash(facets_hash = {"facet_fields" => {}})
facets = []
- price_ranges = YAML::load(Spree::Config[:product_price_ranges])
facets_hash["facet_fields"].each do |name, options|
options = Hash[*options.flatten] if options.is_a?(Array)
next if options.size <= 1
facet = Facet.new(name.sub('_facet', ''))
- if name == 'price_range_facet'
- price_ranges.each do |price_range|
- count = options[price_range]
- facet.options << FacetOption.new(price_range, count) if count
- end
- else
- options.each do |value, count|
- facet.options << FacetOption.new(value, count)
- end
+ options.each do |value, count|
+ facet.options << FacetOption.new(value, count)
end
facets << facet
end
View
@@ -9,8 +9,6 @@ def self.activate
if Spree::Config.instance
Spree::Config.searcher_class = Spree::Search::Solr
- Spree::Config.set(:product_price_ranges =>
- ["Under $25", "$25 to $50", "$50 to $100", "$100 to $200", "$200 and above"])
end
Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|

0 comments on commit 3c578ba

Please sign in to comment.