Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MetaSearch hacks
class ActiveRecord::Base
protected
# This allows us to use scopes via the MetaSearch provided search_methods call
def self.search_with_magic(search_args, *args)
# respond_to doesn't like private/protected methods?
self.scopes.each do |scope|
self.send(:search_methods, scope.first)
end
self.send(:search_without_magic, search_args, *args)
end
# This allows things like Order.id_in(params) to be translated to Order.search(:id_in => params)
def self.method_missing_with_metasearch(method, *args)
Rails.logger.warn("method_missing_with_metasearch: " + method.inspect)
# respond_to doesn't like private/protected methods?
begin
# try to make this work with a call to search
self.search(method => args)
rescue Exception => e
Rails.logger.warn "Rescuing: #{e.message} from #{__FILE__} on #{__LINE__}"
self.send(:method_missing_without_metasearch, method, *args)
end
end
# this alias must be done in the singleton class
class << self
alias_method_chain :search, :magic
alias_method_chain :method_missing, :metasearch
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.