aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/abstract_controller
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib/abstract_controller')
-rw-r--r--actionpack/lib/abstract_controller/asset_paths.rb12
-rw-r--r--actionpack/lib/abstract_controller/base.rb267
-rw-r--r--actionpack/lib/abstract_controller/caching.rb66
-rw-r--r--actionpack/lib/abstract_controller/caching/fragments.rb166
-rw-r--r--actionpack/lib/abstract_controller/callbacks.rb212
-rw-r--r--actionpack/lib/abstract_controller/collector.rb43
-rw-r--r--actionpack/lib/abstract_controller/error.rb6
-rw-r--r--actionpack/lib/abstract_controller/helpers.rb194
-rw-r--r--actionpack/lib/abstract_controller/logger.rb14
-rw-r--r--actionpack/lib/abstract_controller/railties/routes_helpers.rb20
-rw-r--r--actionpack/lib/abstract_controller/rendering.rb136
-rw-r--r--actionpack/lib/abstract_controller/translation.rb31
-rw-r--r--actionpack/lib/abstract_controller/url_for.rb35
13 files changed, 1202 insertions, 0 deletions
diff --git a/actionpack/lib/abstract_controller/asset_paths.rb b/actionpack/lib/abstract_controller/asset_paths.rb
new file mode 100644
index 0000000000..d6ee84b87b
--- /dev/null
+++ b/actionpack/lib/abstract_controller/asset_paths.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+module AbstractController
+ module AssetPaths #:nodoc:
+ extend ActiveSupport::Concern
+
+ included do
+ config_accessor :asset_host, :assets_dir, :javascripts_dir,
+ :stylesheets_dir, :default_asset_host_protocol, :relative_url_root
+ end
+ end
+end
diff --git a/actionpack/lib/abstract_controller/base.rb b/actionpack/lib/abstract_controller/base.rb
new file mode 100644
index 0000000000..3761054bb7
--- /dev/null
+++ b/actionpack/lib/abstract_controller/base.rb
@@ -0,0 +1,267 @@
+# frozen_string_literal: true
+
+require_relative "error"
+require "active_support/configurable"
+require "active_support/descendants_tracker"
+require "active_support/core_ext/module/anonymous"
+require "active_support/core_ext/module/attr_internal"
+
+module AbstractController
+ # Raised when a non-existing controller action is triggered.
+ class ActionNotFound < StandardError
+ end
+
+ # AbstractController::Base is a low-level API. Nobody should be
+ # using it directly, and subclasses (like ActionController::Base) are
+ # expected to provide their own +render+ method, since rendering means
+ # different things depending on the context.
+ class Base
+ ##
+ # Returns the body of the HTTP response sent by the controller.
+ attr_internal :response_body
+
+ ##
+ # Returns the name of the action this controller is processing.
+ attr_internal :action_name
+
+ ##
+ # Returns the formats that can be processed by the controller.
+ attr_internal :formats
+
+ include ActiveSupport::Configurable
+ extend ActiveSupport::DescendantsTracker
+
+ 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) # :nodoc:
+ # Define the abstract ivar on subclasses so that we don't get
+ # uninitialized ivar warnings
+ unless klass.instance_variable_defined?(:@abstract)
+ klass.instance_variable_set(:@abstract, false)
+ end
+ super
+ 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 methods
+ # declared on abstract classes are being removed.
+ # (<tt>ActionController::Metal</tt> and ActionController::Base are defined as abstract)
+ def internal_methods
+ controller = self
+
+ controller = controller.superclass until controller.abstract?
+ controller.public_instance_methods(true)
+ 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.
+ #
+ # ==== Returns
+ # * <tt>Set</tt> - A set of all methods that should be considered actions.
+ def action_methods
+ @action_methods ||= begin
+ # All public instance methods of this class, including ancestors
+ methods = (public_instance_methods(true) -
+ # Except for public instance methods of Base and its ancestors
+ internal_methods +
+ # Be sure to include shadowed public instance methods of this class
+ public_instance_methods(false)).uniq.map(&:to_s)
+
+ methods.to_set
+ end
+ end
+
+ # action_methods are cached and there is sometimes a need to refresh
+ # them. ::clear_action_methods! allows you to do that, so next time
+ # you run action_methods, they will be recalculated.
+ def clear_action_methods!
+ @action_methods = nil
+ end
+
+ # Returns the full controller name, underscored, without the ending Controller.
+ #
+ # class MyApp::MyPostsController < AbstractController::Base
+ #
+ # end
+ #
+ # MyApp::MyPostsController.controller_path # => "my_app/my_posts"
+ #
+ # ==== Returns
+ # * <tt>String</tt>
+ def controller_path
+ @controller_path ||= name.sub(/Controller$/, "".freeze).underscore unless anonymous?
+ end
+
+ # Refresh the cached action_methods when a new action_method is added.
+ def method_added(name)
+ super
+ clear_action_methods!
+ 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
+ # AbstractController::ActionNotFound error is raised.
+ #
+ # ==== Returns
+ # * <tt>self</tt>
+ def process(action, *args)
+ @_action_name = action.to_s
+
+ unless action_name = _find_action_name(@_action_name)
+ raise ActionNotFound, "The action '#{action}' could not be found for #{self.class.name}"
+ end
+
+ @_response_body = nil
+
+ process_action(action_name, *args)
+ end
+
+ # Delegates to the class' ::controller_path
+ def controller_path
+ self.class.controller_path
+ end
+
+ # Delegates to the class' ::action_methods
+ def action_methods
+ self.class.action_methods
+ end
+
+ # Returns true if a method for the action is available and
+ # can be dispatched, false otherwise.
+ #
+ # Notice that <tt>action_methods.include?("foo")</tt> may return
+ # false and <tt>available_action?("foo")</tt> returns true because
+ # this method considers actions that are also available
+ # through other means, for example, implicit render ones.
+ #
+ # ==== Parameters
+ # * <tt>action_name</tt> - The name of an action to be tested
+ def available_action?(action_name)
+ _find_action_name(action_name)
+ end
+
+ # Tests if a response body is set. Used to determine if the
+ # +process_action+ callback needs to be terminated in
+ # +AbstractController::Callbacks+.
+ def performed?
+ response_body
+ end
+
+ # Returns true if the given controller is capable of rendering
+ # a path. A subclass of +AbstractController::Base+
+ # may return false. An Email controller for example does not
+ # support paths, only full URLs.
+ def self.supports_path?
+ true
+ end
+
+ private
+
+ # Returns true if the name can be considered an action because
+ # it has a method defined in the controller.
+ #
+ # ==== Parameters
+ # * <tt>name</tt> - The name of an action to be tested
+ #
+ # :api: private
+ 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.
+ #
+ # Notice that the first argument is the method to be dispatched
+ # which is *not* necessarily the same as the action name.
+ def process_action(method_name, *args)
+ send_action(method_name, *args)
+ 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(*args)
+ action_missing(@_action_name, *args)
+ end
+
+ # Takes an action name and returns the name of the method that will
+ # handle the action.
+ #
+ # It checks if the action name is valid and returns false otherwise.
+ #
+ # See method_for_action for more information.
+ #
+ # ==== Parameters
+ # * <tt>action_name</tt> - An action name to find a method name for
+ #
+ # ==== Returns
+ # * <tt>string</tt> - The name of the method that handles the action
+ # * false - No valid method name could be found.
+ # Raise +AbstractController::ActionNotFound+.
+ def _find_action_name(action_name)
+ _valid_action_name?(action_name) && method_for_action(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 +AbstractController::ActionNotFound+ exception will be raised.
+ #
+ # ==== Parameters
+ # * <tt>action_name</tt> - An action name to find a method name for
+ #
+ # ==== Returns
+ # * <tt>string</tt> - The name of the method that handles the action
+ # * <tt>nil</tt> - No method name could be found.
+ def method_for_action(action_name)
+ if action_method?(action_name)
+ action_name
+ elsif respond_to?(:action_missing, true)
+ "_handle_action_missing"
+ end
+ end
+
+ # Checks if the action name is valid and returns false otherwise.
+ def _valid_action_name?(action_name)
+ !action_name.to_s.include? File::SEPARATOR
+ end
+ end
+end
diff --git a/actionpack/lib/abstract_controller/caching.rb b/actionpack/lib/abstract_controller/caching.rb
new file mode 100644
index 0000000000..ce6b757c3c
--- /dev/null
+++ b/actionpack/lib/abstract_controller/caching.rb
@@ -0,0 +1,66 @@
+# frozen_string_literal: true
+
+module AbstractController
+ module Caching
+ extend ActiveSupport::Concern
+ extend ActiveSupport::Autoload
+
+ eager_autoload do
+ autoload :Fragments
+ end
+
+ module ConfigMethods
+ def cache_store
+ config.cache_store
+ end
+
+ def cache_store=(store)
+ config.cache_store = ActiveSupport::Cache.lookup_store(store)
+ end
+
+ private
+ def cache_configured?
+ perform_caching && cache_store
+ end
+ end
+
+ include ConfigMethods
+ include AbstractController::Caching::Fragments
+
+ included do
+ extend ConfigMethods
+
+ config_accessor :default_static_extension
+ self.default_static_extension ||= ".html"
+
+ config_accessor :perform_caching
+ self.perform_caching = true if perform_caching.nil?
+
+ config_accessor :enable_fragment_cache_logging
+ self.enable_fragment_cache_logging = false
+
+ class_attribute :_view_cache_dependencies, default: []
+ helper_method :view_cache_dependencies if respond_to?(:helper_method)
+ end
+
+ module ClassMethods
+ def view_cache_dependency(&dependency)
+ self._view_cache_dependencies += [dependency]
+ end
+ end
+
+ def view_cache_dependencies
+ self.class._view_cache_dependencies.map { |dep| instance_exec(&dep) }.compact
+ end
+
+ private
+ # Convenience accessor.
+ def cache(key, options = {}, &block) # :doc:
+ if cache_configured?
+ cache_store.fetch(ActiveSupport::Cache.expand_cache_key(key, :controller), options, &block)
+ else
+ yield
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/abstract_controller/caching/fragments.rb b/actionpack/lib/abstract_controller/caching/fragments.rb
new file mode 100644
index 0000000000..f99b0830b2
--- /dev/null
+++ b/actionpack/lib/abstract_controller/caching/fragments.rb
@@ -0,0 +1,166 @@
+# frozen_string_literal: true
+
+module AbstractController
+ module Caching
+ # Fragment caching is used for caching various blocks within
+ # views without caching the entire action as a whole. This is
+ # useful when certain elements of an action change frequently or
+ # depend on complicated state while other parts rarely change or
+ # can be shared amongst multiple parties. The caching is done using
+ # the +cache+ helper available in the Action View. See
+ # ActionView::Helpers::CacheHelper for more information.
+ #
+ # While it's strongly recommended that you use key-based cache
+ # expiration (see links in CacheHelper for more information),
+ # it is also possible to manually expire caches. For example:
+ #
+ # expire_fragment('name_of_cache')
+ module Fragments
+ extend ActiveSupport::Concern
+
+ included do
+ if respond_to?(:class_attribute)
+ class_attribute :fragment_cache_keys
+ else
+ mattr_writer :fragment_cache_keys
+ end
+
+ self.fragment_cache_keys = []
+
+ if respond_to?(:helper_method)
+ helper_method :fragment_cache_key
+ helper_method :combined_fragment_cache_key
+ end
+ end
+
+ module ClassMethods
+ # Allows you to specify controller-wide key prefixes for
+ # cache fragments. Pass either a constant +value+, or a block
+ # which computes a value each time a cache key is generated.
+ #
+ # For example, you may want to prefix all fragment cache keys
+ # with a global version identifier, so you can easily
+ # invalidate all caches.
+ #
+ # class ApplicationController
+ # fragment_cache_key "v1"
+ # end
+ #
+ # When it's time to invalidate all fragments, simply change
+ # the string constant. Or, progressively roll out the cache
+ # invalidation using a computed value:
+ #
+ # class ApplicationController
+ # fragment_cache_key do
+ # @account.id.odd? ? "v1" : "v2"
+ # end
+ # end
+ def fragment_cache_key(value = nil, &key)
+ self.fragment_cache_keys += [key || -> { value }]
+ end
+ end
+
+ # Given a key (as described in +expire_fragment+), returns
+ # a key suitable for use in reading, writing, or expiring a
+ # cached fragment. All keys begin with <tt>views/</tt>,
+ # followed by any controller-wide key prefix values, ending
+ # with the specified +key+ value. The key is expanded using
+ # ActiveSupport::Cache.expand_cache_key.
+ def fragment_cache_key(key)
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
+ Calling fragment_cache_key directly is deprecated and will be removed in Rails 6.0.
+ All fragment accessors now use the combined_fragment_cache_key method that retains the key as an array,
+ such that the caching stores can interrogate the parts for cache versions used in
+ recyclable cache keys.
+ MSG
+
+ head = self.class.fragment_cache_keys.map { |k| instance_exec(&k) }
+ tail = key.is_a?(Hash) ? url_for(key).split("://").last : key
+ ActiveSupport::Cache.expand_cache_key([*head, *tail], :views)
+ end
+
+ # Given a key (as described in +expire_fragment+), returns
+ # a key array suitable for use in reading, writing, or expiring a
+ # cached fragment. All keys begin with <tt>:views</tt>,
+ # followed by ENV["RAILS_CACHE_ID"] or ENV["RAILS_APP_VERSION"] if set,
+ # followed by any controller-wide key prefix values, ending
+ # with the specified +key+ value.
+ def combined_fragment_cache_key(key)
+ head = self.class.fragment_cache_keys.map { |k| instance_exec(&k) }
+ tail = key.is_a?(Hash) ? url_for(key).split("://").last : key
+ [ :views, (ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]), *head, *tail ].compact
+ end
+
+ # Writes +content+ to the location signified by
+ # +key+ (see +expire_fragment+ for acceptable formats).
+ def write_fragment(key, content, options = nil)
+ return content unless cache_configured?
+
+ key = combined_fragment_cache_key(key)
+ instrument_fragment_cache :write_fragment, key do
+ content = content.to_str
+ cache_store.write(key, content, options)
+ end
+ content
+ end
+
+ # Reads a cached fragment from the location signified by +key+
+ # (see +expire_fragment+ for acceptable formats).
+ def read_fragment(key, options = nil)
+ return unless cache_configured?
+
+ key = combined_fragment_cache_key(key)
+ instrument_fragment_cache :read_fragment, key do
+ result = cache_store.read(key, options)
+ result.respond_to?(:html_safe) ? result.html_safe : result
+ end
+ end
+
+ # Check if a cached fragment from the location signified by
+ # +key+ exists (see +expire_fragment+ for acceptable formats).
+ def fragment_exist?(key, options = nil)
+ return unless cache_configured?
+ key = combined_fragment_cache_key(key)
+
+ instrument_fragment_cache :exist_fragment?, key do
+ cache_store.exist?(key, options)
+ end
+ end
+
+ # Removes fragments from the cache.
+ #
+ # +key+ can take one of three forms:
+ #
+ # * String - This would normally take the form of a path, like
+ # <tt>pages/45/notes</tt>.
+ # * Hash - Treated as an implicit call to +url_for+, like
+ # <tt>{ controller: 'pages', action: 'notes', id: 45}</tt>
+ # * Regexp - Will remove any fragment that matches, so
+ # <tt>%r{pages/\d*/notes}</tt> might remove all notes. Make sure you
+ # don't use anchors in the regex (<tt>^</tt> or <tt>$</tt>) because
+ # the actual filename matched looks like
+ # <tt>./cache/filename/path.cache</tt>. Note: Regexp expiration is
+ # only supported on caches that can iterate over all keys (unlike
+ # memcached).
+ #
+ # +options+ is passed through to the cache store's +delete+
+ # method (or <tt>delete_matched</tt>, for Regexp keys).
+ def expire_fragment(key, options = nil)
+ return unless cache_configured?
+ key = combined_fragment_cache_key(key) unless key.is_a?(Regexp)
+
+ instrument_fragment_cache :expire_fragment, key do
+ if key.is_a?(Regexp)
+ cache_store.delete_matched(key, options)
+ else
+ cache_store.delete(key, options)
+ end
+ end
+ end
+
+ def instrument_fragment_cache(name, key) # :nodoc:
+ ActiveSupport::Notifications.instrument("#{name}.#{instrument_name}", instrument_payload(key)) { yield }
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/abstract_controller/callbacks.rb b/actionpack/lib/abstract_controller/callbacks.rb
new file mode 100644
index 0000000000..715d043b4e
--- /dev/null
+++ b/actionpack/lib/abstract_controller/callbacks.rb
@@ -0,0 +1,212 @@
+# frozen_string_literal: true
+
+module AbstractController
+ # = Abstract Controller Callbacks
+ #
+ # Abstract Controller provides hooks during the life cycle of a controller action.
+ # Callbacks allow you to trigger logic during this cycle. Available callbacks are:
+ #
+ # * <tt>after_action</tt>
+ # * <tt>append_after_action</tt>
+ # * <tt>append_around_action</tt>
+ # * <tt>append_before_action</tt>
+ # * <tt>around_action</tt>
+ # * <tt>before_action</tt>
+ # * <tt>prepend_after_action</tt>
+ # * <tt>prepend_around_action</tt>
+ # * <tt>prepend_before_action</tt>
+ # * <tt>skip_after_action</tt>
+ # * <tt>skip_around_action</tt>
+ # * <tt>skip_before_action</tt>
+ #
+ # NOTE: Calling the same callback multiple times will overwrite previous callback definitions.
+ #
+ module Callbacks
+ extend ActiveSupport::Concern
+
+ # Uses ActiveSupport::Callbacks as the base functionality. For
+ # more details on the whole callback system, read the documentation
+ # for ActiveSupport::Callbacks.
+ include ActiveSupport::Callbacks
+
+ included do
+ define_callbacks :process_action,
+ terminator: ->(controller, result_lambda) { result_lambda.call; controller.performed? },
+ skip_after_callbacks_if_terminated: true
+ end
+
+ # Override AbstractController::Base's process_action to run the
+ # process_action callbacks around the normal behavior.
+ def process_action(*args)
+ run_callbacks(:process_action) do
+ super
+ end
+ end
+
+ module ClassMethods
+ # If +:only+ or +:except+ are used, convert the options into the
+ # +:if+ and +:unless+ options of ActiveSupport::Callbacks.
+ #
+ # The basic idea is that <tt>:only => :index</tt> gets converted to
+ # <tt>:if => proc {|c| c.action_name == "index" }</tt>.
+ #
+ # Note that <tt>:only</tt> has priority over <tt>:if</tt> in case they
+ # are used together.
+ #
+ # only: :index, if: -> { true } # the :if option will be ignored.
+ #
+ # Note that <tt>:if</tt> has priority over <tt>:except</tt> in case they
+ # are used together.
+ #
+ # except: :index, if: -> { true } # the :except option will be ignored.
+ #
+ # ==== Options
+ # * <tt>only</tt> - The callback should be run only for this action.
+ # * <tt>except</tt> - The callback should be run for all actions except this action.
+ def _normalize_callback_options(options)
+ _normalize_callback_option(options, :only, :if)
+ _normalize_callback_option(options, :except, :unless)
+ end
+
+ def _normalize_callback_option(options, from, to) # :nodoc:
+ if from = options[from]
+ _from = Array(from).map(&:to_s).to_set
+ from = proc { |c| _from.include? c.action_name }
+ options[to] = Array(options[to]).unshift(from)
+ end
+ 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
+ # * <tt>callbacks</tt> - An array of callbacks, with an optional
+ # options hash as the last parameter.
+ # * <tt>block</tt> - A proc that should be added to the callbacks.
+ #
+ # ==== Block Parameters
+ # * <tt>name</tt> - The callback to be added.
+ # * <tt>options</tt> - A hash of options to be used when adding the callback.
+ def _insert_callbacks(callbacks, block = nil)
+ options = callbacks.extract_options!
+ _normalize_callback_options(options)
+ callbacks.push(block) if block
+ callbacks.each do |callback|
+ yield callback, options
+ end
+ end
+
+ ##
+ # :method: before_action
+ #
+ # :call-seq: before_action(names, block)
+ #
+ # Append a callback before actions. See _insert_callbacks for parameter details.
+
+ ##
+ # :method: prepend_before_action
+ #
+ # :call-seq: prepend_before_action(names, block)
+ #
+ # Prepend a callback before actions. See _insert_callbacks for parameter details.
+
+ ##
+ # :method: skip_before_action
+ #
+ # :call-seq: skip_before_action(names)
+ #
+ # Skip a callback before actions. See _insert_callbacks for parameter details.
+
+ ##
+ # :method: append_before_action
+ #
+ # :call-seq: append_before_action(names, block)
+ #
+ # Append a callback before actions. See _insert_callbacks for parameter details.
+
+ ##
+ # :method: after_action
+ #
+ # :call-seq: after_action(names, block)
+ #
+ # Append a callback after actions. See _insert_callbacks for parameter details.
+
+ ##
+ # :method: prepend_after_action
+ #
+ # :call-seq: prepend_after_action(names, block)
+ #
+ # Prepend a callback after actions. See _insert_callbacks for parameter details.
+
+ ##
+ # :method: skip_after_action
+ #
+ # :call-seq: skip_after_action(names)
+ #
+ # Skip a callback after actions. See _insert_callbacks for parameter details.
+
+ ##
+ # :method: append_after_action
+ #
+ # :call-seq: append_after_action(names, block)
+ #
+ # Append a callback after actions. See _insert_callbacks for parameter details.
+
+ ##
+ # :method: around_action
+ #
+ # :call-seq: around_action(names, block)
+ #
+ # Append a callback around actions. See _insert_callbacks for parameter details.
+
+ ##
+ # :method: prepend_around_action
+ #
+ # :call-seq: prepend_around_action(names, block)
+ #
+ # Prepend a callback around actions. See _insert_callbacks for parameter details.
+
+ ##
+ # :method: skip_around_action
+ #
+ # :call-seq: skip_around_action(names)
+ #
+ # Skip a callback around actions. See _insert_callbacks for parameter details.
+
+ ##
+ # :method: append_around_action
+ #
+ # :call-seq: append_around_action(names, block)
+ #
+ # Append a callback around actions. See _insert_callbacks for parameter details.
+
+ # set up before_action, prepend_before_action, skip_before_action, etc.
+ # for each of before, after, and around.
+ [:before, :after, :around].each do |callback|
+ define_method "#{callback}_action" do |*names, &blk|
+ _insert_callbacks(names, blk) do |name, options|
+ set_callback(:process_action, callback, name, options)
+ end
+ end
+
+ define_method "prepend_#{callback}_action" do |*names, &blk|
+ _insert_callbacks(names, blk) do |name, options|
+ set_callback(:process_action, callback, name, options.merge(prepend: true))
+ end
+ end
+
+ # Skip a before, after or around callback. See _insert_callbacks
+ # for details on the allowed parameters.
+ define_method "skip_#{callback}_action" do |*names|
+ _insert_callbacks(names) do |name, options|
+ skip_callback(:process_action, callback, name, options)
+ end
+ end
+
+ # *_action is the same as append_*_action
+ alias_method :"append_#{callback}_action", :"#{callback}_action"
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/abstract_controller/collector.rb b/actionpack/lib/abstract_controller/collector.rb
new file mode 100644
index 0000000000..297ec5ca40
--- /dev/null
+++ b/actionpack/lib/abstract_controller/collector.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+require "action_dispatch/http/mime_type"
+
+module AbstractController
+ module Collector
+ def self.generate_method_for_mime(mime)
+ sym = mime.is_a?(Symbol) ? mime : mime.to_sym
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def #{sym}(*args, &block)
+ custom(Mime[:#{sym}], *args, &block)
+ end
+ RUBY
+ end
+
+ Mime::SET.each do |mime|
+ generate_method_for_mime(mime)
+ end
+
+ Mime::Type.register_callback do |mime|
+ generate_method_for_mime(mime) unless instance_methods.include?(mime.to_sym)
+ end
+
+ private
+
+ def method_missing(symbol, &block)
+ unless mime_constant = Mime[symbol]
+ raise NoMethodError, "To respond to a custom format, register it as a MIME type first: " \
+ "http://guides.rubyonrails.org/action_controller_overview.html#restful-downloads. " \
+ "If you meant to respond to a variant like :tablet or :phone, not a custom format, " \
+ "be sure to nest your variant response within a format response: " \
+ "format.html { |html| html.tablet { ... } }"
+ end
+
+ if Mime::SET.include?(mime_constant)
+ AbstractController::Collector.generate_method_for_mime(mime_constant)
+ send(symbol, &block)
+ else
+ super
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/abstract_controller/error.rb b/actionpack/lib/abstract_controller/error.rb
new file mode 100644
index 0000000000..89a54f072e
--- /dev/null
+++ b/actionpack/lib/abstract_controller/error.rb
@@ -0,0 +1,6 @@
+# frozen_string_literal: true
+
+module AbstractController
+ class Error < StandardError #:nodoc:
+ end
+end
diff --git a/actionpack/lib/abstract_controller/helpers.rb b/actionpack/lib/abstract_controller/helpers.rb
new file mode 100644
index 0000000000..35b462bc92
--- /dev/null
+++ b/actionpack/lib/abstract_controller/helpers.rb
@@ -0,0 +1,194 @@
+# frozen_string_literal: true
+
+require "active_support/dependencies"
+
+module AbstractController
+ module Helpers
+ extend ActiveSupport::Concern
+
+ included do
+ class_attribute :_helpers, default: Module.new
+ class_attribute :_helper_methods, default: Array.new
+ end
+
+ class MissingHelperError < LoadError
+ def initialize(error, path)
+ @error = error
+ @path = "helpers/#{path}.rb"
+ set_backtrace error.backtrace
+
+ if error.path =~ /^#{path}(\.rb)?$/
+ super("Missing helper file helpers/%s.rb" % path)
+ else
+ raise error
+ end
+ end
+ 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 }
+ klass.class_eval { default_helper_module! } unless klass.anonymous?
+ super
+ end
+
+ # Declare a controller method as a helper. For example, the following
+ # makes the +current_user+ and +logged_in?+ controller methods 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
+ # * <tt>method[, method]</tt> - A name or names of a method on the controller
+ # to be made available on the view.
+ def helper_method(*meths)
+ meths.flatten!
+ self._helper_methods += meths
+
+ meths.each do |meth|
+ _helpers.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1
+ def #{meth}(*args, &blk) # def current_user(*args, &blk)
+ controller.send(%(#{meth}), *args, &blk) # controller.send(:current_user, *args, &blk)
+ end # end
+ ruby_eval
+ end
+ end
+
+ # The +helper+ class method can take a series of helper module names, a block, or both.
+ #
+ # ==== Options
+ # * <tt>*args</tt> - Module, Symbol, String
+ # * <tt>block</tt> - A block defining helper methods
+ #
+ # When the argument is a module it will be included directly in the template class.
+ # helper FooHelper # => includes FooHelper
+ #
+ # When the argument is a string or symbol, the method will provide the "_helper" suffix, require the file
+ # and include the module in the template class. The second form illustrates how to include custom helpers
+ # when working with namespaced controllers, or other cases where the file containing the helper definition is not
+ # in one of Rails' standard load paths:
+ # helper :foo # => requires 'foo_helper' and includes FooHelper
+ # helper 'resources/foo' # => requires 'resources/foo_helper' and includes Resources::FooHelper
+ #
+ # Additionally, the +helper+ class method can receive and evaluate a block, making the methods defined available
+ # to the template.
+ #
+ # # One line
+ # helper { def hello() "Hello, world!" end }
+ #
+ # # Multi-line
+ # helper do
+ # def foo(bar)
+ # "#{bar} is the very best"
+ # end
+ # end
+ #
+ # Finally, all the above styles can be mixed together, and the +helper+ method can be invoked with a mix of
+ # +symbols+, +strings+, +modules+ and blocks.
+ #
+ # helper(:three, BlindHelper) { def mice() 'mice' end }
+ #
+ def helper(*args, &block)
+ modules_for_helpers(args).each do |mod|
+ add_template_helper(mod)
+ end
+
+ _helpers.module_eval(&block) if block_given?
+ end
+
+ # Clears up all existing helpers in this class, only keeping the helper
+ # with the same name as this class.
+ def clear_helpers
+ inherited_helper_methods = _helper_methods
+ self._helpers = Module.new
+ self._helper_methods = Array.new
+
+ inherited_helper_methods.each { |meth| helper_method meth }
+ default_helper_module! unless anonymous?
+ end
+
+ # Returns a list of modules, normalized from the acceptable kinds of
+ # helpers with the following behavior:
+ #
+ # String or Symbol:: :FooBar or "FooBar" becomes "foo_bar_helper",
+ # and "foo_bar_helper.rb" is loaded using require_dependency.
+ #
+ # Module:: No further processing
+ #
+ # After loading the appropriate files, the corresponding modules
+ # are returned.
+ #
+ # ==== Parameters
+ # * <tt>args</tt> - An array of helpers
+ #
+ # ==== Returns
+ # * <tt>Array</tt> - A normalized list of modules for the list of
+ # helpers provided.
+ def modules_for_helpers(args)
+ args.flatten.map! do |arg|
+ case arg
+ when String, Symbol
+ file_name = "#{arg.to_s.underscore}_helper"
+ begin
+ require_dependency(file_name)
+ rescue LoadError => e
+ raise AbstractController::Helpers::MissingHelperError.new(e, file_name)
+ end
+
+ mod_name = file_name.camelize
+ begin
+ mod_name.constantize
+ rescue LoadError
+ # dependencies.rb gives a similar error message but its wording is
+ # not as clear because it mentions autoloading. To the user all it
+ # matters is that a helper module couldn't be loaded, autoloading
+ # is an internal mechanism that should not leak.
+ raise NameError, "Couldn't find #{mod_name}, expected it to be defined in helpers/#{file_name}.rb"
+ end
+ when Module
+ arg
+ else
+ raise ArgumentError, "helper must be a String, Symbol, or Module"
+ end
+ end
+ end
+
+ private
+ # Makes all the (instance) methods in the helper module available to templates
+ # rendered through this controller.
+ #
+ # ==== Parameters
+ # * <tt>module</tt> - The module to include into the current helper module
+ # for the class
+ def add_template_helper(mod)
+ _helpers.module_eval { include mod }
+ end
+
+ def default_helper_module!
+ module_name = name.sub(/Controller$/, "".freeze)
+ module_path = module_name.underscore
+ helper module_path
+ rescue LoadError => e
+ raise e unless e.is_missing? "helpers/#{module_path}_helper"
+ rescue NameError => e
+ raise e unless e.missing_name? "#{module_name}Helper"
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/abstract_controller/logger.rb b/actionpack/lib/abstract_controller/logger.rb
new file mode 100644
index 0000000000..8d0acc1b5c
--- /dev/null
+++ b/actionpack/lib/abstract_controller/logger.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+require "active_support/benchmarkable"
+
+module AbstractController
+ module Logger #:nodoc:
+ extend ActiveSupport::Concern
+
+ included do
+ config_accessor :logger
+ include ActiveSupport::Benchmarkable
+ end
+ end
+end
diff --git a/actionpack/lib/abstract_controller/railties/routes_helpers.rb b/actionpack/lib/abstract_controller/railties/routes_helpers.rb
new file mode 100644
index 0000000000..b6e5631a4e
--- /dev/null
+++ b/actionpack/lib/abstract_controller/railties/routes_helpers.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+module AbstractController
+ module Railties
+ module RoutesHelpers
+ def self.with(routes, include_path_helpers = true)
+ Module.new do
+ define_method(:inherited) do |klass|
+ super(klass)
+ if namespace = klass.parents.detect { |m| m.respond_to?(:railtie_routes_url_helpers) }
+ klass.include(namespace.railtie_routes_url_helpers(include_path_helpers))
+ else
+ klass.include(routes.url_helpers(include_path_helpers))
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb
new file mode 100644
index 0000000000..41898c4c2e
--- /dev/null
+++ b/actionpack/lib/abstract_controller/rendering.rb
@@ -0,0 +1,136 @@
+# frozen_string_literal: true
+
+require_relative "error"
+require "action_view"
+require "action_view/view_paths"
+require "set"
+
+module AbstractController
+ 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
+
+ module Rendering
+ extend ActiveSupport::Concern
+ include ActionView::ViewPaths
+
+ # Normalizes arguments, options and then delegates render_to_body and
+ # sticks the result in <tt>self.response_body</tt>.
+ # :api: public
+ def render(*args, &block)
+ options = _normalize_render(*args, &block)
+ rendered_body = render_to_body(options)
+ if options[:html]
+ _set_html_content_type
+ else
+ _set_rendered_content_type rendered_format
+ end
+ self.response_body = rendered_body
+ end
+
+ # Raw rendering of a template to a string.
+ #
+ # It is similar to render, except that it does not
+ # set the +response_body+ and it should be guaranteed
+ # to always return a string.
+ #
+ # If a component extends the semantics of +response_body+
+ # (as ActionController extends it to be anything that
+ # responds to the method each), this method needs to be
+ # overridden in order to still return a string.
+ # :api: plugin
+ def render_to_string(*args, &block)
+ options = _normalize_render(*args, &block)
+ render_to_body(options)
+ end
+
+ # Performs the actual template rendering.
+ # :api: public
+ def render_to_body(options = {})
+ end
+
+ # Returns Content-Type of rendered content
+ # :api: public
+ def rendered_format
+ Mime[:text]
+ end
+
+ DEFAULT_PROTECTED_INSTANCE_VARIABLES = Set.new %i(
+ @_action_name @_response_body @_formats @_prefixes
+ )
+
+ # This method should return a hash with assigns.
+ # You can overwrite this configuration per controller.
+ # :api: public
+ def view_assigns
+ protected_vars = _protected_ivars
+ variables = instance_variables
+
+ variables.reject! { |s| protected_vars.include? s }
+ variables.each_with_object({}) { |name, hash|
+ hash[name.slice(1, name.length)] = instance_variable_get(name)
+ }
+ end
+
+ # Normalize args by converting <tt>render "foo"</tt> to
+ # <tt>render :action => "foo"</tt> and <tt>render "foo/bar"</tt> to
+ # <tt>render :file => "foo/bar"</tt>.
+ # :api: plugin
+ def _normalize_args(action = nil, options = {})
+ if action.respond_to?(:permitted?)
+ if action.permitted?
+ action
+ else
+ raise ArgumentError, "render parameters are not permitted"
+ end
+ elsif action.is_a?(Hash)
+ action
+ else
+ options
+ end
+ end
+
+ # Normalize options.
+ # :api: plugin
+ def _normalize_options(options)
+ options
+ end
+
+ # Process extra options.
+ # :api: plugin
+ def _process_options(options)
+ options
+ end
+
+ # Process the rendered format.
+ # :api: private
+ def _process_format(format)
+ end
+
+ def _process_variant(options)
+ end
+
+ def _set_html_content_type # :nodoc:
+ end
+
+ def _set_rendered_content_type(format) # :nodoc:
+ end
+
+ # Normalize args and options.
+ # :api: private
+ def _normalize_render(*args, &block)
+ options = _normalize_args(*args, &block)
+ _process_variant(options)
+ _normalize_options(options)
+ options
+ end
+
+ def _protected_ivars # :nodoc:
+ DEFAULT_PROTECTED_INSTANCE_VARIABLES
+ end
+ end
+end
diff --git a/actionpack/lib/abstract_controller/translation.rb b/actionpack/lib/abstract_controller/translation.rb
new file mode 100644
index 0000000000..666e154e4c
--- /dev/null
+++ b/actionpack/lib/abstract_controller/translation.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+module AbstractController
+ module Translation
+ # Delegates to <tt>I18n.translate</tt>. Also aliased as <tt>t</tt>.
+ #
+ # When the given key starts with a period, it will be scoped by the current
+ # controller and action. So if you call <tt>translate(".foo")</tt> from
+ # <tt>PeopleController#index</tt>, it will convert the call to
+ # <tt>I18n.translate("people.index.foo")</tt>. This makes it less repetitive
+ # to translate many keys within the same controller / action and gives you a
+ # simple framework for scoping them consistently.
+ def translate(key, options = {})
+ if key.to_s.first == "."
+ path = controller_path.tr("/", ".")
+ defaults = [:"#{path}#{key}"]
+ defaults << options[:default] if options[:default]
+ options[:default] = defaults.flatten
+ key = "#{path}.#{action_name}#{key}"
+ end
+ I18n.translate(key, options)
+ end
+ alias :t :translate
+
+ # Delegates to <tt>I18n.localize</tt>. Also aliased as <tt>l</tt>.
+ def localize(*args)
+ I18n.localize(*args)
+ end
+ alias :l :localize
+ end
+end
diff --git a/actionpack/lib/abstract_controller/url_for.rb b/actionpack/lib/abstract_controller/url_for.rb
new file mode 100644
index 0000000000..bd74c27d3b
--- /dev/null
+++ b/actionpack/lib/abstract_controller/url_for.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+module AbstractController
+ # Includes +url_for+ into the host class (e.g. an abstract controller or mailer). The class
+ # has to provide a +RouteSet+ by implementing the <tt>_routes</tt> methods. Otherwise, an
+ # exception will be raised.
+ #
+ # Note that this module is completely decoupled from HTTP - the only requirement is a valid
+ # <tt>_routes</tt> implementation.
+ module UrlFor
+ extend ActiveSupport::Concern
+ include ActionDispatch::Routing::UrlFor
+
+ def _routes
+ raise "In order to use #url_for, you must include routing helpers explicitly. " \
+ "For instance, `include Rails.application.routes.url_helpers`."
+ end
+
+ module ClassMethods
+ def _routes
+ nil
+ end
+
+ def action_methods
+ @action_methods ||= begin
+ if _routes
+ super - _routes.named_routes.helper_names
+ else
+ super
+ end
+ end
+ end
+ end
+ end
+end