Created
March 29, 2010 16:48
-
-
Save nordringrayhide/348083 to your computer and use it in GitHub Desktop.
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
http://github.com/cthulhu/by_whatever | |
module ByWhatever | |
def self.included(base) | |
base.send :extend, ClassMethods | |
end | |
module ClassMethods | |
def by_whatever(*args) | |
options = args.extract_options! | |
columns = args.blank? ? self.column_names : args.to_a.map(&:to_s).uniq! | |
columns -= options[:except].to_a.map(&:to_s) | |
%w(minute hour day week month).each do |range| | |
self.class_eval do | |
named_scope "during_last_#{range}", lambda{{:conditions => ['created_at >= ?', Time.now.utc-1.send(range)]}} | |
end | |
end | |
columns.each do |column| | |
self.class_eval do | |
named_scope "by_#{column}", lambda {|value| {:conditions => ["#{column} = ?", value]}} | |
named_scope "by_#{column.pluralize}", lambda {|value| value.blank? || !value.is_a?(Array) ? [] : {:conditions => ["#{column} in (?)", value]} } | |
end | |
end | |
end | |
end | |
end | |
class Bid < ActiveRecord::Base | |
include ByWhatever | |
by_whatever :except => :id | |
... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment