Created
October 20, 2010 15:43
-
-
Save a2ikm/636659 to your computer and use it in GitHub Desktop.
generate named_scopes for each column
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
module Searchable | |
def search(params={}) | |
return self.scoped if params.nil? | |
params.reject{|k,v|v.blank?}.to_a.inject(self) do |base,param| | |
base.send(param.first, param.last) | |
end | |
end | |
def self.extended(base) | |
base.column_names.each do |column_name| | |
base.class_eval %Q{ | |
named_scope :#{column_name}, lambda { |val| { | |
:conditions => { :#{column_name} => val } | |
}} | |
} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment