2011-12-24 06:02:02 -05:00
|
|
|
require 'active_support/deprecation'
|
|
|
|
|
2011-12-18 16:42:09 -05:00
|
|
|
module ActiveRecord
|
2011-12-24 06:28:03 -05:00
|
|
|
# <tt>ActiveRecord::Model</tt> can be included into a class to add Active Record persistence.
|
|
|
|
# This is an alternative to inheriting from <tt>ActiveRecord::Base</tt>. Example:
|
|
|
|
#
|
|
|
|
# class Post
|
|
|
|
# include ActiveRecord::Model
|
|
|
|
# end
|
|
|
|
#
|
2011-12-18 16:42:09 -05:00
|
|
|
module Model
|
2011-12-28 10:38:16 -05:00
|
|
|
module ClassMethods #:nodoc:
|
|
|
|
include ActiveSupport::Callbacks::ClassMethods
|
|
|
|
include ActiveModel::Naming
|
|
|
|
include QueryCache::ClassMethods
|
|
|
|
include ActiveSupport::Benchmarkable
|
|
|
|
include ActiveSupport::DescendantsTracker
|
|
|
|
|
|
|
|
include Querying
|
|
|
|
include Translation
|
|
|
|
include DynamicMatchers
|
|
|
|
include CounterCache
|
|
|
|
include Explain
|
2011-12-28 13:07:08 -05:00
|
|
|
include ConnectionHandling
|
2011-12-18 18:35:25 -05:00
|
|
|
end
|
|
|
|
|
2011-12-18 16:42:09 -05:00
|
|
|
def self.included(base)
|
2011-12-28 10:38:16 -05:00
|
|
|
return if base.singleton_class < ClassMethods
|
2011-12-23 19:33:05 -05:00
|
|
|
|
2011-12-18 16:42:09 -05:00
|
|
|
base.class_eval do
|
2011-12-28 10:38:16 -05:00
|
|
|
extend ClassMethods
|
|
|
|
Callbacks::Register.setup(self)
|
|
|
|
initialize_generated_modules unless self == Base
|
2011-12-18 16:42:09 -05:00
|
|
|
end
|
|
|
|
end
|
2011-12-24 06:02:02 -05:00
|
|
|
|
2011-12-28 10:38:16 -05:00
|
|
|
extend ActiveModel::Configuration
|
|
|
|
extend ActiveModel::Callbacks
|
|
|
|
extend ActiveModel::MassAssignmentSecurity::ClassMethods
|
|
|
|
extend ActiveModel::AttributeMethods::ClassMethods
|
|
|
|
extend Callbacks::Register
|
|
|
|
extend Explain
|
2011-12-28 13:07:08 -05:00
|
|
|
extend ConnectionHandling
|
2011-12-28 10:38:16 -05:00
|
|
|
|
|
|
|
def self.extend(*modules)
|
|
|
|
ClassMethods.send(:include, *modules)
|
|
|
|
end
|
|
|
|
|
|
|
|
include Persistence
|
|
|
|
include ReadonlyAttributes
|
|
|
|
include ModelSchema
|
|
|
|
include Inheritance
|
|
|
|
include Scoping
|
|
|
|
include Sanitization
|
|
|
|
include Integration
|
|
|
|
include AttributeAssignment
|
|
|
|
include ActiveModel::Conversion
|
|
|
|
include Validations
|
|
|
|
include Locking::Optimistic, Locking::Pessimistic
|
|
|
|
include AttributeMethods
|
|
|
|
include Callbacks, ActiveModel::Observing, Timestamp
|
|
|
|
include Associations
|
|
|
|
include IdentityMap
|
|
|
|
include ActiveModel::SecurePassword
|
|
|
|
include AutosaveAssociation, NestedAttributes
|
|
|
|
include Aggregations, Transactions, Reflection, Serialization, Store
|
|
|
|
include Core
|
|
|
|
|
2011-12-28 13:07:08 -05:00
|
|
|
class << self
|
|
|
|
def arel_engine
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def abstract_class?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def inheritance_column
|
|
|
|
'type'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-24 06:02:02 -05:00
|
|
|
module DeprecationProxy #:nodoc:
|
|
|
|
class << self
|
|
|
|
instance_methods.each { |m| undef_method m unless m =~ /^__|^object_id$|^instance_eval$/ }
|
|
|
|
|
|
|
|
def method_missing(name, *args, &block)
|
|
|
|
if Model.respond_to?(name)
|
|
|
|
Model.send(name, *args, &block)
|
|
|
|
else
|
|
|
|
ActiveSupport::Deprecation.warn(
|
|
|
|
"The object passed to the active_record load hook was previously ActiveRecord::Base " \
|
|
|
|
"(a Class). Now it is ActiveRecord::Model (a Module). You have called `#{name}' which " \
|
|
|
|
"is only defined on ActiveRecord::Base. Please change your code so that it works with " \
|
|
|
|
"a module rather than a class. (Model is included in Base, so anything added to Model " \
|
|
|
|
"will be available on Base as well.)"
|
|
|
|
)
|
|
|
|
Base.send(name, *args, &block)
|
|
|
|
end
|
|
|
|
end
|
2011-12-24 09:19:03 -05:00
|
|
|
|
|
|
|
alias send method_missing
|
2011-12-24 06:02:02 -05:00
|
|
|
end
|
|
|
|
end
|
2011-12-18 16:42:09 -05:00
|
|
|
end
|
|
|
|
|
2011-12-24 07:30:10 -05:00
|
|
|
# Load Base at this point, because the active_record load hook is run in that file.
|
|
|
|
Base
|
|
|
|
end
|