2010-01-31 21:32:28 -05:00
|
|
|
require 'active_support/core_ext/class/attribute'
|
|
|
|
|
2009-03-12 15:19:13 -04:00
|
|
|
module ActionController
|
2009-06-10 17:43:43 -04:00
|
|
|
# ActionController::HideActions adds the ability to prevent public methods on a controller
|
|
|
|
# to be called as actions.
|
2009-03-12 15:19:13 -04:00
|
|
|
module HideActions
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2009-05-11 20:07:05 -04:00
|
|
|
|
2009-05-07 11:38:57 -04:00
|
|
|
included do
|
2010-01-31 21:32:28 -05:00
|
|
|
class_attribute :hidden_actions
|
|
|
|
self.hidden_actions = Set.new
|
2009-03-12 15:19:13 -04:00
|
|
|
end
|
2009-05-07 11:38:57 -04:00
|
|
|
|
2009-06-10 17:43:43 -04:00
|
|
|
private
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-06-10 18:27:53 -04:00
|
|
|
# Overrides AbstractController::Base#action_method? to return false if the
|
|
|
|
# action name is in the list of hidden actions.
|
2010-02-16 13:45:59 -05:00
|
|
|
def method_for_action(action_name)
|
|
|
|
self.class.visible_action?(action_name) && super
|
2009-06-10 17:43:43 -04:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-06-10 17:43:43 -04:00
|
|
|
module ClassMethods
|
2009-06-10 18:27:53 -04:00
|
|
|
# Sets all of the actions passed in as hidden actions.
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# *args<#to_s>:: A list of actions
|
2009-06-10 17:43:43 -04:00
|
|
|
def hide_action(*args)
|
2010-01-31 21:32:28 -05:00
|
|
|
self.hidden_actions = hidden_actions.dup.merge(args.map(&:to_s))
|
2009-06-10 17:43:43 -04:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-08-10 18:49:33 -04:00
|
|
|
def inherited(klass)
|
2010-02-16 13:45:59 -05:00
|
|
|
klass.class_eval { @visible_actions = {} }
|
2009-08-10 18:49:33 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def visible_action?(action_name)
|
|
|
|
return @visible_actions[action_name] if @visible_actions.key?(action_name)
|
2010-02-16 13:45:59 -05:00
|
|
|
@visible_actions[action_name] = !hidden_actions.include?(action_name)
|
2009-08-10 18:49:33 -04:00
|
|
|
end
|
|
|
|
|
2009-06-10 18:27:53 -04:00
|
|
|
# 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
|
2009-06-10 18:27:53 -04:00
|
|
|
@action_methods ||= Set.new(super.reject {|name| hidden_actions.include?(name)})
|
2009-05-28 10:49:02 -04:00
|
|
|
end
|
2009-06-10 17:43:43 -04:00
|
|
|
end
|
2009-03-12 15:19:13 -04:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
end
|