diff options
Diffstat (limited to 'actionpack/lib/action_controller/abstract')
-rw-r--r-- | actionpack/lib/action_controller/abstract/base.rb | 159 | ||||
-rw-r--r-- | actionpack/lib/action_controller/abstract/benchmarker.rb | 38 | ||||
-rw-r--r-- | actionpack/lib/action_controller/abstract/callbacks.rb | 115 | ||||
-rw-r--r-- | actionpack/lib/action_controller/abstract/exceptions.rb | 12 | ||||
-rw-r--r-- | actionpack/lib/action_controller/abstract/helpers.rb | 82 | ||||
-rw-r--r-- | actionpack/lib/action_controller/abstract/layouts.rb | 164 | ||||
-rw-r--r-- | actionpack/lib/action_controller/abstract/logger.rb | 52 | ||||
-rw-r--r-- | actionpack/lib/action_controller/abstract/renderer.rb | 156 |
8 files changed, 0 insertions, 778 deletions
diff --git a/actionpack/lib/action_controller/abstract/base.rb b/actionpack/lib/action_controller/abstract/base.rb deleted file mode 100644 index ca00e66349..0000000000 --- a/actionpack/lib/action_controller/abstract/base.rb +++ /dev/null @@ -1,159 +0,0 @@ -module AbstractController - - class Base - attr_internal :response_body - attr_internal :action_name - - class << self - attr_reader :abstract - alias_method :abstract?, :abstract - - # Define a controller as abstract. See internal_methods for more - # details. - def abstract! - @abstract = true - end - - def inherited(klass) - ::AbstractController::Base.descendants << klass.to_s - super - end - - # A list of all descendents of AbstractController::Base. This is - # useful for initializers which need to add behavior to all controllers. - def descendants - @descendants ||= [] - end - - # A list of all internal methods for a controller. This finds the first - # abstract superclass of a controller, and gets a list of all public - # instance methods on that abstract class. Public instance methods of - # a controller would normally be considered action methods, so we - # are removing those methods on classes declared as abstract - # (ActionController::Http and ActionController::Base are defined - # as abstract) - def internal_methods - controller = self - controller = controller.superclass until controller.abstract? - controller.public_instance_methods(true) - end - - # The list of hidden actions to an empty Array. Defaults to an - # empty Array. This can be modified by other modules or subclasses - # to specify particular actions as hidden. - # - # ==== Returns - # Array[String]:: An array of method names that should not be - # considered actions. - def hidden_actions - [] - end - - # A list of method names that should be considered actions. This - # includes all public instance methods on a controller, less - # any internal methods (see #internal_methods), adding back in - # any methods that are internal, but still exist on the class - # itself. Finally, #hidden_actions are removed. - # - # ==== Returns - # Array[String]:: A list of all methods that should be considered - # actions. - def action_methods - @action_methods ||= - # All public instance methods of this class, including ancestors - public_instance_methods(true).map { |m| m.to_s }.to_set - - # Except for public instance methods of Base and its ancestors - internal_methods.map { |m| m.to_s } + - # Be sure to include shadowed public instance methods of this class - public_instance_methods(false).map { |m| m.to_s } - - # And always exclude explicitly hidden actions - hidden_actions - end - end - - abstract! - - # Calls the action going through the entire action dispatch stack. - # - # The actual method that is called is determined by calling - # #method_for_action. If no method can handle the action, then an - # ActionNotFound error is raised. - # - # ==== Returns - # self - def process(action) - @_action_name = action_name = action.to_s - - unless action_name = method_for_action(action_name) - raise ActionNotFound, "The action '#{action}' could not be found" - end - - process_action(action_name) - self - end - - private - # Returns true if the name can be considered an action. This can - # be overridden in subclasses to modify the semantics of what - # can be considered an action. - # - # ==== Parameters - # name<String>:: The name of an action to be tested - # - # ==== Returns - # TrueClass, FalseClass - def action_method?(name) - self.class.action_methods.include?(name) - end - - # Call the action. Override this in a subclass to modify the - # behavior around processing an action. This, and not #process, - # is the intended way to override action dispatching. - def process_action(method_name) - send_action(method_name) - end - - # Actually call the method associated with the action. Override - # this method if you wish to change how action methods are called, - # not to add additional behavior around it. For example, you would - # override #send_action if you want to inject arguments into the - # method. - alias send_action send - - # If the action name was not found, but a method called "action_missing" - # was found, #method_for_action will return "_handle_action_missing". - # This method calls #action_missing with the current action name. - def _handle_action_missing - action_missing(@_action_name) - end - - # Takes an action name and returns the name of the method that will - # handle the action. In normal cases, this method returns the same - # name as it receives. By default, if #method_for_action receives - # a name that is not an action, it will look for an #action_missing - # method and return "_handle_action_missing" if one is found. - # - # Subclasses may override this method to add additional conditions - # that should be considered an action. For instance, an HTTP controller - # with a template matching the action name is considered to exist. - # - # If you override this method to handle additional cases, you may - # also provide a method (like _handle_method_missing) to handle - # the case. - # - # If none of these conditions are true, and method_for_action - # returns nil, an ActionNotFound exception will be raised. - # - # ==== Parameters - # action_name<String>:: An action name to find a method name for - # - # ==== Returns - # String:: The name of the method that handles the action - # nil:: No method name could be found. Raise ActionNotFound. - def method_for_action(action_name) - if action_method?(action_name) then action_name - elsif respond_to?(:action_missing, true) then "_handle_action_missing" - end - end - end -end diff --git a/actionpack/lib/action_controller/abstract/benchmarker.rb b/actionpack/lib/action_controller/abstract/benchmarker.rb deleted file mode 100644 index 58e9564c2f..0000000000 --- a/actionpack/lib/action_controller/abstract/benchmarker.rb +++ /dev/null @@ -1,38 +0,0 @@ -module AbstractController - module Benchmarker - extend ActiveSupport::Concern - - include Logger - - module ClassMethods - # Execute the passed in block, timing the duration of the block in ms. - # - # ==== Parameters - # title<#to_s>:: The title of block to benchmark - # log_level<Integer>:: A valid log level. Defaults to Logger::DEBUG - # use_silence<TrueClass, FalseClass>:: Whether or not to silence the - # logger for the duration of the block. - # - # ==== Returns - # Object:: The result of the block - def benchmark(title, log_level = ::Logger::DEBUG, use_silence = true) - if logger && logger.level >= log_level - result = nil - ms = Benchmark.ms { result = use_silence ? silence { yield } : yield } - logger.add(log_level, "#{title} (#{('%.1f' % ms)}ms)") - result - else - yield - end - end - - # Silences the logger for the duration of the block. - def silence - old_logger_level, logger.level = logger.level, ::Logger::ERROR if logger - yield - ensure - logger.level = old_logger_level if logger - end - end - end -end diff --git a/actionpack/lib/action_controller/abstract/callbacks.rb b/actionpack/lib/action_controller/abstract/callbacks.rb deleted file mode 100644 index ea4b59466e..0000000000 --- a/actionpack/lib/action_controller/abstract/callbacks.rb +++ /dev/null @@ -1,115 +0,0 @@ -require "active_support/new_callbacks" - -module AbstractController - module Callbacks - extend ActiveSupport::Concern - - # Uses ActiveSupport::NewCallbacks as the base functionality. For - # more details on the whole callback system, read the documentation - # for ActiveSupport::NewCallbacks. - include ActiveSupport::NewCallbacks - - included do - define_callbacks :process_action, "response_body" - end - - # Override AbstractController::Base's process_action to run the - # process_action callbacks around the normal behavior. - def process_action(method_name) - _run_process_action_callbacks(method_name) do - super - end - end - - module ClassMethods - # If :only or :accept are used, convert the options into the - # primitive form (:per_key) used by ActiveSupport::Callbacks. - # The basic idea is that :only => :index gets converted to - # :if => proc {|c| c.action_name == "index" }, but that the - # proc is only evaluated once per action for the lifetime of - # a Rails process. - # - # ==== Options - # :only<#to_s>:: The callback should be run only for this action - # :except<#to_s>:: The callback should be run for all actions - # except this action - def _normalize_callback_options(options) - if only = options[:only] - only = Array(only).map {|o| "action_name == '#{o}'"}.join(" || ") - options[:per_key] = {:if => only} - end - if except = options[:except] - except = Array(except).map {|e| "action_name == '#{e}'"}.join(" || ") - options[:per_key] = {:unless => except} - end - end - - # Skip before, after, and around filters matching any of the names - # - # ==== Parameters - # *names<Object>:: A list of valid names that could be used for - # callbacks. Note that skipping uses Ruby equality, so it's - # impossible to skip a callback defined using an anonymous proc - # using #skip_filter - def skip_filter(*names, &blk) - skip_before_filter(*names) - skip_after_filter(*names) - skip_around_filter(*names) - end - - # Take callback names and an optional callback proc, normalize them, - # then call the block with each callback. This allows us to abstract - # the normalization across several methods that use it. - # - # ==== Parameters - # callbacks<Array[*Object, Hash]>:: A list of callbacks, with an optional - # options hash as the last parameter. - # block<Proc>:: A proc that should be added to the callbacks. - # - # ==== Block Parameters - # name<Symbol>:: The callback to be added - # options<Hash>:: A list of options to be used when adding the callback - def _insert_callbacks(callbacks, block) - options = callbacks.last.is_a?(Hash) ? callbacks.pop : {} - _normalize_callback_options(options) - callbacks.push(block) if block - callbacks.each do |callback| - yield callback, options - end - end - - # set up before_filter, prepend_before_filter, skip_before_filter, etc. - # for each of before, after, and around. - [:before, :after, :around].each do |filter| - class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 - # Append a before, after or around filter. See _insert_callbacks - # for details on the allowed parameters. - def #{filter}_filter(*names, &blk) - _insert_callbacks(names, blk) do |name, options| - set_callback(:process_action, :#{filter}, name, options) - end - end - - # Prepend a before, after or around filter. See _insert_callbacks - # for details on the allowed parameters. - def prepend_#{filter}_filter(*names, &blk) - _insert_callbacks(names, blk) do |name, options| - set_callback(:process_action, :#{filter}, name, options.merge(:prepend => true)) - end - end - - # Skip a before, after or around filter. See _insert_callbacks - # for details on the allowed parameters. - def skip_#{filter}_filter(*names, &blk) - _insert_callbacks(names, blk) do |name, options| - skip_callback(:process_action, :#{filter}, name, options) - end - end - - # *_filter is the same as append_*_filter - alias_method :append_#{filter}_filter, :#{filter}_filter - RUBY_EVAL - end - end - end -end diff --git a/actionpack/lib/action_controller/abstract/exceptions.rb b/actionpack/lib/action_controller/abstract/exceptions.rb deleted file mode 100644 index b671516de1..0000000000 --- a/actionpack/lib/action_controller/abstract/exceptions.rb +++ /dev/null @@ -1,12 +0,0 @@ -module AbstractController - class Error < StandardError; end - class ActionNotFound < StandardError; end - - class DoubleRenderError < Error - DEFAULT_MESSAGE = "Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like \"redirect_to(...) and return\"." - - def initialize(message = nil) - super(message || DEFAULT_MESSAGE) - end - end -end diff --git a/actionpack/lib/action_controller/abstract/helpers.rb b/actionpack/lib/action_controller/abstract/helpers.rb deleted file mode 100644 index 5efa37fde3..0000000000 --- a/actionpack/lib/action_controller/abstract/helpers.rb +++ /dev/null @@ -1,82 +0,0 @@ -module AbstractController - module Helpers - extend ActiveSupport::Concern - - include Renderer - - included do - extlib_inheritable_accessor(:_helpers) { Module.new } - end - - module ClassMethods - # When a class is inherited, wrap its helper module in a new module. - # This ensures that the parent class's module can be changed - # independently of the child class's. - def inherited(klass) - helpers = _helpers - klass._helpers = Module.new { include helpers } - - super - end - - # Declare a controller method as a helper. For example, the following - # makes the +current_user+ controller method available to the view: - # class ApplicationController < ActionController::Base - # helper_method :current_user, :logged_in? - # - # def current_user - # @current_user ||= User.find_by_id(session[:user]) - # end - # - # def logged_in? - # current_user != nil - # end - # end - # - # In a view: - # <% if logged_in? -%>Welcome, <%= current_user.name %><% end -%> - # - # ==== Parameters - # meths<Array[#to_s]>:: The name of a method on the controller - # to be made available on the view. - def helper_method(*meths) - meths.flatten.each do |meth| - _helpers.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1 - def #{meth}(*args, &blk) - controller.send(%(#{meth}), *args, &blk) - end - ruby_eval - end - end - - # Make a number of helper modules part of this class' default - # helpers. - # - # ==== Parameters - # *args<Array[Module]>:: Modules to be included - # block<Block>:: Evalulate the block in the context - # of the helper module. Any methods defined in the block - # will be helpers. - def helper(*args, &block) - args.flatten.each do |arg| - case arg - when Module - add_template_helper(arg) - end - end - _helpers.module_eval(&block) if block_given? - end - - private - # Makes all the (instance) methods in the helper module available to templates - # rendered through this controller. - # - # ==== Parameters - # mod<Module>:: The module to include into the current helper module - # for the class - def add_template_helper(mod) - _helpers.module_eval { include mod } - end - end - end -end diff --git a/actionpack/lib/action_controller/abstract/layouts.rb b/actionpack/lib/action_controller/abstract/layouts.rb deleted file mode 100644 index f021dd8b62..0000000000 --- a/actionpack/lib/action_controller/abstract/layouts.rb +++ /dev/null @@ -1,164 +0,0 @@ -module AbstractController - module Layouts - extend ActiveSupport::Concern - - include Renderer - - included do - extlib_inheritable_accessor(:_layout_conditions) { Hash.new } - _write_layout_method - end - - module ClassMethods - def inherited(klass) - super - klass._write_layout_method - end - - # Specify the layout to use for this class. - # - # If the specified layout is a: - # String:: the String is the template name - # Symbol:: call the method specified by the symbol, which will return - # the template name - # false:: There is no layout - # true:: raise an ArgumentError - # - # ==== Parameters - # layout<String, Symbol, false)>:: The layout to use. - # - # ==== Options (conditions) - # :only<#to_s, Array[#to_s]>:: A list of actions to apply this layout to. - # :except<#to_s, Array[#to_s]>:: Apply this layout to all actions but this one - def layout(layout, conditions = {}) - conditions.each {|k, v| conditions[k] = Array(v).map {|a| a.to_s} } - self._layout_conditions = conditions - - @_layout = layout || false # Converts nil to false - _write_layout_method - end - - # If no layout is supplied, look for a template named the return - # value of this method. - # - # ==== Returns - # String:: A template name - def _implied_layout_name - name.underscore - end - - # Takes the specified layout and creates a _layout method to be called - # by _default_layout - # - # If there is no explicit layout specified: - # If a layout is found in the view paths with the controller's - # name, return that string. Otherwise, use the superclass' - # layout (which might also be implied) - def _write_layout_method - case @_layout - when String - self.class_eval %{def _layout(details) #{@_layout.inspect} end} - when Symbol - self.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1 - def _layout(details) - #{@_layout}.tap do |layout| - unless layout.is_a?(String) || !layout - raise ArgumentError, "Your layout method :#{@_layout} returned \#{layout}. It " \ - "should have returned a String, false, or nil" - end - end - end - ruby_eval - when false - self.class_eval %{def _layout(details) end} - when true - raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil" - when nil - self.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1 - def _layout(details) - if view_paths.find_by_parts?("#{_implied_layout_name}", details, "layouts") - "#{_implied_layout_name}" - else - super - end - end - ruby_eval - end - self.class_eval { private :_layout } - end - end - - private - # This will be overwritten by _write_layout_method - def _layout(details) end - - # Determine the layout for a given name and details. - # - # ==== Parameters - # name<String>:: The name of the template - # details<Hash{Symbol => Object}>:: A list of details to restrict - # the lookup to. By default, layout lookup is limited to the - # formats specified for the current request. - def _layout_for_name(name, details = {:formats => formats}) - name && _find_layout(name, details) - end - - # Take in the name and details and find a Template. - # - # ==== Parameters - # name<String>:: The name of the template to retrieve - # details<Hash>:: A list of details to restrict the search by. This - # might include details like the format or locale of the template. - # - # ==== Returns - # Template:: A template object matching the name and details - def _find_layout(name, details) - # TODO: Make prefix actually part of details in ViewPath#find_by_parts - prefix = details.key?(:prefix) ? details.delete(:prefix) : "layouts" - view_paths.find_by_parts(name, details, prefix) - end - - # Returns the default layout for this controller and a given set of details. - # Optionally raises an exception if the layout could not be found. - # - # ==== Parameters - # details<Hash>:: A list of details to restrict the search by. This - # might include details like the format or locale of the template. - # require_layout<Boolean>:: If this is true, raise an ArgumentError - # with details about the fact that the exception could not be - # found (defaults to false) - # - # ==== Returns - # Template:: The template object for the default layout (or nil) - def _default_layout(details, require_layout = false) - if require_layout && _action_has_layout? && !_layout(details) - raise ArgumentError, - "There was no default layout for #{self.class} in #{view_paths.inspect}" - end - - begin - _layout_for_name(_layout(details), details) if _action_has_layout? - rescue NameError => e - raise NoMethodError, - "You specified #{@_layout.inspect} as the layout, but no such method was found" - end - end - - # Determines whether the current action has a layout by checking the - # action name against the :only and :except conditions set on the - # layout. - # - # ==== Returns - # Boolean:: True if the action has a layout, false otherwise. - def _action_has_layout? - conditions = _layout_conditions - if only = conditions[:only] - only.include?(action_name) - elsif except = conditions[:except] - !except.include?(action_name) - else - true - end - end - end -end diff --git a/actionpack/lib/action_controller/abstract/logger.rb b/actionpack/lib/action_controller/abstract/logger.rb deleted file mode 100644 index fd33bd2ddd..0000000000 --- a/actionpack/lib/action_controller/abstract/logger.rb +++ /dev/null @@ -1,52 +0,0 @@ -require 'active_support/core_ext/logger' - -module AbstractController - module Logger - extend ActiveSupport::Concern - - # A class that allows you to defer expensive processing - # until the logger actually tries to log. Otherwise, you are - # forced to do the processing in advance, and send the - # entire processed String to the logger, which might - # just discard the String if the log level is too low. - # - # TODO: Require that Rails loggers accept a block. - class DelayedLog - def initialize(&blk) - @blk = blk - end - - def to_s - @blk.call - end - alias to_str to_s - end - - included do - cattr_accessor :logger - end - - # Override process_action in the AbstractController::Base - # to log details about the method. - def process_action(action) - super - - if logger - log = DelayedLog.new do - "\n\nProcessing #{self.class.name}\##{action_name} " \ - "to #{request.formats} " \ - "(for #{request_origin}) [#{request.method.to_s.upcase}]" - end - - logger.info(log) - end - end - - private - def request_origin - # this *needs* to be cached! - # otherwise you'd get different results if calling it more than once - @request_origin ||= "#{request.remote_ip} at #{Time.now.to_s(:db)}" - end - end -end diff --git a/actionpack/lib/action_controller/abstract/renderer.rb b/actionpack/lib/action_controller/abstract/renderer.rb deleted file mode 100644 index 41b7d47458..0000000000 --- a/actionpack/lib/action_controller/abstract/renderer.rb +++ /dev/null @@ -1,156 +0,0 @@ -require "action_controller/abstract/logger" - -module AbstractController - module Renderer - extend ActiveSupport::Concern - - include AbstractController::Logger - - included do - attr_internal :formats - - extlib_inheritable_accessor :_view_paths - - self._view_paths ||= ActionView::PathSet.new - end - - # An instance of a view class. The default view class is ActionView::Base - # - # The view class must have the following methods: - # View.for_controller[controller] Create a new ActionView instance for a - # controller - # View#_render_partial_from_controller[options] - # - responsible for setting options[:_template] - # - Returns String with the rendered partial - # options<Hash>:: see _render_partial in ActionView::Base - # View#_render_template_from_controller[template, layout, options, partial] - # - Returns String with the rendered template - # template<ActionView::Template>:: The template to render - # layout<ActionView::Template>:: The layout to render around the template - # options<Hash>:: See _render_template_with_layout in ActionView::Base - # partial<Boolean>:: Whether or not the template to render is a partial - # - # Override this method in a to change the default behavior. - def _action_view - @_action_view ||= ActionView::Base.for_controller(self) - end - - # Mostly abstracts the fact that calling render twice is a DoubleRenderError. - # Delegates render_to_body and sticks the result in self.response_body. - def render(*args) - if response_body - raise AbstractController::DoubleRenderError, "OMG" - end - - self.response_body = render_to_body(*args) - end - - # Raw rendering of a template to a Rack-compatible body. - # - # ==== Options - # _partial_object<Object>:: The object that is being rendered. If this - # exists, we are in the special case of rendering an object as a partial. - # - # :api: plugin - def render_to_body(options = {}) - # TODO: Refactor so we can just use the normal template logic for this - if options[:_partial_object] - _action_view._render_partial_from_controller(options) - else - _determine_template(options) - _render_template(options) - end - end - - # Raw rendering of a template to a string. Just convert the results of - # render_to_body into a String. - # - # :api: plugin - def render_to_string(options = {}) - AbstractController::Renderer.body_to_s(render_to_body(options)) - end - - # Renders the template from an object. - # - # ==== Options - # _template<ActionView::Template>:: The template to render - # _layout<ActionView::Template>:: The layout to wrap the template in (optional) - # _partial<TrueClass, FalseClass>:: Whether or not the template to be rendered is a partial - def _render_template(options) - _action_view._render_template_from_controller(options[:_template], options[:_layout], options, options[:_partial]) - end - - # The list of view paths for this controller. See ActionView::ViewPathSet for - # more details about writing custom view paths. - def view_paths - _view_paths - end - - # Return a string representation of a Rack-compatible response body. - def self.body_to_s(body) - if body.respond_to?(:to_str) - body - else - strings = [] - body.each { |part| strings << part.to_s } - body.close if body.respond_to?(:close) - strings.join - end - end - - private - # Take in a set of options and determine the template to render - # - # ==== Options - # _template<ActionView::Template>:: If this is provided, the search is over - # _template_name<#to_s>:: The name of the template to look up. Otherwise, - # use the current action name. - # _prefix<String>:: The prefix to look inside of. In a file system, this corresponds - # to a directory. - # _partial<TrueClass, FalseClass>:: Whether or not the file to look up is a partial - def _determine_template(options) - name = (options[:_template_name] || action_name).to_s - - options[:_template] ||= view_paths.find_by_parts( - name, { :formats => formats }, options[:_prefix], options[:_partial] - ) - end - - module ClassMethods - # Append a path to the list of view paths for this controller. - # - # ==== Parameters - # path<String, ViewPath>:: If a String is provided, it gets converted into - # the default view path. You may also provide a custom view path - # (see ActionView::ViewPathSet for more information) - def append_view_path(path) - self.view_paths << path - end - - # Prepend a path to the list of view paths for this controller. - # - # ==== Parameters - # path<String, ViewPath>:: If a String is provided, it gets converted into - # the default view path. You may also provide a custom view path - # (see ActionView::ViewPathSet for more information) - def prepend_view_path(path) - self.view_paths.unshift(path) - end - - # A list of all of the default view paths for this controller. - def view_paths - self._view_paths - end - - # Set the view paths. - # - # ==== Parameters - # paths<ViewPathSet, Object>:: If a ViewPathSet is provided, use that; - # otherwise, process the parameter into a ViewPathSet. - def view_paths=(paths) - self._view_paths = paths.is_a?(ActionView::PathSet) ? - paths : ActionView::Base.process_view_paths(paths) - end - end - end -end |