1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Break up DependencyModule's dual function of providing a "depend_on" DSL and "included" block DSL into separate modules. But, unify both approaches under AS::Concern.

This commit is contained in:
Joshua Peek 2009-05-28 11:35:36 -05:00
parent de203245af
commit 4e50a35fa2
52 changed files with 98 additions and 89 deletions

View file

@ -1,6 +1,6 @@
module AbstractController
module Benchmarker
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
depends_on Logger

View file

@ -1,6 +1,6 @@
module AbstractController
module Callbacks
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
depends_on ActiveSupport::NewCallbacks

View file

@ -1,6 +1,6 @@
module AbstractController
module Helpers
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
depends_on Renderer

View file

@ -1,6 +1,6 @@
module AbstractController
module Layouts
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
depends_on Renderer

View file

@ -3,7 +3,7 @@ require 'active_support/core_ext/logger'
module AbstractController
module Logger
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
class DelayedLog
def initialize(&blk)

View file

@ -2,7 +2,7 @@ require "action_controller/abstract/logger"
module AbstractController
module Renderer
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
depends_on AbstractController::Logger

View file

@ -26,7 +26,7 @@ module ActionController #:nodoc:
#
# See docs on the FlashHash class for more details about the flash.
module Flash
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
# TODO : Remove the defined? check when new base is the main base
depends_on Session if defined?(ActionController::Http)

View file

@ -1,6 +1,6 @@
module ActionController
module FilterParameterLogging
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
# TODO : Remove the defined? check when new base is the main base
if defined?(ActionController::Http)

View file

@ -3,7 +3,7 @@ require 'active_support/dependencies'
# FIXME: helper { ... } is broken on Ruby 1.9
module ActionController #:nodoc:
module Helpers #:nodoc:
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
included do
# Initialize the base module to aggregate its helpers.

View file

@ -3,7 +3,7 @@ module ActionController #:nodoc:
end
module RequestForgeryProtection
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
# TODO : Remove the defined? check when new base is the main base
if defined?(ActionController::Http)

View file

@ -2,7 +2,7 @@ module ActionController #:nodoc:
# Methods for sending arbitrary data and for streaming files to the browser,
# instead of rendering.
module Streaming
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
# TODO : Remove the defined? check when new base is the main base
if defined?(ActionController::Http)

View file

@ -1,6 +1,6 @@
module ActionController #:nodoc:
module Verification #:nodoc:
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
# TODO : Remove the defined? check when new base is the main base
if defined?(ActionController::Http)

View file

@ -24,7 +24,7 @@ module ActionController #:nodoc:
# ActionController::Base.cache_store = :mem_cache_store, "localhost"
# ActionController::Base.cache_store = MyOwnStore.new("parameter")
module Caching
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
autoload :Actions, 'action_controller/caching/actions'
autoload :Fragments, 'action_controller/caching/fragments'

View file

@ -1,6 +1,6 @@
module ActionController
module Rails2Compatibility
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
class ::ActionController::ActionControllerError < StandardError #:nodoc:
end

View file

@ -1,6 +1,6 @@
module ActionController
module ConditionalGet
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
depends_on RackConvenience

View file

@ -4,7 +4,7 @@ require 'active_support/dependencies'
module ActionController
module Helpers
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
depends_on AbstractController::Helpers

View file

@ -1,6 +1,6 @@
module ActionController
module HideActions
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
included do
extlib_inheritable_accessor :hidden_actions

View file

@ -1,6 +1,6 @@
module ActionController
module Layouts
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
depends_on ActionController::Renderer
depends_on AbstractController::Layouts

View file

@ -1,6 +1,6 @@
module ActionController
module RackConvenience
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
included do
delegate :headers, :status=, :location=,

View file

@ -1,6 +1,6 @@
module ActionController
module RenderOptions
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
included do
extlib_inheritable_accessor :_renderers
@ -36,14 +36,12 @@ module ActionController
end
end
module RenderOption
extend ActiveSupport::DependencyModule
module RenderOption #:nodoc:
def self.extended(base)
base.extend ActiveSupport::Concern
base.depends_on ::ActionController::RenderOptions
included do
extend ActiveSupport::DependencyModule
depends_on ::ActionController::RenderOptions
def self.register_renderer(name)
def base.register_renderer(name)
included { _add_render_option(name) }
end
end
@ -51,7 +49,7 @@ module ActionController
module Renderers
module Json
include RenderOption
extend RenderOption
register_renderer :json
def _render_json(json, options)
@ -63,7 +61,7 @@ module ActionController
end
module Js
include RenderOption
extend RenderOption
register_renderer :js
def _render_js(js, options)
@ -73,7 +71,7 @@ module ActionController
end
module Xml
include RenderOption
extend RenderOption
register_renderer :xml
def _render_xml(xml, options)
@ -82,8 +80,8 @@ module ActionController
end
end
module Rjs
include RenderOption
module RJS
extend RenderOption
register_renderer :update
def _render_update(proc, options)
@ -94,14 +92,12 @@ module ActionController
end
module All
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
included do
include ::ActionController::Renderers::Json
include ::ActionController::Renderers::Js
include ::ActionController::Renderers::Xml
include ::ActionController::Renderers::Rjs
end
depends_on ActionController::Renderers::Json
depends_on ActionController::Renderers::Js
depends_on ActionController::Renderers::Xml
depends_on ActionController::Renderers::RJS
end
end
end

View file

@ -1,6 +1,6 @@
module ActionController
module Renderer
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
depends_on AbstractController::Renderer

View file

@ -15,7 +15,7 @@ module ActionController #:nodoc:
# behavior is achieved by overriding the <tt>rescue_action_in_public</tt>
# and <tt>rescue_action_locally</tt> methods.
module Rescue
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
included do
include ActiveSupport::Rescuable

View file

@ -1,6 +1,6 @@
module ActionController
module Session
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
depends_on RackConvenience

View file

@ -1,6 +1,6 @@
module ActionController
module Testing
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
depends_on RackConvenience

View file

@ -1,6 +1,6 @@
module ActionController
module UrlFor
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
depends_on RackConvenience

View file

@ -301,7 +301,7 @@ module ActionController
# A module used to extend ActionController::Base, so that integration tests
# can capture the controller used to satisfy a request.
module ControllerCapture #:nodoc:
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
included do
alias_method_chain :initialize, :capture

View file

@ -171,7 +171,7 @@ module ActionView
# <% end %>
module Partials
extend ActiveSupport::Memoizable
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
included do
attr_accessor :_partial

View file

@ -1,6 +1,6 @@
module ActiveRecord
module Aggregations # :nodoc:
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
def clear_aggregation_cache #:nodoc:
self.class.reflect_on_all_aggregations.to_a.each do |assoc|

View file

@ -1,7 +1,7 @@
module ActiveRecord
# See ActiveRecord::AssociationPreload::ClassMethods for documentation.
module AssociationPreload #:nodoc:
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
# Implements the details of eager loading of ActiveRecord associations.
# Application developers should not use this module directly.

View file

@ -79,7 +79,7 @@ module ActiveRecord
# See ActiveRecord::Associations::ClassMethods for documentation.
module Associations # :nodoc:
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
# These classes will be loaded when associations are created.
# So there is no need to eager load them.

View file

@ -2,7 +2,7 @@ require 'active_support/core_ext/enumerable'
module ActiveRecord
module AttributeMethods #:nodoc:
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
DEFAULT_SUFFIXES = %w(= ? _before_type_cast)
ATTRIBUTE_TYPES_CACHED_BY_DEFAULT = [:datetime, :timestamp, :time, :date]

View file

@ -125,7 +125,7 @@ module ActiveRecord
# post.author.name = ''
# post.save(false) # => true
module AutosaveAssociation
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
ASSOCIATION_TYPES = %w{ has_one belongs_to has_many has_and_belongs_to_many }

View file

@ -1,6 +1,6 @@
module ActiveRecord
module Batches # :nodoc:
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
# When processing large numbers of records, it's often a good idea to do
# so in batches to prevent memory ballooning.

View file

@ -1,6 +1,6 @@
module ActiveRecord
module Calculations #:nodoc:
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
CALCULATIONS_OPTIONS = [:conditions, :joins, :order, :select, :group, :having, :distinct, :limit, :offset, :include, :from]

View file

@ -211,7 +211,7 @@ module ActiveRecord
# needs to be aware of it because an ordinary +save+ will raise such exception
# instead of quietly returning +false+.
module Callbacks
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
CALLBACKS = %w(
after_find after_initialize before_save after_save before_create after_create before_update after_update before_validation

View file

@ -34,7 +34,7 @@ module ActiveRecord
# person.name << 'by'
# person.name_change # => ['uncle bob', 'uncle bobby']
module Dirty
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
DIRTY_SUFFIXES = ['_changed?', '_change', '_will_change!', '_was']

View file

@ -807,7 +807,7 @@ end
module ActiveRecord
module TestFixtures
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
included do
setup :setup_fixtures

View file

@ -42,7 +42,7 @@ module ActiveRecord
# 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>
module Optimistic
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
included do
cattr_accessor :lock_optimistically, :instance_writer => false

View file

@ -3,7 +3,7 @@ require 'active_support/core_ext/hash/except'
module ActiveRecord
module NamedScope
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
# 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>

View file

@ -3,7 +3,7 @@ require 'active_support/core_ext/object/try'
module ActiveRecord
module NestedAttributes #:nodoc:
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
included do
class_inheritable_accessor :reject_new_nested_attributes_procs, :instance_writer => false

View file

@ -3,7 +3,7 @@ require 'set'
module ActiveRecord
module Observing # :nodoc:
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
module ClassMethods
# Activates the observers assigned. Examples:

View file

@ -1,6 +1,6 @@
module ActiveRecord
module Reflection # :nodoc:
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
# 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

View file

@ -2,7 +2,7 @@ require 'active_support/json'
module ActiveRecord #:nodoc:
module Serialization
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
included do
cattr_accessor :include_root_in_json, :instance_writer => false

View file

@ -8,7 +8,7 @@ module ActiveRecord
# Timestamps are in the local timezone by default but you can use UTC by setting
# <tt>ActiveRecord::Base.default_timezone = :utc</tt>
module Timestamp
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
included do
alias_method_chain :create, :timestamps

View file

@ -3,7 +3,7 @@ require 'thread'
module ActiveRecord
# See ActiveRecord::Transactions::ClassMethods for documentation.
module Transactions
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
class TransactionError < ActiveRecordError # :nodoc:
end

View file

@ -302,7 +302,7 @@ module ActiveRecord
#
# An Errors object is automatically created for every Active Record.
module Validations
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
VALIDATIONS = %w( validate validate_on_create validate_on_update )

View file

@ -7,7 +7,7 @@ require 'models/categorization'
require 'active_support/core_ext/array/random_access'
module Remembered
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
included do
after_create :remember

View file

@ -1,7 +1,7 @@
module ActiveRecord
module Testing
module RepairHelper
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
module Toolbox
def self.record_validations(*model_classes)

View file

@ -5,7 +5,7 @@ module ActiveSupport
autoload :BufferedLogger, 'active_support/buffered_logger'
autoload :Cache, 'active_support/cache'
autoload :Callbacks, 'active_support/callbacks'
autoload :NewCallbacks, 'active_support/new_callbacks'
autoload :Concern, 'active_support/concern'
autoload :ConcurrentHash, 'active_support/concurrent_hash'
autoload :DependencyModule, 'active_support/dependency_module'
autoload :Deprecation, 'active_support/deprecation'
@ -15,6 +15,7 @@ module ActiveSupport
autoload :MessageEncryptor, 'active_support/message_encryptor'
autoload :MessageVerifier, 'active_support/message_verifier'
autoload :Multibyte, 'active_support/multibyte'
autoload :NewCallbacks, 'active_support/new_callbacks'
autoload :OptionMerger, 'active_support/option_merger'
autoload :OrderedHash, 'active_support/ordered_hash'
autoload :OrderedOptions, 'active_support/ordered_options'

View file

@ -0,0 +1,22 @@
require 'active_support/dependency_module'
module ActiveSupport
module Concern
include DependencyModule
def append_features(base)
if super
base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block")
end
end
def included(base = nil, &block)
if base.nil?
@_included_block = block
else
super
end
end
end
end

View file

@ -1,19 +1,9 @@
module ActiveSupport
module DependencyModule
def append_features(base)
return if base < self
return false if base < self
(@_dependencies ||= []).each { |dep| base.send(:include, dep) }
super
base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block")
end
def included(base = nil, &block)
if base.nil?
@_included_block = block
else
super
end
end
def depends_on(*mods)

View file

@ -1,9 +1,9 @@
require 'abstract_unit'
require 'active_support/dependency_module'
require 'active_support/concern'
class DependencyModuleTest < Test::Unit::TestCase
class ConcernTest < Test::Unit::TestCase
module Baz
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
module ClassMethods
def baz
@ -29,7 +29,7 @@ class DependencyModuleTest < Test::Unit::TestCase
end
module Bar
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
depends_on Baz
@ -43,7 +43,7 @@ class DependencyModuleTest < Test::Unit::TestCase
end
module Foo
extend ActiveSupport::DependencyModule
extend ActiveSupport::Concern
depends_on Bar, Baz
end
@ -55,17 +55,17 @@ class DependencyModuleTest < Test::Unit::TestCase
def test_module_is_included_normally
@klass.send(:include, Baz)
assert_equal "baz", @klass.new.baz
assert_equal DependencyModuleTest::Baz, @klass.included_modules[0]
assert_equal ConcernTest::Baz, @klass.included_modules[0]
@klass.send(:include, Baz)
assert_equal "baz", @klass.new.baz
assert_equal DependencyModuleTest::Baz, @klass.included_modules[0]
assert_equal ConcernTest::Baz, @klass.included_modules[0]
end
def test_class_methods_are_extended
@klass.send(:include, Baz)
assert_equal "baz", @klass.baz
assert_equal DependencyModuleTest::Baz::ClassMethods, (class << @klass; self.included_modules; end)[0]
assert_equal ConcernTest::Baz::ClassMethods, (class << @klass; self.included_modules; end)[0]
end
def test_included_block_is_ran
@ -78,11 +78,11 @@ class DependencyModuleTest < Test::Unit::TestCase
assert_equal "bar", @klass.new.bar
assert_equal "bar+baz", @klass.new.baz
assert_equal "baz", @klass.baz
assert_equal [DependencyModuleTest::Bar, DependencyModuleTest::Baz], @klass.included_modules[0..1]
assert_equal [ConcernTest::Bar, ConcernTest::Baz], @klass.included_modules[0..1]
end
def test_depends_on_with_multiple_modules
@klass.send(:include, Foo)
assert_equal [DependencyModuleTest::Foo, DependencyModuleTest::Bar, DependencyModuleTest::Baz], @klass.included_modules[0..2]
assert_equal [ConcernTest::Foo, ConcernTest::Bar, ConcernTest::Baz], @klass.included_modules[0..2]
end
end