mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Remove ActionController::HideActions (closes #18336)
This commit is contained in:
parent
362acba04e
commit
08d3f0e3b3
5 changed files with 7 additions and 93 deletions
|
@ -1,3 +1,7 @@
|
|||
* 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 @@ module AbstractController
|
|||
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 @@ module AbstractController
|
|||
# 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 @@ module ActionController
|
|||
AbstractController::AssetPaths,
|
||||
|
||||
Helpers,
|
||||
HideActions,
|
||||
UrlFor,
|
||||
Redirecting,
|
||||
ActionView::Layouts,
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
|
||||
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
|
|
@ -1,31 +1,11 @@
|
|||
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 @@ class ControllerInstanceTests < ActiveSupport::TestCase
|
|||
@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 @@ class PerformActionTest < ActionController::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue