Created
April 14, 2011 23:08
-
-
Save timlinquist/920791 to your computer and use it in GitHub Desktop.
A beautiful hack to extend Rails to allow custom actions for a controller instance & helpers for thew view.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def self.extended(controller) | |
| controller.extend(ControllerMethods) | |
| controller._helpers.module_eval { include HelperMethods } | |
| existing_actions = controller.action_methods + ControllerMethods.public_instance_methods(true) | |
| controller.class.class_eval do | |
| cattr_accessor :actions_to_use | |
| def self.override_action_methods(actions=[]) | |
| self.actions_to_use = actions | |
| end | |
| def self.action_methods | |
| self.actions_to_use || @action_methods | |
| end | |
| end | |
| controller.class.override_action_methods(existing_actions) | |
| end | |
| Use: | |
| In before_filter on controller: | |
| load File.join(file_root,'content_methods.rb') unless Object.const_defined?(@client_domain.namespace) | |
| self.extend(@client_domain.namespace.constantize) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment