class Ambassador
include Searchable
end
class Speaker
include Searchable
end
module Searchable
extend ActiveSupport::Concern
included do
text :gender # == male || female
end
module ClassMethods
def search_solr(params = {})
solr_search do
with(:gender, params[:gender])
end
end
end
end
GOOD !
but . . . what if . . . Speaker
need to be search by theme too ?
##0. Refacto the search with Sunspot.new_search
module Searchable
module ClassMethods
def search_solr(params = {})
n_search = Sunspot.new_search(self) # self == Ambassador || Speaker
search_by_gender(n_search, params)
search = n_search.execute
end
private
def search_by_gender(n_search, params)
n_search.build do
with(:gender, params[:gender])
end
end
end
end
##1. Searchable
class Speaker
include Searchable
searchable do
text(:theme)
end
# it does not overwrite the module list, it add this list to the module list !
end
##2 Add some specific search ####2.1 Complete the specific model
class Speaker
# [...]
class << self
def search_solr(params = {})
super { |n_search| search_by_theme(n_search, params) }
end
end
end
####2.2 complete the module
module Searchable
module ClassMethods
def search_solr(params = {})
n_search = Sunspot.new_search(self)
search_by_gender(n_search, params)
yield(n_search) if block_given?
search = n_search.execute
end
private
# [...]
def search_by_theme(n_search, params)
n_search.build do
with(:theme, params[:theme])
end
end
end
end
TADAM !!
Resume:
class Ambassador
include Searchable
end
class Speaker
include Searchable
searchable do
text(:theme)
end
class << self
def search_solr(params = {})
super { |n_search| search_by_theme(n_search, params) }
end
end
end
module Searchable
module ClassMethods
def search_solr(params = {})
n_search = Sunspot.new_search(self)
search_by_gender(n_search, params)
yield(n_search) if block_given?
search = n_search.execute
end
private
def search_by_gender(n_search, params)
n_search.build do
with(:gender, params[:gender])
end
end
def search_by_theme(n_search, params)
n_search.build do
with(:theme, params[:theme])
end
end
end
end