提交 a2875bec 编写于 作者: B Bryan Helmkamp

Use DependencyModule for included hooks in ActiveRecord

上级 00a9d4b9
module ActiveRecord module ActiveRecord
module Aggregations # :nodoc: module Aggregations # :nodoc:
def self.included(base) extend ActiveSupport::DependencyModule
base.extend(ClassMethods)
end
def clear_aggregation_cache #:nodoc: def clear_aggregation_cache #:nodoc:
self.class.reflect_on_all_aggregations.to_a.each do |assoc| self.class.reflect_on_all_aggregations.to_a.each do |assoc|
......
module ActiveRecord module ActiveRecord
# See ActiveRecord::AssociationPreload::ClassMethods for documentation. # See ActiveRecord::AssociationPreload::ClassMethods for documentation.
module AssociationPreload #:nodoc: module AssociationPreload #:nodoc:
def self.included(base) extend ActiveSupport::DependencyModule
base.extend(ClassMethods)
end
# Implements the details of eager loading of ActiveRecord associations. # Implements the details of eager loading of ActiveRecord associations.
# Application developers should not use this module directly. # Application developers should not use this module directly.
......
...@@ -77,6 +77,8 @@ def initialize(reflection) ...@@ -77,6 +77,8 @@ def initialize(reflection)
# See ActiveRecord::Associations::ClassMethods for documentation. # See ActiveRecord::Associations::ClassMethods for documentation.
module Associations # :nodoc: module Associations # :nodoc:
extend ActiveSupport::DependencyModule
# These classes will be loaded when associations are created. # These classes will be loaded when associations are created.
# So there is no need to eager load them. # So there is no need to eager load them.
autoload :AssociationCollection, 'active_record/associations/association_collection' autoload :AssociationCollection, 'active_record/associations/association_collection'
...@@ -89,10 +91,6 @@ module Associations # :nodoc: ...@@ -89,10 +91,6 @@ module Associations # :nodoc:
autoload :HasOneAssociation, 'active_record/associations/has_one_association' autoload :HasOneAssociation, 'active_record/associations/has_one_association'
autoload :HasOneThroughAssociation, 'active_record/associations/has_one_through_association' autoload :HasOneThroughAssociation, 'active_record/associations/has_one_through_association'
def self.included(base)
base.extend(ClassMethods)
end
# Clears out the association cache # Clears out the association cache
def clear_association_cache #:nodoc: def clear_association_cache #:nodoc:
self.class.reflect_on_all_associations.to_a.each do |assoc| self.class.reflect_on_all_associations.to_a.each do |assoc|
......
module ActiveRecord module ActiveRecord
module AttributeMethods #:nodoc: module AttributeMethods #:nodoc:
extend ActiveSupport::DependencyModule
DEFAULT_SUFFIXES = %w(= ? _before_type_cast) DEFAULT_SUFFIXES = %w(= ? _before_type_cast)
ATTRIBUTE_TYPES_CACHED_BY_DEFAULT = [:datetime, :timestamp, :time, :date] ATTRIBUTE_TYPES_CACHED_BY_DEFAULT = [:datetime, :timestamp, :time, :date]
def self.included(base) included do
base.extend ClassMethods attribute_method_suffix(*DEFAULT_SUFFIXES)
base.attribute_method_suffix(*DEFAULT_SUFFIXES)
base.cattr_accessor :attribute_types_cached_by_default, :instance_writer => false cattr_accessor :attribute_types_cached_by_default, :instance_writer => false
base.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT self.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
base.cattr_accessor :time_zone_aware_attributes, :instance_writer => false
base.time_zone_aware_attributes = false cattr_accessor :time_zone_aware_attributes, :instance_writer => false
base.class_inheritable_accessor :skip_time_zone_conversion_for_attributes, :instance_writer => false self.time_zone_aware_attributes = false
base.skip_time_zone_conversion_for_attributes = []
class_inheritable_accessor :skip_time_zone_conversion_for_attributes, :instance_writer => false
self.skip_time_zone_conversion_for_attributes = []
end end
# Declare and check for suffixed attribute methods. # Declare and check for suffixed attribute methods.
......
...@@ -125,16 +125,15 @@ module ActiveRecord ...@@ -125,16 +125,15 @@ module ActiveRecord
# post.author.name = '' # post.author.name = ''
# post.save(false) # => true # post.save(false) # => true
module AutosaveAssociation module AutosaveAssociation
extend ActiveSupport::DependencyModule
ASSOCIATION_TYPES = %w{ has_one belongs_to has_many has_and_belongs_to_many } ASSOCIATION_TYPES = %w{ has_one belongs_to has_many has_and_belongs_to_many }
def self.included(base) included do
base.class_eval do alias_method_chain :reload, :autosave_associations
base.extend(ClassMethods)
alias_method_chain :reload, :autosave_associations
ASSOCIATION_TYPES.each do |type| ASSOCIATION_TYPES.each do |type|
base.send("valid_keys_for_#{type}_association") << :autosave send("valid_keys_for_#{type}_association") << :autosave
end
end end
end end
......
module ActiveRecord module ActiveRecord
module Batches # :nodoc: module Batches # :nodoc:
def self.included(base) extend ActiveSupport::DependencyModule
base.extend(ClassMethods)
end
# When processing large numbers of records, it's often a good idea to do # When processing large numbers of records, it's often a good idea to do
# so in batches to prevent memory ballooning. # so in batches to prevent memory ballooning.
......
module ActiveRecord module ActiveRecord
module Calculations #:nodoc: module Calculations #:nodoc:
extend ActiveSupport::DependencyModule
CALCULATIONS_OPTIONS = [:conditions, :joins, :order, :select, :group, :having, :distinct, :limit, :offset, :include, :from] CALCULATIONS_OPTIONS = [:conditions, :joins, :order, :select, :group, :having, :distinct, :limit, :offset, :include, :from]
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods module ClassMethods
# Count operates using three different approaches. # Count operates using three different approaches.
......
...@@ -211,21 +211,23 @@ module ActiveRecord ...@@ -211,21 +211,23 @@ module ActiveRecord
# needs to be aware of it because an ordinary +save+ will raise such exception # needs to be aware of it because an ordinary +save+ will raise such exception
# instead of quietly returning +false+. # instead of quietly returning +false+.
module Callbacks module Callbacks
extend ActiveSupport::DependencyModule
CALLBACKS = %w( CALLBACKS = %w(
after_find after_initialize before_save after_save before_create after_create before_update after_update before_validation after_find after_initialize before_save after_save before_create after_create before_update after_update before_validation
after_validation before_validation_on_create after_validation_on_create before_validation_on_update after_validation before_validation_on_create after_validation_on_create before_validation_on_update
after_validation_on_update before_destroy after_destroy after_validation_on_update before_destroy after_destroy
) )
def self.included(base) #:nodoc: included do
base.extend Observable extend Observable
[:create_or_update, :valid?, :create, :update, :destroy].each do |method| [:create_or_update, :valid?, :create, :update, :destroy].each do |method|
base.send :alias_method_chain, method, :callbacks alias_method_chain method, :callbacks
end end
base.send :include, ActiveSupport::Callbacks include ActiveSupport::Callbacks
base.define_callbacks *CALLBACKS define_callbacks *CALLBACKS
end end
# Is called when the object was instantiated by one of the finders, like <tt>Base.find</tt>. # Is called when the object was instantiated by one of the finders, like <tt>Base.find</tt>.
......
...@@ -34,20 +34,21 @@ module ActiveRecord ...@@ -34,20 +34,21 @@ module ActiveRecord
# person.name << 'by' # person.name << 'by'
# person.name_change # => ['uncle bob', 'uncle bobby'] # person.name_change # => ['uncle bob', 'uncle bobby']
module Dirty module Dirty
extend ActiveSupport::DependencyModule
DIRTY_SUFFIXES = ['_changed?', '_change', '_will_change!', '_was'] DIRTY_SUFFIXES = ['_changed?', '_change', '_will_change!', '_was']
def self.included(base) included do
base.attribute_method_suffix *DIRTY_SUFFIXES attribute_method_suffix *DIRTY_SUFFIXES
base.alias_method_chain :write_attribute, :dirty
base.alias_method_chain :save, :dirty
base.alias_method_chain :save!, :dirty
base.alias_method_chain :update, :dirty
base.alias_method_chain :reload, :dirty
base.superclass_delegating_accessor :partial_updates alias_method_chain :write_attribute, :dirty
base.partial_updates = true alias_method_chain :save, :dirty
alias_method_chain :save!, :dirty
alias_method_chain :update, :dirty
alias_method_chain :reload, :dirty
base.send(:extend, ClassMethods) superclass_delegating_accessor :partial_updates
self.partial_updates = true
end end
# Do any attributes have unsaved changes? # Do any attributes have unsaved changes?
......
...@@ -805,27 +805,25 @@ def find ...@@ -805,27 +805,25 @@ def find
module ActiveRecord module ActiveRecord
module TestFixtures module TestFixtures
def self.included(base) extend ActiveSupport::DependencyModule
base.class_eval do
setup :setup_fixtures included do
teardown :teardown_fixtures setup :setup_fixtures
teardown :teardown_fixtures
superclass_delegating_accessor :fixture_path
superclass_delegating_accessor :fixture_table_names superclass_delegating_accessor :fixture_path
superclass_delegating_accessor :fixture_class_names superclass_delegating_accessor :fixture_table_names
superclass_delegating_accessor :use_transactional_fixtures superclass_delegating_accessor :fixture_class_names
superclass_delegating_accessor :use_instantiated_fixtures # true, false, or :no_instances superclass_delegating_accessor :use_transactional_fixtures
superclass_delegating_accessor :pre_loaded_fixtures superclass_delegating_accessor :use_instantiated_fixtures # true, false, or :no_instances
superclass_delegating_accessor :pre_loaded_fixtures
self.fixture_table_names = []
self.use_transactional_fixtures = false self.fixture_table_names = []
self.use_instantiated_fixtures = true self.use_transactional_fixtures = false
self.pre_loaded_fixtures = false self.use_instantiated_fixtures = true
self.pre_loaded_fixtures = false
self.fixture_class_names = {}
end
base.extend ClassMethods self.fixture_class_names = {}
end end
module ClassMethods module ClassMethods
......
...@@ -42,17 +42,17 @@ module Locking ...@@ -42,17 +42,17 @@ module Locking
# To override the name of the lock_version column, invoke the <tt>set_locking_column</tt> method. # To override the name of the lock_version column, invoke the <tt>set_locking_column</tt> method.
# This method uses the same syntax as <tt>set_table_name</tt> # This method uses the same syntax as <tt>set_table_name</tt>
module Optimistic module Optimistic
def self.included(base) #:nodoc: extend ActiveSupport::DependencyModule
base.extend ClassMethods
base.cattr_accessor :lock_optimistically, :instance_writer => false included do
base.lock_optimistically = true cattr_accessor :lock_optimistically, :instance_writer => false
self.lock_optimistically = true
base.alias_method_chain :update, :lock alias_method_chain :update, :lock
base.alias_method_chain :destroy, :lock alias_method_chain :destroy, :lock
base.alias_method_chain :attributes_from_column_definition, :lock alias_method_chain :attributes_from_column_definition, :lock
class << base class << self
alias_method :locking_column=, :set_locking_column alias_method :locking_column=, :set_locking_column
end end
end end
......
module ActiveRecord module ActiveRecord
module NamedScope module NamedScope
extend ActiveSupport::DependencyModule
# All subclasses of ActiveRecord::Base have one named scope: # All subclasses of ActiveRecord::Base have one named scope:
# * <tt>scoped</tt> - which allows for the creation of anonymous \scopes, on the fly: <tt>Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions)</tt> # * <tt>scoped</tt> - which allows for the creation of anonymous \scopes, on the fly: <tt>Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions)</tt>
# #
...@@ -7,11 +9,8 @@ module NamedScope ...@@ -7,11 +9,8 @@ module NamedScope
# intermediate values (scopes) around as first-class objects is convenient. # intermediate values (scopes) around as first-class objects is convenient.
# #
# You can define a scope that applies to all finders using ActiveRecord::Base.default_scope. # You can define a scope that applies to all finders using ActiveRecord::Base.default_scope.
def self.included(base) included do
base.class_eval do named_scope :scoped, lambda { |scope| scope }
extend ClassMethods
named_scope :scoped, lambda { |scope| scope }
end
end end
module ClassMethods module ClassMethods
......
module ActiveRecord module ActiveRecord
module NestedAttributes #:nodoc: module NestedAttributes #:nodoc:
def self.included(base) extend ActiveSupport::DependencyModule
base.extend(ClassMethods)
base.class_inheritable_accessor :reject_new_nested_attributes_procs, :instance_writer => false included do
base.reject_new_nested_attributes_procs = {} class_inheritable_accessor :reject_new_nested_attributes_procs, :instance_writer => false
self.reject_new_nested_attributes_procs = {}
end end
# == Nested Attributes # == Nested Attributes
......
...@@ -3,9 +3,7 @@ ...@@ -3,9 +3,7 @@
module ActiveRecord module ActiveRecord
module Observing # :nodoc: module Observing # :nodoc:
def self.included(base) extend ActiveSupport::DependencyModule
base.extend ClassMethods
end
module ClassMethods module ClassMethods
# Activates the observers assigned. Examples: # Activates the observers assigned. Examples:
......
module ActiveRecord module ActiveRecord
module Reflection # :nodoc: module Reflection # :nodoc:
def self.included(base) extend ActiveSupport::DependencyModule
base.extend(ClassMethods)
end
# Reflection allows you to interrogate Active Record classes and objects about their associations and aggregations. # Reflection allows you to interrogate Active Record classes and objects about their associations and aggregations.
# This information can, for example, be used in a form builder that took an Active Record object and created input # This information can, for example, be used in a form builder that took an Active Record object and created input
......
...@@ -2,9 +2,10 @@ ...@@ -2,9 +2,10 @@
module ActiveRecord #:nodoc: module ActiveRecord #:nodoc:
module Serialization module Serialization
def self.included(base) extend ActiveSupport::DependencyModule
base.cattr_accessor :include_root_in_json, :instance_writer => false
base.extend ClassMethods included do
cattr_accessor :include_root_in_json, :instance_writer => false
end end
# Returns a JSON string representing the model. Some configuration is # Returns a JSON string representing the model. Some configuration is
......
...@@ -8,12 +8,14 @@ module ActiveRecord ...@@ -8,12 +8,14 @@ module ActiveRecord
# Timestamps are in the local timezone by default but you can use UTC by setting # Timestamps are in the local timezone by default but you can use UTC by setting
# <tt>ActiveRecord::Base.default_timezone = :utc</tt> # <tt>ActiveRecord::Base.default_timezone = :utc</tt>
module Timestamp module Timestamp
def self.included(base) #:nodoc: extend ActiveSupport::DependencyModule
base.alias_method_chain :create, :timestamps
base.alias_method_chain :update, :timestamps
base.class_inheritable_accessor :record_timestamps, :instance_writer => false included do
base.record_timestamps = true alias_method_chain :create, :timestamps
alias_method_chain :update, :timestamps
class_inheritable_accessor :record_timestamps, :instance_writer => false
self.record_timestamps = true
end end
# Saves the record with the updated_at/on attributes set to the current time. # Saves the record with the updated_at/on attributes set to the current time.
......
...@@ -3,16 +3,14 @@ ...@@ -3,16 +3,14 @@
module ActiveRecord module ActiveRecord
# See ActiveRecord::Transactions::ClassMethods for documentation. # See ActiveRecord::Transactions::ClassMethods for documentation.
module Transactions module Transactions
extend ActiveSupport::DependencyModule
class TransactionError < ActiveRecordError # :nodoc: class TransactionError < ActiveRecordError # :nodoc:
end end
def self.included(base) included do
base.extend(ClassMethods) [:destroy, :save, :save!].each do |method|
alias_method_chain method, :transactions
base.class_eval do
[:destroy, :save, :save!].each do |method|
alias_method_chain method, :transactions
end
end end
end end
......
...@@ -301,17 +301,16 @@ def to_xml(options={}) ...@@ -301,17 +301,16 @@ def to_xml(options={})
# #
# An Errors object is automatically created for every Active Record. # An Errors object is automatically created for every Active Record.
module Validations module Validations
extend ActiveSupport::DependencyModule
VALIDATIONS = %w( validate validate_on_create validate_on_update ) VALIDATIONS = %w( validate validate_on_create validate_on_update )
def self.included(base) # :nodoc: included do
base.extend ClassMethods alias_method_chain :save, :validation
base.class_eval do alias_method_chain :save!, :validation
alias_method_chain :save, :validation
alias_method_chain :save!, :validation
end
base.send :include, ActiveSupport::Callbacks include ActiveSupport::Callbacks
base.define_callbacks *VALIDATIONS define_callbacks *VALIDATIONS
end end
# Active Record classes can implement validations in several ways. The highest level, easiest to read, # Active Record classes can implement validations in several ways. The highest level, easiest to read,
......
...@@ -6,13 +6,12 @@ ...@@ -6,13 +6,12 @@
require 'models/categorization' require 'models/categorization'
module Remembered module Remembered
def self.included(base) extend ActiveSupport::DependencyModule
base.extend ClassMethods
base.class_eval do included do
after_create :remember after_create :remember
protected protected
def remember; self.class.remembered << self; end def remember; self.class.remembered << self; end
end
end end
module ClassMethods module ClassMethods
......
module ActiveRecord module ActiveRecord
module Testing module Testing
module RepairHelper module RepairHelper
def self.included(base) extend ActiveSupport::DependencyModule
base.class_eval do
extend ClassMethods
end
end
module Toolbox module Toolbox
def self.record_validations(*model_classes) def self.record_validations(*model_classes)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册