Module: ResourcesController::IncludeActions

Defined in:
lib/resources_controller/include_actions.rb

Overview

extension for any module that is used as an Actions module.

After extending the module (say 'MyActions'), instead of doing this:

self.include ActionsModule

do this:

ActionsModule.include_actions(self, <:only or :except options>)

RC extends any actions module with this automatically, so you don't need to know about it.

However, if you ahve any special behaviour in your actions module that is sensitive to :only and :except, you can define your own include_actions method on that module to effect this special behaviour.

Instance Method Summary collapse

Instance Method Details

#action_methods_to_remove(options = {}) ⇒ Object


27
28
29
30
31
32
33
34
35
36
# File 'lib/resources_controller/include_actions.rb', line 27

def action_methods_to_remove(options = {})
  if options[:only]
    # instance_methods contains Strings in < 1.9, and Symbols in >= 1.9
    instance_methods.map(&:to_s) - Array(options[:only]).map(&:to_s)
  elsif options[:except]
    Array(options[:except]).map(&:to_s) & instance_methods.map(&:to_s)
  else
    []
  end
end

#include_actions(controller, options = {}) ⇒ Object

Raises:

  • (ArgumentError)

15
16
17
18
19
20
21
# File 'lib/resources_controller/include_actions.rb', line 15

def include_actions(controller, options = {})
  options.assert_valid_keys(:only, :except)
  raise ArgumentError, "you can only specify either :except or :only, not both" if options[:only] && options[:except]
  mixin = self.clone
  action_methods_to_remove(options).each {|a| mixin.remove_action_method(a) }
  controller.send :include, mixin
end

#remove_action_method(action) ⇒ Object


23
24
25
# File 'lib/resources_controller/include_actions.rb', line 23

def remove_action_method(action)
  undef_method action
end