Created
September 7, 2009 12:08
-
-
Save maxlapshin/182330 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
module ActiveRecordExtensions | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def ids(field = nil) | |
attribute(field ||= "id").map{|id| id.to_i} | |
end | |
def attribute(field = nil) | |
connection.select_values(send(:construct_finder_sql, field ? {:select => field} : {})) | |
end | |
def attributes(fields) | |
connection.send(:select_all, send(:construct_finder_sql, :select => fields)) | |
end | |
def cache_id(prefix = nil) | |
ActionController::RecordIdentifier.dom_class(self, prefix) | |
end | |
end | |
end | |
ActiveRecord::Base.class_eval do | |
include ActiveRecordExtensions | |
def cache_id(prefix = nil) | |
ActionController::RecordIdentifier.dom_id(self, prefix) | |
end | |
named_scope :limit, lambda {|limit| {:limit => limit}} | |
named_scope :order, lambda {|order| {:order => order}} | |
named_scope :except, lambda {|object| | |
klass = Array(object).first && Array(object).first.class.table_name | |
objects = Array(object).compact.map {|object| object.id} | |
objects.blank? ? {} : {:conditions => ["#{klass}.id NOT IN (?)", objects]} | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment