Created
May 7, 2009 18:18
-
-
Save collin/108255 to your computer and use it in GitHub Desktop.
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
| # Ended up overriding send. | |
| # Seems a bit intense, but action-args are too awesome to not use. | |
| class ActionController::Base | |
| class << self | |
| attr_accessor :action_argument_list | |
| alias_method :old_inherited, :inherited | |
| # Stores the argument lists for all methods for this class. | |
| # | |
| # ==== Parameters | |
| # klass<Class>:: | |
| # The controller that is being inherited from Merb::AbstractController. | |
| def inherited(klass) | |
| klass.action_argument_list = Hash.new do |h,k| | |
| args = klass.instance_method(k).get_args | |
| arguments = args[0] | |
| defaults = [] | |
| arguments.each {|a| defaults << a[0] if a.size == 2} if arguments | |
| h[k] = [arguments || [], defaults] | |
| end | |
| old_inherited(klass) | |
| end | |
| end | |
| def send action, *args | |
| if action_methods.include?(action) | |
| arguments, defaults = self.class.action_argument_list[action] | |
| args = arguments.map do |arg, default| | |
| arg = arg | |
| p = params.key?(arg.to_sym) | |
| raise BadRequest unless p || (defaults && defaults.include?(arg)) | |
| p ? params[arg.to_sym] : default | |
| end | |
| end | |
| super action, *args | |
| end | |
| end |
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
| #I tried this, but the spiral of deep hurting from the method no longer actually being the method killed me. | |
| class ActionController::Base | |
| class << self | |
| attr_accessor :action_argument_list | |
| alias_method :old_inherited, :inherited | |
| # Stores the argument lists for all methods for this class. | |
| # | |
| # ==== Parameters | |
| # klass<Class>:: | |
| # The controller that is being inherited from Merb::AbstractController. | |
| def inherited(klass) | |
| klass.action_argument_list = Hash.new do |h,k| | |
| args = klass.instance_method(k).get_args | |
| arguments = args[0] | |
| defaults = [] | |
| arguments.each {|a| defaults << a[0] if a.size == 2} if arguments | |
| h[k] = [arguments || [], defaults] | |
| end | |
| old_inherited(klass) | |
| end | |
| end | |
| # Calls an action and maps the params hash to the action parameters. | |
| # | |
| # ==== Parameters | |
| # action<Symbol>:: The action to call | |
| # | |
| # ==== Raises | |
| # BadRequest:: The params hash doesn't have a required parameter. | |
| def _call_action(action) | |
| arguments, defaults = self.class.action_argument_list[action] | |
| args = arguments.map do |arg, default| | |
| arg = arg | |
| p = params.key?(arg.to_sym) | |
| raise BadRequest unless p || (defaults && defaults.include?(arg)) | |
| p ? params[arg.to_sym] : default | |
| end | |
| send(action, *args) | |
| end | |
| def perform_action | |
| if action_methods.include?(action_name) | |
| _call_action(action_name) | |
| default_render unless performed? | |
| elsif respond_to? :method_missing | |
| method_missing action_name | |
| default_render unless performed? | |
| else | |
| begin | |
| default_render | |
| rescue ActionView::MissingTemplate => e | |
| # Was the implicit template missing, or was it another template? | |
| if e.path == default_template_name | |
| raise UnknownAction, "No action responded to #{action_name}. Actions: #{action_methods.sort.to_sentence(:locale => :en)}", caller | |
| else | |
| raise e | |
| end | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment