Skip to content

Instantly share code, notes, and snippets.

@Marchino
Created September 9, 2011 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Marchino/1205876 to your computer and use it in GitHub Desktop.
Save Marchino/1205876 to your computer and use it in GitHub Desktop.
module Mmplus
module Controllers
module Search
extend ActiveSupport::Concern
included do
before_filter :fix_params
def do_search(search_params, classes = nil)
q = search_params
custom_sorting = build_custom_sorting(params[:order_by])
@items = Sunspot.search(classes || MM_ITEMS.collect{|item| item.to_s.classify.constantize}) do
if block_given?
yield self, q
else
MM_ITEMS.each{ |klass| data_accessor_for(klass.to_s.classify.constantize).include= [:bookshops, :item_images] }
keywords(q[:title].gsub('-', ' '), :fields => I18n.available_locales.collect {|locale| "title_#{locale}" }) if q[:title].present? && q[:title_exact_match]!="1"
keywords(q[:author].gsub('-', ' '), :fields => [:author]) if q[:author].present? && q[:author_exact_match]!="1"
keywords q[:keyword].gsub('-', ' ') if q[:keyword].present?
keywords(q[:description].gsub('-', ' '), :fields => I18n.available_locales.collect {|locale| "description_#{locale}" }) if q[:description].present?
keywords(q[:subject].gsub('-', ' '), :fields => [:subjects_it, :subjects_en]) if q[:subject].present?
with(:visible, true)
with(:title_lowercase, q[:title].downcase) if q[:title].present? && q[:title_exact_match]=="1"
with(:author, q[:author].downcase) if q[:author].present? && q[:author_exact_match]=="1"
with(:bookshop_name, q[:bookshop]) if q[:bookshop].present?
with(:category_ids, q[:category_id]) if q[:category_id].present?
with(:publisher, q[:publisher]) if q[:publisher].present?
with(:publishing_year, q[:year]) if q[:year].present?
with(:publishing_year, q[:publishing_year_from]..q[:publishing_year_to]) if q[:publishing_year_from].present? && q[:publishing_year_to].present?
with(:item_type, q[:item_type]) if q[:item_type].present?
with(:has_image, q[:has_image].present?) if q[:has_image].present?
with(:price, PRICE_RANGES[q[:price]]) if q[:price].present?
with(:language, q[:language]) if q[:language].present?
end
facet(:item_type)
facet(:category_ids)
facet(:author)
facet(:publishing_year)
facet(:bookshop_name)
facet(:has_image) do
row('only items with image') do
with(:has_image, true)
end
end
facet(:price) do
PRICE_RANGES.each do |key, value|
row(key) do
with(:price, value)
end
end
end
order_by(custom_sorting[:sort_by], custom_sorting[:order]) unless custom_sorting.nil?
order_by :score, :desc
order_by :has_image, :desc
order_by :id, :desc
paginate(:page => params[:page] || 1, :per_page => 20)
end
@q = q
end
private
def fix_params
params[:search] = {} if params[:search].nil?
if request.params[:field].present?
request.params[:search] = { params[:field].to_sym => params[:value] }
request.params[:field] = request.params[:value] = nil
end
if request.params[:additional_keyword].present?
request.params[:search] = {} if request.params[:search].nil?
request.params[:search][:keyword] = [request.params[:search][:keyword], request.params[:additional_keyword]].compact.join(' ')
request.params[:additional_keyword] = nil
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment