mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add depends_on, use, and setup to abstract up ideas about module inheritance.
This commit is contained in:
parent
9c8eaf8e25
commit
c1aa5b0e14
15 changed files with 82 additions and 73 deletions
|
@ -1,11 +1,8 @@
|
||||||
module AbstractController
|
module AbstractController
|
||||||
module Callbacks
|
module Callbacks
|
||||||
def self.included(klass)
|
setup do
|
||||||
klass.class_eval do
|
include ActiveSupport::NewCallbacks
|
||||||
include ActiveSupport::NewCallbacks
|
define_callbacks :process_action
|
||||||
define_callbacks :process_action
|
|
||||||
extend ClassMethods
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def process_action
|
def process_action
|
||||||
|
|
|
@ -1,17 +1,18 @@
|
||||||
module AbstractController
|
module AbstractController
|
||||||
module Helpers
|
module Helpers
|
||||||
|
depends_on Renderer
|
||||||
def self.included(klass)
|
|
||||||
klass.class_eval do
|
setup do
|
||||||
extend ClassMethods
|
extlib_inheritable_accessor :master_helper_module
|
||||||
unless self < ::AbstractController::Renderer
|
self.master_helper_module = Module.new
|
||||||
raise "You need to include AbstractController::Renderer before including " \
|
|
||||||
"AbstractController::Helpers"
|
|
||||||
end
|
|
||||||
extlib_inheritable_accessor :master_helper_module
|
|
||||||
self.master_helper_module = Module.new
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# def self.included(klass)
|
||||||
|
# klass.class_eval do
|
||||||
|
# extlib_inheritable_accessor :master_helper_module
|
||||||
|
# self.master_helper_module = Module.new
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
|
||||||
def _action_view
|
def _action_view
|
||||||
@_action_view ||= begin
|
@_action_view ||= begin
|
||||||
|
|
|
@ -1,25 +1,24 @@
|
||||||
module AbstractController
|
module AbstractController
|
||||||
module Layouts
|
module Layouts
|
||||||
|
|
||||||
def self.included(base)
|
depends_on Renderer
|
||||||
base.extend ClassMethods
|
|
||||||
end
|
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
def layout(layout)
|
def layout(layout)
|
||||||
unless [String, Symbol, FalseClass, NilClass].include?(layout.class)
|
unless [String, Symbol, FalseClass, NilClass].include?(layout.class)
|
||||||
raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil"
|
raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil"
|
||||||
end
|
end
|
||||||
|
|
||||||
@layout = layout || false # Converts nil to false
|
@_layout = layout || false # Converts nil to false
|
||||||
|
_write_layout_method
|
||||||
end
|
end
|
||||||
|
|
||||||
def _write_layout_method
|
def _write_layout_method
|
||||||
case @layout
|
case @_layout
|
||||||
when String
|
when String
|
||||||
self.class_eval %{def _layout() #{@layout.inspect} end}
|
self.class_eval %{def _layout() #{@_layout.inspect} end}
|
||||||
when Symbol
|
when Symbol
|
||||||
self.class_eval %{def _layout() #{@layout} end}
|
self.class_eval %{def _layout() #{@_layout} end}
|
||||||
when false
|
when false
|
||||||
self.class_eval %{def _layout() end}
|
self.class_eval %{def _layout() end}
|
||||||
else
|
else
|
||||||
|
@ -43,7 +42,7 @@ module AbstractController
|
||||||
private
|
private
|
||||||
|
|
||||||
def _layout() end # This will be overwritten
|
def _layout() end # This will be overwritten
|
||||||
|
|
||||||
def _layout_for_option(name)
|
def _layout_for_option(name)
|
||||||
case name
|
case name
|
||||||
when String then _layout_for_name(name)
|
when String then _layout_for_name(name)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module AbstractController
|
module AbstractController
|
||||||
module Logger
|
module Logger
|
||||||
def self.included(klass)
|
setup do
|
||||||
klass.cattr_accessor :logger
|
cattr_accessor :logger
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -2,20 +2,16 @@ require "action_controller/abstract/logger"
|
||||||
|
|
||||||
module AbstractController
|
module AbstractController
|
||||||
module Renderer
|
module Renderer
|
||||||
|
depends_on AbstractController::Logger
|
||||||
|
|
||||||
def self.included(klass)
|
setup do
|
||||||
klass.class_eval do
|
attr_internal :formats
|
||||||
extend ClassMethods
|
|
||||||
|
extlib_inheritable_accessor :_view_paths
|
||||||
attr_internal :formats
|
|
||||||
|
self._view_paths ||= ActionView::PathSet.new
|
||||||
extlib_inheritable_accessor :_view_paths
|
|
||||||
|
|
||||||
self._view_paths ||= ActionView::PathSet.new
|
|
||||||
include AbstractController::Logger
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def _action_view
|
def _action_view
|
||||||
@_action_view ||= ActionView::Base.new(self.class.view_paths, {}, self)
|
@_action_view ||= ActionView::Base.new(self.class.view_paths, {}, self)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
module ActionController
|
module ActionController
|
||||||
module HideActions
|
module HideActions
|
||||||
def self.included(klass)
|
setup do
|
||||||
klass.class_eval do
|
extlib_inheritable_accessor :hidden_actions
|
||||||
extend ClassMethods
|
self.hidden_actions ||= Set.new
|
||||||
extlib_inheritable_accessor :hidden_actions
|
|
||||||
self.hidden_actions ||= Set.new
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def action_methods() self.class.action_names end
|
def action_methods() self.class.action_names end
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
module ActionController
|
module ActionController
|
||||||
module Layouts
|
module Layouts
|
||||||
|
depends_on ActionController::Renderer
|
||||||
|
depends_on AbstractController::Layouts
|
||||||
|
|
||||||
def render_to_string(options)
|
def render_to_string(options)
|
||||||
if !options.key?(:text) || options.key?(:layout)
|
if !options.key?(:text) || options.key?(:layout)
|
||||||
options[:_layout] = options.key?(:layout) ? _layout_for_option(options[:layout]) : _default_layout
|
options[:_layout] = options.key?(:layout) ? _layout_for_option(options[:layout]) : _default_layout
|
||||||
|
|
|
@ -1,15 +1,6 @@
|
||||||
module ActionController
|
module ActionController
|
||||||
module Renderer
|
module Renderer
|
||||||
|
depends_on AbstractController::Renderer
|
||||||
# def self.included(klass)
|
|
||||||
# klass.extend ClassMethods
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# module ClassMethods
|
|
||||||
# def prefix
|
|
||||||
# @prefix ||= name.underscore
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
|
|
||||||
def initialize(*)
|
def initialize(*)
|
||||||
self.formats = [:html]
|
self.formats = [:html]
|
||||||
|
|
|
@ -27,7 +27,7 @@ module AbstractController
|
||||||
# Test Render mixin
|
# Test Render mixin
|
||||||
# ====
|
# ====
|
||||||
class RenderingController < AbstractController::Base
|
class RenderingController < AbstractController::Base
|
||||||
include Renderer
|
use Renderer
|
||||||
|
|
||||||
def _prefix() end
|
def _prefix() end
|
||||||
|
|
||||||
|
@ -116,7 +116,7 @@ module AbstractController
|
||||||
# ====
|
# ====
|
||||||
# self._layout is used when defined
|
# self._layout is used when defined
|
||||||
class WithLayouts < PrefixedViews
|
class WithLayouts < PrefixedViews
|
||||||
include Layouts
|
use Layouts
|
||||||
|
|
||||||
private
|
private
|
||||||
def self.layout(formats)
|
def self.layout(formats)
|
||||||
|
|
|
@ -4,7 +4,7 @@ module AbstractController
|
||||||
module Testing
|
module Testing
|
||||||
|
|
||||||
class ControllerWithCallbacks < AbstractController::Base
|
class ControllerWithCallbacks < AbstractController::Base
|
||||||
include AbstractController::Callbacks
|
use AbstractController::Callbacks
|
||||||
end
|
end
|
||||||
|
|
||||||
class Callback1 < ControllerWithCallbacks
|
class Callback1 < ControllerWithCallbacks
|
||||||
|
|
|
@ -4,8 +4,8 @@ module AbstractController
|
||||||
module Testing
|
module Testing
|
||||||
|
|
||||||
class ControllerWithHelpers < AbstractController::Base
|
class ControllerWithHelpers < AbstractController::Base
|
||||||
include Renderer
|
use Renderer
|
||||||
include Helpers
|
use Helpers
|
||||||
|
|
||||||
def render(string)
|
def render(string)
|
||||||
super(:_template_name => string)
|
super(:_template_name => string)
|
||||||
|
|
|
@ -5,8 +5,8 @@ module AbstractControllerTests
|
||||||
|
|
||||||
# Base controller for these tests
|
# Base controller for these tests
|
||||||
class Base < AbstractController::Base
|
class Base < AbstractController::Base
|
||||||
include AbstractController::Renderer
|
use AbstractController::Renderer
|
||||||
include AbstractController::Layouts
|
use AbstractController::Layouts
|
||||||
|
|
||||||
self.view_paths = [ActionView::FixtureTemplate::FixturePath.new(
|
self.view_paths = [ActionView::FixtureTemplate::FixturePath.new(
|
||||||
"layouts/hello.erb" => "With String <%= yield %>",
|
"layouts/hello.erb" => "With String <%= yield %>",
|
||||||
|
|
|
@ -26,16 +26,14 @@ require 'rack/test'
|
||||||
|
|
||||||
module ActionController
|
module ActionController
|
||||||
class Base2 < AbstractBase
|
class Base2 < AbstractBase
|
||||||
include AbstractController::Callbacks
|
use AbstractController::Callbacks
|
||||||
include AbstractController::Renderer
|
use AbstractController::Helpers
|
||||||
include AbstractController::Helpers
|
use AbstractController::Logger
|
||||||
include AbstractController::Layouts
|
|
||||||
include AbstractController::Logger
|
use ActionController::HideActions
|
||||||
|
use ActionController::UrlFor
|
||||||
include ActionController::HideActions
|
use ActionController::Renderer
|
||||||
include ActionController::UrlFor
|
use ActionController::Layouts
|
||||||
include ActionController::Layouts
|
|
||||||
include ActionController::Renderer
|
|
||||||
|
|
||||||
def self.inherited(klass)
|
def self.inherited(klass)
|
||||||
::ActionController::Base2.subclasses << klass.to_s
|
::ActionController::Base2.subclasses << klass.to_s
|
||||||
|
|
|
@ -8,6 +8,7 @@ require 'active_support/core_ext/module/loading'
|
||||||
require 'active_support/core_ext/module/aliasing'
|
require 'active_support/core_ext/module/aliasing'
|
||||||
require 'active_support/core_ext/module/model_naming'
|
require 'active_support/core_ext/module/model_naming'
|
||||||
require 'active_support/core_ext/module/synchronization'
|
require 'active_support/core_ext/module/synchronization'
|
||||||
|
require 'active_support/core_ext/module/setup'
|
||||||
|
|
||||||
module ActiveSupport
|
module ActiveSupport
|
||||||
module CoreExtensions
|
module CoreExtensions
|
||||||
|
|
26
activesupport/lib/active_support/core_ext/module/setup.rb
Normal file
26
activesupport/lib/active_support/core_ext/module/setup.rb
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
class Module
|
||||||
|
attr_accessor :_setup_block
|
||||||
|
attr_accessor :_dependencies
|
||||||
|
|
||||||
|
def setup(&blk)
|
||||||
|
@_setup_block = blk
|
||||||
|
end
|
||||||
|
|
||||||
|
def use(mod)
|
||||||
|
return if self < mod
|
||||||
|
|
||||||
|
(mod._dependencies || []).each do |dep|
|
||||||
|
use dep
|
||||||
|
end
|
||||||
|
# raise "Circular dependencies" if self < mod
|
||||||
|
include mod
|
||||||
|
extend mod.const_get("ClassMethods") if mod.const_defined?("ClassMethods")
|
||||||
|
class_eval(&mod._setup_block) if mod._setup_block
|
||||||
|
end
|
||||||
|
|
||||||
|
def depends_on(mod)
|
||||||
|
return if self < mod
|
||||||
|
@_dependencies ||= []
|
||||||
|
@_dependencies << mod
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue