1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/lib/action_controller/metal/hide_actions.rb

41 lines
1.1 KiB
Ruby
Raw Normal View History

2010-01-31 21:32:28 -05:00
module ActionController
# Adds the ability to prevent public methods on a controller to be called as actions.
module HideActions
extend ActiveSupport::Concern
included do
2010-01-31 21:32:28 -05:00
class_attribute :hidden_actions
self.hidden_actions = Set.new.freeze
end
2009-06-10 17:43:43 -04:00
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
2009-06-10 17:43:43 -04:00
end
2009-06-10 17:43:43 -04:00
module ClassMethods
# Sets all of the actions passed in as hidden actions.
#
# ==== Parameters
# * <tt>args</tt> - A list of actions
2009-06-10 17:43:43 -04:00
def hide_action(*args)
self.hidden_actions = hidden_actions.dup.merge(args.map(&:to_s)).freeze
2009-06-10 17:43:43 -04:00
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.
2009-06-10 17:43:43 -04:00
def action_methods
@action_methods ||= Set.new(super.reject { |name| hidden_actions.include?(name) }).freeze
end
2009-06-10 17:43:43 -04:00
end
end
end