提交 46c853f3 编写于 作者: D David Heinemeier Hansson

Merge pull request #18371 from brainopia/remove_hide_actions

Remove ActionController::HideActions (closes #18336)
* Remove `ActionController::HideActions`
*Ravil Bayramgalin*
* Remove `respond_to`/`respond_with` placeholder methods, this functionality
has been extracted to the `responders` gem.
......
......@@ -57,21 +57,11 @@ def internal_methods
controller.public_instance_methods(true)
end
# The list of hidden actions. Defaults to an empty array.
# This can be modified by other modules or subclasses
# to specify particular actions as hidden.
#
# ==== Returns
# * <tt>Array</tt> - 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.
# itself.
#
# ==== Returns
# * <tt>Set</tt> - A set of all methods that should be considered actions.
......@@ -82,9 +72,7 @@ def action_methods
# 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) -
# And always exclude explicitly hidden actions
hidden_actions.to_a
public_instance_methods(false)).uniq.map(&:to_s)
methods.to_set
end
......
......@@ -206,7 +206,6 @@ def self.without_modules(*modules)
AbstractController::AssetPaths,
Helpers,
HideActions,
UrlFor,
Redirecting,
ActionView::Layouts,
......
module ActionController
# Adds the ability to prevent public methods on a controller to be called as actions.
module HideActions
extend ActiveSupport::Concern
included do
class_attribute :hidden_actions
self.hidden_actions = Set.new.freeze
end
private
# Overrides AbstractController::Base#action_method? to return false if the
# action name is in the list of hidden actions.
def method_for_action(action_name)
self.class.visible_action?(action_name) && super
end
module ClassMethods
# Sets all of the actions passed in as hidden actions.
#
# ==== Parameters
# * <tt>args</tt> - A list of actions
def hide_action(*args)
self.hidden_actions = hidden_actions.dup.merge(args.map(&:to_s)).freeze
end
def visible_action?(action_name)
not hidden_actions.include?(action_name)
end
# Overrides AbstractController::Base#action_methods to remove any methods
# that are listed as hidden methods.
def action_methods
@action_methods ||= Set.new(super.reject { |name| hidden_actions.include?(name) }).freeze
end
end
end
end
require 'abstract_unit'
require 'active_support/logger'
require 'controller/fake_models'
require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late
# Provide some controller to run the tests on.
module Submodule
class ContainedEmptyController < ActionController::Base
end
class ContainedNonEmptyController < ActionController::Base
def public_action
render :nothing => true
end
hide_action :hidden_action
def hidden_action
raise "Noooo!"
end
def another_hidden_action
end
hide_action :another_hidden_action
end
class SubclassedController < ContainedNonEmptyController
hide_action :public_action # Hiding it here should not affect the superclass.
end
end
class EmptyController < ActionController::Base
......@@ -35,10 +15,6 @@ class NonEmptyController < ActionController::Base
def public_action
render :nothing => true
end
hide_action :hidden_action
def hidden_action
end
end
class DefaultUrlOptionsController < ActionController::Base
......@@ -108,10 +84,7 @@ class ControllerInstanceTests < ActiveSupport::TestCase
def setup
@empty = EmptyController.new
@contained = Submodule::ContainedEmptyController.new
@empty_controllers = [@empty, @contained, Submodule::SubclassedController.new]
@non_empty_controllers = [NonEmptyController.new,
Submodule::ContainedNonEmptyController.new]
@empty_controllers = [@empty, @contained]
end
def test_performed?
......@@ -124,10 +97,6 @@ def test_action_methods
@empty_controllers.each do |c|
assert_equal Set.new, c.class.action_methods, "#{c.controller_path} should be empty!"
end
@non_empty_controllers.each do |c|
assert_equal Set.new(%w(public_action)), c.class.action_methods, "#{c.controller_path} should not be empty!"
end
end
def test_temporary_anonymous_controllers
......@@ -161,12 +130,6 @@ def test_process_should_be_precise
assert_equal "The action 'non_existent' could not be found for EmptyController", exception.message
end
def test_get_on_hidden_should_fail
use_controller NonEmptyController
assert_raise(AbstractController::ActionNotFound) { get :hidden_action }
assert_raise(AbstractController::ActionNotFound) { get :another_hidden_action }
end
def test_action_missing_should_work
use_controller ActionMissingController
get :arbitrary_action
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册