public
Description: resources_controller rails plugin: rc makes RESTful controllers fun
Homepage: http://plugins.ardes.com/doc/resources_controller
Clone URL: git://github.com/ianwhite/resources_controller.git
Click here to lend your support to: resources_controller and make a donation at www.pledgie.com !
resources_controller / lib / ardes / resources_controller / include_actions.rb
100644 35 lines (34 sloc) 1.327 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Ardes
  module ResourcesController
    # 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.
    module IncludeActions
      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.dup
        methods_to_remove(options).each {|m| mixin.send(:undef_method, m)}
        controller.send :include, mixin
      end
      
      def methods_to_remove(options = {})
        if options[:only]
          instance_methods - options[:only].map(&:to_s)
        elsif options[:except]
          options[:except].map(&:to_s) & instance_methods
        else
          []
        end
      end
    end
  end
end