Permalink
Browse files

Implemented more flexible configuration process

  • Loading branch information...
1 parent 19163b3 commit 8256c665a255cca7cec4692c05506b74c5150553 @iloveitaly committed May 8, 2012
View
14 app/models/product_decorator.rb
@@ -2,12 +2,14 @@
searchable do
boolean :is_active, :using => :is_active?
- PRODUCT_FIELDS.each do |field|
+ conf = Spree::Search::SpreeSunspot.configuration
+
+ conf.fields.each do |field|
if field.class == Hash
field = { :opts => {} }.merge field
send field[:type], field[:name], field[:opts]
else
- send :text, field
+ text(field)
end
end
@@ -23,19 +25,19 @@
taxons.map(&:name)
end
- PRODUCT_OPTION_FACETS.each do |option|
+ conf.option_facets.each do |option|
string "#{option}_facet", :multiple => true do
get_option_values(option.to_s).map(&:presentation)
end
end
- PRODUCT_PROPERTY_FACETS.each do |prop|
+ conf.property_facets.each do |prop|
string "#{prop}_facet", :multiple => true do
property(prop.to_s)
end
end
- PRODUCT_OTHER_FACETS.each do |method|
+ conf.other_facets.each do |method|
string "#{method}_facet", :multiple => true do
send(method)
end
@@ -57,7 +59,7 @@ def is_active?
def price_range
max = 0
- PRODUCT_PRICE_RANGES.each do |range, name|
+ Spree::Search::SpreeSunspot.configuration.price_ranges.each do |range, name|
return name if range.include?(price)
max = range.max if range.max > max
end
View
2 app/overrides/add_search_pagination.rb
@@ -2,4 +2,4 @@
:name => "add_sunspot_search_pagination",
:replace => "code[erb-silent]:contains('if paginated_products.respond_to')",
:closing_selector => "code[erb-silent]:contains('end')",
- :text => "<%= paginate @searcher.sunspot.hits unless params.has_key? 'taxon' %>")
+ :text => "<%= paginate @searcher.sunspot.hits %>")
View
5 app/views/spree/products/_facets.html.erb
@@ -1,13 +1,14 @@
<%
-facets_arr = PRODUCT_OPTION_FACETS + PRODUCT_PROPERTY_FACETS + PRODUCT_OTHER_FACETS + PRODUCT_SHOW_FACETS
+facets_arr = Spree::Search::SpreeSunspot.configuration.display_facets
limit = Spree::SunspotSearch::Config[:facet_display_limit]
+
facets_arr.each do |f|
%>
<% unless @searcher.sunspot.facet("#{f}_facet").rows.empty? %>
<h6><%= t "#{f}_facet" %></h6>
<ul>
<% @searcher.sunspot.facet("#{f}_facet").rows.slice(0..limit).each do |row| %>
- <li><%= link_to(row.value, params.merge("#{f}_facet" => row.value)) %> (<%= row.count %>)</li>
+ <li><%= link_to(row.value, params.merge("#{f}_facet" => row.value, "page" => nil)) %> (<%= row.count %>)</li>
<% end %>
</ul>
<% end %>
View
4 app/views/spree/products/_sort_bar.html.erb
@@ -1,8 +1,8 @@
<%
-if !params[:keywords].blank?
+unless params.keys.detect { |k| k != 'controller' and k != 'action' }.nil?
# hate to throw this logic here (messy)
# I think it would be worse to create a one-time-use helper method
- options = PRODUCT_SORT_FIELDS.map do |key, value|
+ options = Spree::Search::SpreeSunspot.configuration.sort_fields.map do |key, value|
# value is sort direction
value = [value] if !value.is_a? Array
value.map { |sort| [t("sort.#{key}_#{sort}"), url_for(request.params.merge({:sort => key, :order => sort}))] }
View
45 config/initializers/spree_sunspot_search.rb
@@ -1,45 +0,0 @@
-# Price ranges to be used for faceting
-# gets turned into a Range object for searching (MUST specify with a dash!!!)
-unless defined?(PRODUCT_PRICE_RANGES)
- PRODUCT_PRICE_RANGES = ["0-25", "25-50", "50-100", "100-150"]
-end
-
-# Product Options for use with Faceting
-# gets turned to #{value}_option for the facet
-unless defined?(PRODUCT_OPTION_FACETS)
- PRODUCT_OPTION_FACETS = [:color, :size]
-end
-
-# Product Properties for use with Faceting
-# gets turned to #{value}_property for the facet
-unless defined?(PRODUCT_PROPERTY_FACETS)
- # product properties retrieved using the new Spree::Product#property
- PRODUCT_PROPERTY_FACETS = [:brand]
-end
-
-unless defined?(PRODUCT_FIELDS)
- PRODUCT_FIELDS = [
- # boost gives the product title a bit of a priority
- { :type => :text, :name => :name, :opts => { :boost => 2.0 } },
- :description,
- { :type => :float, :name => :price }
- ]
-end
-
-unless defined?(PRODUCT_OTHER_FACETS)
- # custom facets defined as methods directly on Spree::Product
- PRODUCT_OTHER_FACETS = [:author_list]
-end
-
-unless defined?(PRODUCT_SHOW_FACETS)
- # facets that have already been created and should be displayed
- # in the suggestions partial
- PRODUCT_SHOW_FACETS = [:taxon_name]
-end
-
-unless defined?(PRODUCT_SORT_FIELDS)
- PRODUCT_SORT_FIELDS = {
- :score => :desc,
- :price => [:asc, :desc],
- }
-end
View
2 lib/generators/spree_sunspot_search/install/install_generator.rb
@@ -1,7 +1,7 @@
module SpreeSunspotSearch
module Generators
class InstallGenerator < Rails::Generators::Base
- source_root File.expand_path('../../../../../config/initializers/', __FILE__)
+ source_root File.expand_path('../../../templates/', __FILE__)
def add_javascripts
append_file "app/assets/javascripts/store/all.js", "//= require store/solr_sort_by\n"
View
11 lib/generators/templates/spree_sunspot_search.rb
@@ -0,0 +1,11 @@
+# Take a look at the Spree::Search::SpreeSunspot::Configuration class for details
+
+# Spree::Search::SpreeSunspot.configure do |conf|
+# conf.price_ranges = []
+# conf.option_facets = []
+# conf.property_facets = []
+# conf.other_facets = []
+# conf.show_facet = []
+# conf.fields = []
+# conf.sort_field = []
+# end
View
54 lib/spree/search/spree_sunspot.rb
@@ -1,54 +0,0 @@
-module Spree
- module Search
- class SpreeSunspot < defined?(Spree::Search::MultiDomain) ? Spree::Search::MultiDomain : Spree::Core::Search::Base
- def retrieve_products
- @properties[:sunspot] = Sunspot.search(::Spree::Product) do
- # This is a little tricky to understand
- # - we are sending the block value as a method
- # - Spree::Search::Base is using method_missing() to return the param values
-
- (PRODUCT_OPTION_FACETS + PRODUCT_PROPERTY_FACETS + PRODUCT_OTHER_FACETS + PRODUCT_SHOW_FACETS).each do |name|
- with("#{name}_facet", send(name)) if send(name)
- facet("#{name}_facet")
- end
-
- with(:price, Range.new(price.split('-').first, price.split('-').last)) if price
- facet(:price) do
- PRODUCT_PRICE_RANGES.each do |range|
- row(range) do
- with(:price, Range.new(range.split('-').first, range.split('-').last))
- end
- end
- end
-
- order_by sort, order
- with(:is_active, true)
- keywords(query)
- paginate(:page => page, :per_page => per_page)
- end
-
- self.sunspot.results
- end
-
- protected
-
- def prepare(params)
- super
- @properties[:query] = params[:keywords]
- @properties[:price] = params[:price]
-
- @properties[:sort] = params[:sort] || :score
- @properties[:order] = params[:order] || :desc
-
- # when taxon navigation is in place, Spree::TaxonsController passes :taxon in params
- @properties[:taxon_name_facet] = params[:taxon] unless params[:taxon].blank?
-
- (PRODUCT_OPTION_FACETS + PRODUCT_PROPERTY_FACETS + PRODUCT_OTHER_FACETS + PRODUCT_SHOW_FACETS).each do |name|
- @properties[name] = params["#{name}_facet"]
- end
-
- end
-
- end
- end
-end
View
65 lib/spree/search/spree_sunspot/configuration.rb
@@ -0,0 +1,65 @@
+module Spree
+ module Search
+ module SpreeSunspot
+ class Configuration
+ attr_accessor :price_ranges,
+ :option_facets,
+ :property_facets,
+ :other_facets,
+ :show_facets,
+ :fields,
+ :sort_fields
+
+ def initialize
+ # Price ranges to be used for faceting
+ # gets turned into a Range object for searching (MUST specify with a dash!!!)
+ self.price_ranges = ["0-25", "25-50", "50-100", "100-150"]
+
+ # Product Options for use with Faceting
+ # gets turned to #{value}_option for the facet
+ self.option_facets = [:color, :size]
+
+ # Product Properties for use with Faceting
+ # gets turned to #{value}_property for the facet
+ # product properties retrieved using the new Spree::Product#property
+ self.property_facets = [:brand]
+
+ self.fields = [
+ # boost gives the product title a bit of a priority
+ { :type => :text, :name => :name, :opts => { :boost => 2.0 } },
+ :description,
+ { :type => :float, :name => :price }
+ ]
+
+ # custom facets defined as methods directly on Spree::Product
+ self.other_facets = []
+
+ # facets that have already been created and should be displayed
+ # in the suggestions partial
+ self.show_facets = [:taxon_name]
+
+ self.sort_fields = {
+ :score => :desc,
+ :price => [:asc, :desc],
+ }
+ end
+
+ def display_facets
+ property_facets + option_facets + other_facets + show_facets
+ end
+
+ end
+
+ class << self
+ attr_accessor :configuration
+ end
+
+ def self.configure
+ self.configuration ||= Spree::Search::SpreeSunspot::Configuration.new
+ yield configuration
+ end
+ end
+ end
+end
+
+Spree::Search::SpreeSunspot.configure {}
View
64 lib/spree/search/spree_sunspot/search.rb
@@ -0,0 +1,64 @@
+module Spree
+ module Search
+ module SpreeSunspot
+
+ class Search < defined?(Spree::Search::MultiDomain) ? Spree::Search::MultiDomain : Spree::Core::Search::Base
+ def retrieve_products
+ @properties[:sunspot] = Sunspot.search(::Spree::Product) do
+ conf = Spree::Search::SpreeSunspot.configuration
+
+ # This is a little tricky to understand
+ # - we are sending the block value as a method
+ # - Spree::Search::Base is using method_missing() to return the param values
+ conf.display_facets.each do |name|
+ with("#{name}_facet", send(name)) if send(name)
+ facet("#{name}_facet")
+ end
+
+ with(:price, Range.new(price.split('-').first, price.split('-').last)) if price
+ facet(:price) do
+ conf.price_ranges.each do |range|
+ row(range) do
+ with(:price, Range.new(range.split('-').first, range.split('-').last))
+ end
+ end
+
+ # TODO add greater than range
+ end
+
+ # TODO should check
+ # with(sort) unless ['price', 'score'].include? sort
+
+ order_by sort.to_sym, order.to_sym
+ with(:is_active, true)
+ keywords(query)
+ paginate(:page => page, :per_page => per_page)
+ end
+
+ self.sunspot.results
+ end
+
+ protected
+
+ def prepare(params)
+ super
+ @properties[:query] = params[:keywords]
+ @properties[:price] = params[:price]
+
+ @properties[:sort] = params[:sort] || :score
+ @properties[:order] = params[:order] || :desc
+
+ # when taxon navigation is in place, Spree::TaxonsController passes :taxon in params
+ @properties[:taxon_name_facet] = params[:taxon] unless params[:taxon].blank?
+
+ conf.display_facets.each do |name|
+ @properties[name] = params["#{name}_facet"]
+ end
+
+ end
+
+ end
+
+ end
+ end
+end
View
3 lib/spree_sunspot_search.rb
@@ -1,5 +1,6 @@
require 'spree_core'
require 'sunspot_rails'
+require 'spree/search/spree_sunspot/configuration'
module Spree::SunspotSearch; end
@@ -10,7 +11,7 @@ class Engine < Rails::Engine
config.autoload_paths += %W(#{config.root}/lib)
initializer "spree.sunspot_search.preferences", :after => "spree.environment" do |app|
- Spree::Config.searcher_class = Spree::Search::SpreeSunspot
+ Spree::Config.searcher_class = Spree::Search::SpreeSunspot::Search
Spree::SunspotSearch::Config = Spree::SunspotSearchConfiguration.new
end

0 comments on commit 8256c66

Please sign in to comment.