Permalink
Browse files

Make ActionArgs something you include in your Application controller

  • Loading branch information...
1 parent 7af8d0f commit 8b6d2c21058fa908230d057457653910df6d5881 Andy Delcambre committed Apr 3, 2010
Showing with 56 additions and 56 deletions.
  1. +41 −45 lib/rails_action_args/abstract_controller.rb
  2. +15 −11 spec/controllers/action_args_controller.rb
@@ -1,56 +1,52 @@
-module AbstractController
- class BadRequest < StandardError; end
-
- module ActionArgs
+# Hook up the BadRequest exception to return 400 Bad Request
+class AbstractController::BadRequest < StandardError; end
+ActionDispatch::ShowExceptions.rescue_responses["AbstractController::BadRequest"] = :bad_request
- def self.included(base)
+module ActionArgs
- base.class_eval do
- class << self
- attr_accessor :action_argument_list
- alias_method :old_inherited, :inherited
+ def self.included(base)
+ base.class_eval do
+ 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 |hash,action|
- args = klass.instance_method(action).get_args
- arguments = args[0]
- defaults = []
- arguments.each {|a| defaults << a[0] if a.size == 2} if arguments
- hash[action] = [arguments || [], defaults]
- end
- old_inherited(klass)
- end
- end
-
- alias :old_send_action :send_action
- # Calls an action and maps the params hash to the action parameters.
+ # Stores the argument lists for all methods for this class.
#
# ==== Parameters
- # action<Symbol>:: The action to call
- #
- # ==== Raises
- # BadRequest:: The params hash doesn't have a required parameter.
- def send_action(action)
- arguments, defaults = self.class.action_argument_list[action.to_s]
-
- args = arguments.map do |arg, default|
- p = params.key?(arg.to_sym)
- unless p || (defaults && defaults.include?(arg))
- missing = arguments.reject {|arg| params.key?(arg[0].to_sym || arg[1])}
- raise BadRequest, "Your parameters (#{params.inspect}) were missing #{missing.join(", ")}"
- end
- p ? params[arg.to_sym] : default
+ # klass<Class>::
+ # The controller that is being inherited from AbstractController.
+ def inherited(klass)
+ klass.action_argument_list = Hash.new do |hash,action|
+ args = klass.instance_method(action).get_args
+ arguments = args[0]
+ defaults = []
+ arguments.each {|a| defaults << a[0] if a.size == 2} if arguments
+ hash[action] = [arguments || [], defaults]
end
- old_send_action(action, *args)
+ old_inherited(klass)
end
end
end
end
-end
-AbstractController::Base.send(:include, AbstractController::ActionArgs)
+ # 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 send_action(action)
+ arguments, defaults = self.class.action_argument_list[action.to_s]
+
+ args = arguments.map do |arg, default|
+ p = params.key?(arg.to_sym)
+ unless p || (defaults && defaults.include?(arg))
+ missing = arguments.reject {|arg| params.key?(arg[0].to_sym || arg[1])}
+ raise AbstractController::BadRequest, "Your parameters (#{params.inspect}) were missing #{missing.join(", ")}"
+ end
+ p ? params[arg.to_sym] : default
+ end
+ super(action, *args)
+ end
+end
@@ -1,55 +1,59 @@
+class ApplicationController < ActionController::Base
+ include ActionArgs
+end
+
module ExtraActions
# def self.included(base)
# base.show_action(:funky_inherited_method)
# end
-
+
def funky_inherited_method(foo, bar)
render :text => "#{foo} #{bar}"
end
end
module Awesome
- class ActionArgsController < ActionController::Base
+ class ActionArgsController < ApplicationController
def index(foo)
render :text => foo.to_s
end
end
end
-class ActionArgsController < ActionController::Base
+class ActionArgsController < ApplicationController
include ExtraActions
def nada
render :text => "NADA"
end
-
+
def index(foo)
render :text => foo
end
-
+
def multi(foo, bar)
render :text => "#{foo} #{bar}"
end
-
+
def defaults(foo, bar = "bar")
render :text => "#{foo} #{bar}"
end
-
+
def defaults_mixed(foo, bar ="bar", baz = "baz")
render :text => "#{foo} #{bar} #{baz}"
end
-
+
define_method :dynamic_define_method do
render :text => "mos def"
end
-
+
def with_default_nil(foo, bar = nil)
render :text => "#{foo} #{bar}"
end
-
+
def with_default_array(foo, bar = [])
render :text => "#{foo} #{bar.inspect}"
end
-
+
end

0 comments on commit 8b6d2c2

Please sign in to comment.