Permalink
Browse files

fixed callbacks String condition

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@349 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information...
1 parent d8487bb commit 161cc080e7141aeddecfcdb75bb3c3d1ecf3d27e jcarroll committed Feb 15, 2008
Showing with 76 additions and 126 deletions.
  1. +1 −1 lib/callbacks.rb
  2. +43 −55 test/callbacks_test.rb
  3. +0 −10 test/fixtures/companies_controller.rb
  4. +0 −20 test/fixtures/company.rb
  5. +32 −40 test/validations_test.rb
View
@@ -21,7 +21,7 @@ def #{callback}_with_conditions(*callbacks, &block)
if callback.class == Symbol
record.send callback
elsif callback.class == String
- eval(callback, binding)
+ eval(callback, record.send(:binding))
elsif callback.class == Proc || callback.class == Method
callback.call(record)
else
View
@@ -182,120 +182,108 @@ def setup
conditions.each do |condition|
basic_callbacks.each do |callback|
- define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_bio" do
- Company.send callback.to_sym, 'self.bio = "new bio"', :if => condition
+ define_method "test_#{callback}_with_string_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
+ Company.send callback.to_sym, 'change_name', :if => condition
- Company.bio = 'thoughtbot'
- company = Company.new :flag => true
+ company = Company.new :name => 'thoughtbot', :flag => true
assert company.save
- assert_equal 'new bio', Company.bio
+ assert_equal 'new name', company.name
end
- define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_bio" do
- Company.send callback.to_sym, 'self.bio = "new bio"', :if => condition
+ define_method "test_#{callback}_with_string_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
+ Company.send callback.to_sym, 'change_name', :if => condition
- Company.bio = 'thoughtbot'
- company = Company.new :flag => false
+ company = Company.new :name => 'thoughtbot', :flag => false
assert company.save
- assert_equal 'thoughtbot', Company.bio
+ assert_equal 'thoughtbot', company.name
end
- define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_bio" do
- Company.send callback.to_sym, 'self.bio = "new bio"', :unless => condition
+ define_method "test_#{callback}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
+ Company.send callback.to_sym, 'change_name', :unless => condition
- Company.bio = 'thoughtbot'
- company = Company.new :flag => true
+ company = Company.new :name => 'thoughtbot', :flag => true
assert company.save
- assert_equal 'thoughtbot', Company.bio
+ assert_equal 'thoughtbot', company.name
end
- define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_bio" do
- Company.send callback.to_sym, 'self.bio = "new bio"', :unless => condition
+ define_method "test_#{callback}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
+ Company.send callback.to_sym, 'change_name', :unless => condition
- Company.bio = 'thoughtbot'
- company = Company.new :flag => false
+ company = Company.new :name => 'thoughtbot', :flag => false
assert company.save
- assert_equal 'new bio', Company.bio
+ assert_equal 'new name', company.name
end
end
update_callbacks.each do |callback|
- define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_bio" do
- Company.send callback.to_sym, 'self.bio = "new bio"', :if => condition
+ define_method "test_#{callback}_with_string_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
+ Company.send callback.to_sym, 'change_name', :if => condition
- Company.bio = 'thoughtbot'
company = Company.create :flag => true
assert company.save
- assert_equal 'new bio', Company.bio
+ assert_equal 'new name', company.name
end
- define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_bio" do
- Company.send callback.to_sym, 'self.bio = "new bio"', :if => condition
+ define_method "test_#{callback}_with_string_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
+ Company.send callback.to_sym, 'change_name', :if => condition
- Company.bio = 'thoughtbot'
- company = Company.create :flag => false
+ company = Company.create :name => 'thoughtbot', :flag => false
assert company.save
- assert_equal 'thoughtbot', Company.bio
+ assert_equal 'thoughtbot', company.name
end
- define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_bio" do
- Company.send callback.to_sym, 'self.bio = "new bio"', :unless => condition
+ define_method "test_#{callback}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
+ Company.send callback.to_sym, 'change_name', :unless => condition
- Company.bio = 'thoughtbot'
- company = Company.create :flag => true
+ company = Company.create :name => 'thoughtbot', :flag => true
assert company.save
- assert_equal 'thoughtbot', Company.bio
+ assert_equal 'thoughtbot', company.name
end
- define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_bio" do
- Company.send callback.to_sym, 'self.bio = "new bio"', :unless => condition
+ define_method "test_#{callback}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
+ Company.send callback.to_sym, 'change_name', :unless => condition
- Company.bio = 'thoughtbot'
- company = Company.create :flag => false
+ company = Company.create :name => 'thoughtbot', :flag => false
assert company.save
- assert_equal 'new bio', Company.bio
+ assert_equal 'new name', company.name
end
end
destroy_callbacks.each do |callback|
- define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_toggle_class_flag" do
- Company.send callback.to_sym, 'self.flag = ! flag; true;', :if => condition
+ define_method "test_#{callback}_with_string_callback_with_if_condition_#{condition.class}_which_returns_true_should_toggle_flag" do
+ Company.send callback.to_sym, 'toggle_flag; true', :if => condition
- Company.flag = true
company = Company.new :flag => true
assert company.save
assert company.destroy
- assert ! Company.flag
+ assert ! company.flag
end
- define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_false_should_not_toggle_class_flag" do
- Company.send callback.to_sym, 'self.flag = ! flag', :if => condition
+ define_method "test_#{callback}_with_string_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_toggle_flag" do
+ Company.send callback.to_sym, 'toggle_flag', :if => condition
- Company.flag = false
company = Company.new :flag => false
assert company.save
assert company.destroy
- assert ! Company.flag
+ assert ! company.flag
end
- define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_true_should_not_toggle_class_flag" do
- Company.send callback.to_sym, 'self.flag = ! flag', :unless => condition
+ define_method "test_#{callback}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_toggle_flag" do
+ Company.send callback.to_sym, 'toggle_flag', :unless => condition
- Company.flag = true
company = Company.new :flag => true
assert company.save
assert company.destroy
- assert Company.flag
+ assert company.flag
end
- define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_toggle_class_flag" do
- Company.send callback.to_sym, 'self.flag = ! flag', :unless => condition
+ define_method "test_#{callback}_with_string_callback_unless_condition_#{condition.class}_which_returns_false_should_toggle_flag" do
+ Company.send callback.to_sym, 'toggle_flag', :unless => condition
- Company.flag = false
company = Company.new :flag => false
assert company.save
assert company.destroy
- assert Company.flag
+ assert company.flag
end
end
end
@@ -1,15 +1,5 @@
class CompaniesController < ActionController::Base
- @@bio = nil
- class << self
- def bio
- @@bio
- end
- def bio=(bio)
- @@bio = bio
- end
- end
-
attr_accessor :flag,
:name
View
@@ -1,25 +1,5 @@
class Company < ActiveRecord::Base
- @@bio = nil
- class << self
- def bio
- @@bio
- end
- def bio=(bio)
- @@bio = bio
- end
- end
-
- @@flag = nil
- class << self
- def flag
- @@flag
- end
- def flag=(flag)
- @@flag = flag
- end
- end
-
attr_accessor :flag
def change_name
View
@@ -119,78 +119,70 @@ def setup
conditions.each do |condition|
basic_validations.each do |validation|
- define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_bio" do
- Company.send validation.to_sym, 'self.bio = "new bio"', :if => condition
+ define_method "test_#{validation}_with_string_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
+ Company.send validation.to_sym, 'change_name', :if => condition
- Company.bio = 'thoughtbot'
- company = Company.new :flag => true
+ company = Company.new :name => 'thoughtbot', :flag => true
assert company.save
- assert_equal 'new bio', Company.bio
+ assert_equal 'new name', company.name
end
- define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_bio" do
- Company.send validation.to_sym, 'self.bio = "new bio"', :if => condition
+ define_method "test_#{validation}_with_string_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
+ Company.send validation.to_sym, 'change_name', :if => condition
- Company.bio = 'thoughtbot'
- company = Company.new :flag => false
+ company = Company.new :name => 'thoughtbot', :flag => false
assert company.save
- assert_equal 'thoughtbot', Company.bio
+ assert_equal 'thoughtbot', company.name
end
- define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_bio" do
- Company.send validation.to_sym, 'self.bio = "new bio"', :unless => condition
+ define_method "test_#{validation}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
+ Company.send validation.to_sym, 'change_name', :unless => condition
- Company.bio = 'thoughtbot'
- company = Company.new :flag => true
+ company = Company.new :name => 'thoughtbot', :flag => true
assert company.save
- assert_equal 'thoughtbot', Company.bio
+ assert_equal 'thoughtbot', company.name
end
- define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_bio" do
- Company.send validation.to_sym, 'self.bio = "new bio"', :unless => condition
+ define_method "test_#{validation}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
+ Company.send validation.to_sym, 'change_name', :unless => condition
- Company.bio = 'thoughtbot'
- company = Company.new :flag => false
+ company = Company.new :name => 'thoughtbot', :flag => false
assert company.save
- assert_equal 'new bio', Company.bio
+ assert_equal 'new name', company.name
end
end
update_validations.each do |validation|
- define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_bio" do
- Company.send validation.to_sym, 'self.bio = "new bio"', :if => condition
+ define_method "test_#{validation}_with_string_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
+ Company.send validation.to_sym, 'change_name', :if => condition
- Company.bio = 'thoughtbot'
- company = Company.create :flag => true
+ company = Company.create :name => 'thoughtbot', :flag => true
assert company.save
- assert_equal 'new bio', Company.bio
+ assert_equal 'new name', company.name
end
- define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_bio" do
- Company.send validation.to_sym, 'self.bio = "new bio"', :if => condition
+ define_method "test_#{validation}_with_string_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
+ Company.send validation.to_sym, 'change_name', :if => condition
- Company.bio = 'thoughtbot'
- company = Company.create :flag => false
+ company = Company.create :name => 'thoughtbot', :flag => false
assert company.save
- assert_equal 'thoughtbot', Company.bio
+ assert_equal 'thoughtbot', company.name
end
- define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_bio" do
- Company.send validation.to_sym, 'self.bio = "new bio"', :unless => condition
+ define_method "test_#{validation}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
+ Company.send validation.to_sym, 'change_name', :unless => condition
- Company.bio = 'thoughtbot'
- company = Company.create :flag => true
+ company = Company.create :name => 'thoughtbot', :flag => true
assert company.save
- assert_equal 'thoughtbot', Company.bio
+ assert_equal 'thoughtbot', company.name
end
- define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_bio" do
- Company.send validation.to_sym, 'self.bio = "new bio"', :unless => condition
+ define_method "test_#{validation}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
+ Company.send validation.to_sym, 'change_name', :unless => condition
- Company.bio = 'thoughtbot'
- company = Company.create :flag => false
+ company = Company.create :name => 'thoughtbot', :flag => false
assert company.save
- assert_equal 'new bio', Company.bio
+ assert_equal 'new name', company.name
end
end
end

0 comments on commit 161cc08

Please sign in to comment.