Skip to content
Browse files

added tests for not supporting String, Proc or Class object callbacks…

… and filters

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@332 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information...
1 parent 2508c83 commit ce56a07177835946159c0f89d924bc2c79d09455 jcarroll committed Feb 14, 2008
Showing with 133 additions and 3 deletions.
  1. +6 −1 lib/callbacks.rb
  2. +6 −1 lib/filters.rb
  3. +57 −1 test/callbacks_test.rb
  4. +28 −0 test/filters_test.rb
  5. +36 −0 test/validations_test.rb
View
7 lib/callbacks.rb
@@ -15,7 +15,12 @@ def #{callback}_with_conditions(*callbacks)
#{callback}_without_conditions do |record|
unless (! options[:if].nil? && ! evaluate_condition(options[:if], record)) ||
(! options[:unless].nil? && evaluate_condition(options[:unless], record))
- record.send callback
+ if callback.class == Symbol
+ record.send callback
+ else
+ raise ActiveRecord::ActiveRecordError,
+ 'When only supports Symbol callbacks, refactor to use a Symbol or remove When'
+ end
end
end
end
View
7 lib/filters.rb
@@ -13,7 +13,12 @@ def #{filter}_with_conditions(*filters)
#{filter}_without_conditions do |controller|
unless (! options[:if].nil? && ! ActiveRecord::Base.evaluate_condition(options[:if], controller)) ||
(! options[:unless].nil? && ActiveRecord::Base.evaluate_condition(options[:unless], controller))
- controller.send filter
+ if filter.class == Symbol
+ controller.send filter
+ else
+ raise ActionController::ActionControllerError,
+ 'When only supports Symbol filters, refactor to use a Symbol or remove When'
+ end
end
end
end
View
58 test/callbacks_test.rb
@@ -49,6 +49,24 @@ def setup
assert company.save
assert_equal 'new name', company.name
end
+
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
+ Company.send callback.to_sym, "'puts #{callback}'", :if => condition
+
+ company = Company.new :name => 'thoughtbot', :flag => true
+ assert_raises(ActiveRecord::ActiveRecordError) do
+ assert company.save
+ end
+ end
+
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
+ Company.send callback.to_sym, "'puts #{callback}'", :unless => condition
+
+ company = Company.new :name => 'thoughtbot', :flag => false
+ assert_raises(ActiveRecord::ActiveRecordError) do
+ assert company.save
+ end
+ end
end
update_callbacks.each do |callback|
@@ -83,6 +101,24 @@ def setup
assert company.save
assert_equal 'new name', company.name
end
+
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
+ Company.send callback.to_sym, "'puts #{callback}'", :if => condition
+
+ company = Company.create :name => 'thoughtbot', :flag => true
+ assert_raises(ActiveRecord::ActiveRecordError) do
+ assert company.save
+ end
+ end
+
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
+ Company.send callback.to_sym, "'puts #{callback}'", :unless => condition
+
+ company = Company.create :name => 'thoughtbot', :flag => false
+ assert_raises(ActiveRecord::ActiveRecordError) do
+ assert company.save
+ end
+ end
end
destroy_callbacks.each do |callback|
@@ -121,9 +157,29 @@ def setup
assert company.destroy
assert company.flag
end
+
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
+ Company.send callback.to_sym, "'puts #{callback}'", :if => condition
+
+ company = Company.new :name => 'thoughtbot', :flag => true
+ assert company.save
+ assert_raises(ActiveRecord::ActiveRecordError) do
+ assert company.destroy
+ end
+ end
+
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
+ Company.send callback.to_sym, "'puts #{callback}'", :unless => condition
+
+ company = Company.new :name => 'thoughtbot', :flag => false
+ assert company.save
+ assert_raises(ActiveRecord::ActiveRecordError) do
+ assert company.destroy
+ end
+ end
end
end
-
+
def teardown
Object.class_eval do
remove_const Company.to_s if const_defined? Company.to_s
View
28 test/filters_test.rb
@@ -5,6 +5,12 @@ class FiltersTest < ActionController::TestCase
def setup
load File.join(File.dirname(__FILE__), 'fixtures', 'companies_controller.rb')
+ CompaniesController.class_eval do
+ def rescue_action(exception)
+ raise exception
+ end
+ end
+
@controller = CompaniesController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@@ -58,6 +64,28 @@ def setup
get :index
assert_equal 'new name', @controller.name
end
+
+ define_method "test_#{filter}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
+ CompaniesController.send filter.to_sym, CompaniesController, :if => condition
+
+ @controller.name = 'thoughtbot'
+ @controller.flag = true
+
+ assert_raises(ActionController::ActionControllerError) do
+ get :index
+ end
+ end
+
+ define_method "test_#{filter}_with_unless_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
+ CompaniesController.send filter.to_sym, CompaniesController, :unless => condition
+
+ @controller.name = 'thoughtbot'
+ @controller.flag = false
+
+ assert_raises(ActionController::ActionControllerError) do
+ get :index
+ end
+ end
end
end
View
36 test/validations_test.rb
@@ -40,6 +40,24 @@ def setup
assert company.save
assert_equal 'new name', company.name
end
+
+ define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
+ Company.send validation.to_sym, "'puts #{validation}'", :if => condition
+
+ company = Company.new :name => 'thoughtbot', :flag => true
+ assert_raises(ActiveRecord::ActiveRecordError) do
+ assert company.save
+ end
+ end
+
+ define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
+ Company.send validation.to_sym, "'puts #{validation}'", :unless => condition
+
+ company = Company.new :name => 'thoughtbot', :flag => false
+ assert_raises(ActiveRecord::ActiveRecordError) do
+ assert company.save
+ end
+ end
end
update_validations.each do |validation|
@@ -70,6 +88,24 @@ def setup
assert company.save
assert_equal 'new name', company.name
end
+
+ define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
+ Company.send validation.to_sym, "'puts #{validation}'", :if => condition
+
+ company = Company.create :name => 'thoughtbot', :flag => true
+ assert_raises(ActiveRecord::ActiveRecordError) do
+ assert company.save
+ end
+ end
+
+ define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
+ Company.send validation.to_sym, "'puts #{validation}'", :unless => condition
+
+ company = Company.create :name => 'thoughtbot', :flag => false
+ assert_raises(ActiveRecord::ActiveRecordError) do
+ assert company.save
+ end
+ end
end
end

0 comments on commit ce56a07

Please sign in to comment.
Something went wrong with that request. Please try again.