Skip to content

Instantly share code, notes, and snippets.

@nickmathis
Created June 3, 2011 21:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nickmathis/dbab58161d9a65cf5f1c to your computer and use it in GitHub Desktop.
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