2011-12-24 06:02:02 -05:00
|
|
|
require 'active_support/deprecation'
|
2012-05-25 10:58:16 -04:00
|
|
|
require 'active_support/concern'
|
|
|
|
require 'active_support/core_ext/module/delegation'
|
|
|
|
require 'active_support/core_ext/module/attribute_accessors'
|
2011-12-24 06:02:02 -05:00
|
|
|
|
2011-12-18 16:42:09 -05:00
|
|
|
module ActiveRecord
|
2012-05-25 10:58:16 -04:00
|
|
|
module Configuration # :nodoc:
|
|
|
|
# This just abstracts out how we define configuration options in AR. Essentially we
|
|
|
|
# have mattr_accessors on the ActiveRecord:Model constant that define global defaults.
|
|
|
|
# Classes that then use AR get class_attributes defined, which means that when they
|
|
|
|
# are assigned the default will be overridden for that class and subclasses. (Except
|
|
|
|
# when options[:global] == true, in which case there is one global value always.)
|
|
|
|
def config_attribute(name, options = {})
|
|
|
|
if options[:global]
|
|
|
|
class_eval <<-CODE, __FILE__, __LINE__ + 1
|
|
|
|
def self.#{name}; ActiveRecord::Model.#{name}; end
|
|
|
|
def #{name}; ActiveRecord::Model.#{name}; end
|
|
|
|
def self.#{name}=(val); ActiveRecord::Model.#{name} = val; end
|
|
|
|
CODE
|
|
|
|
else
|
|
|
|
options[:instance_writer] ||= false
|
|
|
|
class_attribute name, options
|
|
|
|
|
|
|
|
singleton_class.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
|
|
|
remove_method :#{name}
|
|
|
|
def #{name}; ActiveRecord::Model.#{name}; end
|
|
|
|
CODE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
2012-05-25 10:58:16 -04:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
extend ConnectionHandling
|
2012-06-15 13:04:13 -04:00
|
|
|
extend ActiveModel::Observing::ClassMethods
|
2011-12-18 18:35:25 -05:00
|
|
|
|
2012-05-25 10:58:16 -04:00
|
|
|
# This allows us to detect an ActiveRecord::Model while it's in the process of being included.
|
|
|
|
module Tag; end
|
2011-12-23 19:33:05 -05:00
|
|
|
|
2012-05-25 10:58:16 -04:00
|
|
|
def self.append_features(base)
|
2011-12-18 16:42:09 -05:00
|
|
|
base.class_eval do
|
2012-05-25 10:58:16 -04:00
|
|
|
include Tag
|
|
|
|
extend Configuration
|
2011-12-18 16:42:09 -05:00
|
|
|
end
|
2012-05-25 10:58:16 -04:00
|
|
|
|
|
|
|
super
|
2011-12-18 16:42:09 -05:00
|
|
|
end
|
2011-12-24 06:02:02 -05:00
|
|
|
|
2012-05-25 10:58:16 -04:00
|
|
|
included do
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
extend ActiveSupport::Benchmarkable
|
|
|
|
extend ActiveSupport::DescendantsTracker
|
|
|
|
|
|
|
|
extend QueryCache::ClassMethods
|
|
|
|
extend Querying
|
|
|
|
extend Translation
|
|
|
|
extend DynamicMatchers
|
|
|
|
extend Explain
|
|
|
|
extend ConnectionHandling
|
2011-12-28 10:38:16 -05:00
|
|
|
|
2012-05-25 10:58:16 -04:00
|
|
|
initialize_generated_modules unless self == Base
|
2011-12-28 10:38:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
include Persistence
|
|
|
|
include ReadonlyAttributes
|
|
|
|
include ModelSchema
|
|
|
|
include Inheritance
|
|
|
|
include Scoping
|
|
|
|
include Sanitization
|
|
|
|
include Integration
|
|
|
|
include AttributeAssignment
|
|
|
|
include ActiveModel::Conversion
|
|
|
|
include Validations
|
2012-05-25 10:58:16 -04:00
|
|
|
include CounterCache
|
|
|
|
include Locking::Optimistic
|
|
|
|
include Locking::Pessimistic
|
2011-12-28 10:38:16 -05:00
|
|
|
include AttributeMethods
|
2012-05-25 10:58:16 -04:00
|
|
|
include Callbacks
|
|
|
|
include ActiveModel::Observing
|
|
|
|
include Timestamp
|
2011-12-28 10:38:16 -05:00
|
|
|
include Associations
|
|
|
|
include ActiveModel::SecurePassword
|
2012-05-25 10:58:16 -04:00
|
|
|
include AutosaveAssociation
|
|
|
|
include NestedAttributes
|
|
|
|
include Aggregations
|
|
|
|
include Transactions
|
|
|
|
include Reflection
|
|
|
|
include Serialization
|
|
|
|
include Store
|
2011-12-28 10:38:16 -05:00
|
|
|
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
|
|
|
|
|
2012-05-25 10:58:16 -04:00
|
|
|
# This hook is where config accessors on Model get defined.
|
|
|
|
#
|
|
|
|
# We don't want to just open the Model module and add stuff to it in other files, because
|
|
|
|
# that would cause Model to load, which causes all sorts of loading order issues.
|
|
|
|
#
|
|
|
|
# We need this hook rather than just using the :active_record one, because users of the
|
|
|
|
# :active_record hook may need to use config options.
|
|
|
|
ActiveSupport.run_load_hooks(:active_record_config, Model)
|
|
|
|
|
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
|