Skip to content

Commit

Permalink
Added default sort field configuration. Moved all configuration over …
Browse files Browse the repository at this point in the history
…to the configuration model.
  • Loading branch information
iloveitaly committed Dec 17, 2012
1 parent 42de821 commit d118a44
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
10 changes: 6 additions & 4 deletions app/models/spree/search/spree_sunspot/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,23 @@ def prepare(params)
# as well as handles pagination
super

conf = Spree::Search::SpreeSunspot.configuration

# TODO should do some parameter cleaning here: only allow valid search params to be passed through
# the faceting partial is kind of 'dumb' about the params object: doesn't clean it out and just
# dumps all the params into the query string

@properties[:query] = params[:keywords]
@properties[:price] = params[:price]

@properties[:sort] = params[:sort].try(:to_sym) || :score
@properties[:order] = params[:order].try(:to_sym) || :desc
@properties[:sort] = params[:sort] || conf.default_sort_key
@properties[:order] = params[:order] || conf.default_sort_order

# ensure that :sort and :order are legit
@properties[:sort] = :score unless Spree::Search::SpreeSunspot.configuration.sort_fields.keys.include? @properties[:sort]
@properties[:sort] = :score unless conf.sort_fields.keys.include? @properties[:sort]
@properties[:order] = :desc unless [:desc, :asc].include? @properties[:sort]

Spree::Search::SpreeSunspot.configuration.display_facets.each do |name|
conf.display_facets.each do |name|
@properties[name] ||= params["#{name}_facet"]
end
end
Expand Down
5 changes: 0 additions & 5 deletions app/models/spree/sunspot_search_configuration.rb

This file was deleted.

5 changes: 3 additions & 2 deletions app/views/spree/products/_facets.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<%
facets_arr = Spree::Search::SpreeSunspot.configuration.display_facets
limit = Spree::SunspotSearch::Config[:facet_display_limit]
conf = Spree::Search::SpreeSunspot.configuration
facets_arr = conf.display_facets
limit = conf.facet_display_limit

if @taxon
display_list = @taxon.leaf? ? [@taxon.id] : @taxon.children.map(&:id)
Expand Down
10 changes: 9 additions & 1 deletion lib/spree/search/spree_sunspot/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ class Configuration
:other_facets,
:show_facets,
:fields,
:sort_fields
:sort_fields,
:default_sort_key,
:default_sort_order,
:facet_display_limit

def initialize
# Price ranges to be used for faceting
Expand Down Expand Up @@ -37,10 +40,15 @@ def initialize
# facets that have already been created and should be displayed in the suggestions partial
self.show_facets = []

self.facet_display_limit = 1

self.sort_fields = {
:score => :desc,
:price => [:asc, :desc],
}

self.default_sort_key = :score
self.default_sort_order = :desc
end

def display_facets
Expand Down
3 changes: 0 additions & 3 deletions lib/spree_sunspot_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
require 'sunspot_rails'
require 'spree/search/spree_sunspot/configuration'

module Spree::SunspotSearch; end

module SpreeSunspotSearch
class Engine < Rails::Engine
engine_name 'spree_sunspot_search'
Expand All @@ -12,7 +10,6 @@ class Engine < Rails::Engine

initializer "spree.sunspot_search.preferences", :after => "spree.environment" do |app|
Spree::Config.searcher_class = Spree::Search::SpreeSunspot::Search
Spree::SunspotSearch::Config = Spree::SunspotSearchConfiguration.new
end

def self.activate
Expand Down

0 comments on commit d118a44

Please sign in to comment.