Guest User

Untitled

a guest
Apr 25th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. From c1c45a3f2ca3123ccb8ff005b568ab34a69b0e0d Mon Sep 17 00:00:00 2001
  2. From: Matthew Ford <matt@new-bamboo.co.uk>
  3. Date: Thu, 8 May 2008 22:45:30 +0100
  4. Subject: [PATCH] spec and code for catching invalid filter options
  5.  
  6. ---
  7. lib/merb-core/controller/abstract_controller.rb | 10 ++++++++--
  8. spec/public/abstract_controller/filter_spec.rb | 9 +++++++++
  9. 2 files changed, 17 insertions(+), 2 deletions(-)
  10.  
  11. diff --git a/lib/merb-core/controller/abstract_controller.rb b/lib/merb-core/controller/abstract_controller.rb
  12. index d12406d..7c4fc91 100644
  13. --- a/lib/merb-core/controller/abstract_controller.rb
  14. +++ b/lib/merb-core/controller/abstract_controller.rb
  15. @@ -72,6 +72,8 @@ class Merb::AbstractController
  16. include Merb::InlineTemplates
  17.  
  18. class_inheritable_accessor :_before_filters, :_after_filters, :_layout, :_template_root
  19. +
  20. + FilterOptions = [:only, :exclude, :if, :unless, :with]
  21.  
  22. # ==== Returns
  23. # String:: The controller name in path form, e.g. "admin/items".
  24. @@ -435,8 +437,8 @@ class Merb::AbstractController
  25. #
  26. # ==== Raises
  27. # ArgumentError::
  28. - # Both :only and :exclude, or :if and :unless given, or filter is not a
  29. - # Symbol, String or Proc.
  30. + # Both :only and :exclude, or :if and :unless given, if filter is not a
  31. + # Symbol, String or Proc, or if an unknown option is passed.
  32. def self.add_filter(filters, filter, opts={})
  33. raise(ArgumentError,
  34. "You can specify either :only or :exclude but
  35. @@ -445,6 +447,10 @@ class Merb::AbstractController
  36. raise(ArgumentError,
  37. "You can specify either :if or :unless but
  38. not both at the same time for the same filter.") if opts.key?(:if) && opts.key?(:unless)
  39. +
  40. + opts.each_key {|key| raise(ArgumentError,
  41. + "You can only specify known filter options, #{key} is invalid.") unless FilterOptions.include?(key)
  42. + }
  43.  
  44. opts = normalize_filters!(opts)
  45.  
  46. diff --git a/spec/public/abstract_controller/filter_spec.rb b/spec/public/abstract_controller/filter_spec.rb
  47. index 7835c2f..2a19ecc 100644
  48. --- a/spec/public/abstract_controller/filter_spec.rb
  49. +++ b/spec/public/abstract_controller/filter_spec.rb
  50. @@ -74,6 +74,15 @@ describe Merb::AbstractController, " should support before and after filters" do
  51. end }.should raise_error(ArgumentError, /either :if or :unless/)
  52. end
  53.  
  54. + it "should throw an error if an unknown option is passed to a filter" do
  55. + running { Merb::Test::Fixtures::Abstract.class_eval do
  56. +
  57. + class TestErrorFilter < Merb::Test::Fixtures::Abstract::Testing
  58. + before :foo, :except => :index
  59. + end
  60. + end }.should raise_error(ArgumentError, /known filter options/)
  61. + end
  62. +
  63. it "should throw an error" do
  64. running { dispatch_should_make_body("TestConditionalFilterWithNoProcOrSymbol", "") }.should raise_error(ArgumentError, /a Symbol or a Proc/)
  65. end
  66. --
  67. 1.5.4.5
Add Comment
Please, Sign In to add comment