Permalink
Browse files

Fix exceptions

  • Loading branch information...
1 parent 2e6efcc commit 9c898d38cfef4d22d5c06c17a3f313f9c2b3b4e1 Andy Delcambre committed Aug 23, 2009
Showing with 56 additions and 53 deletions.
  1. +44 −41 lib/rails_action_args/abstract_controller.rb
  2. +12 −12 spec/rails_action_args_spec.rb
@@ -1,53 +1,56 @@
-module ActionArgs
+module AbstractController
+ class BadRequest < StandardError; end
+
+ module ActionArgs
- def self.included(base)
-
- base.class_eval do
- class << self
- attr_accessor :action_argument_list
- alias_method :old_inherited, :inherited
+ def self.included(base)
- # 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]
+ 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
- old_inherited(klass)
end
- end
-
- alias :old_send_action :send_action
- # 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 BadRequest, "Your parameters (#{params.inspect}) were missing #{missing.join(", ")}"
+ alias :old_send_action :send_action
+ # 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 BadRequest, "Your parameters (#{params.inspect}) were missing #{missing.join(", ")}"
+ end
+ p ? params[arg.to_sym] : default
end
- p ? params[arg.to_sym] : default
+ old_send_action(action, *args)
end
- old_send_action(action, *args)
end
end
end
-
end
-AbstractController::Base.send(:include, ActionArgs)
+AbstractController::Base.send(:include, AbstractController::ActionArgs)
@@ -25,9 +25,9 @@
ActionArgsController.action(:defaults_mixed).call(Rack::MockRequest.env_for("/?foo=bar&baz=bar"))[2].body.should == "bar bar bar"
end
- # it "should throw a BadRequest if the arguments are not provided" do
- # lambda { ActionArgsController.action(:index).call(Rack::MockRequest.env_for("/")) }.should raise_error(Merb::ControllerExceptions::BadRequest)
- # end
+ it "should throw a BadRequest if the arguments are not provided" do
+ lambda { ActionArgsController.action(:index).call(Rack::MockRequest.env_for("/")) }.should raise_error(AbstractController::BadRequest)
+ end
it "should treat define_method actions as equal" do
ActionArgsController.action(:dynamic_define_method).call(Rack::MockRequest.env_for("/"))[2].body.should == "mos def"
@@ -45,13 +45,13 @@
ActionArgsController.action(:with_default_array).call(Rack::MockRequest.env_for("/?foo=bar"))[2].body.should == "bar []"
end
- # it "should print out the missing parameters if all are required" do
- # lambda { ActionArgsController.action(:multi).call(Rack::MockRequest.env_for("/")) }.should raise_error(
- # Merb::ControllerExceptions::BadRequest, /were missing foo, bar/)
- # end
- #
- # it "should only print out missing parameters" do
- # lambda { ActionArgsController.action(:multi).call(Rack::MockRequest.env_for("/?foo=Hello")) }.should raise_error(
- # Merb::ControllerExceptions::BadRequest, /were missing bar/)
- # end
+ it "should print out the missing parameters if all are required" do
+ lambda { ActionArgsController.action(:multi).call(Rack::MockRequest.env_for("/")) }.should raise_error(
+ AbstractController::BadRequest, /were missing foo, bar/)
+ end
+
+ it "should only print out missing parameters" do
+ lambda { ActionArgsController.action(:multi).call(Rack::MockRequest.env_for("/?foo=Hello")) }.should raise_error(
+ AbstractController::BadRequest, /were missing bar/)
+ end
end

0 comments on commit 9c898d3

Please sign in to comment.