Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactored tests

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@321 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information...
commit f32f02174ffe2e366c01b00a23fd44bbd4b4a893 1 parent e3230a4
jcarroll authored
View
6 lib/callbacks.rb
@@ -4,7 +4,9 @@ module Callbacks
def self.included(klass)
class << klass
- callbacks = ActiveRecord::Callbacks::CALLBACKS - %w(after_find after_initialize)
+ callbacks = ActiveRecord::Callbacks::CALLBACKS +
+ ActiveRecord::Validations::VALIDATIONS -
+ %w(after_find after_initialize)
callbacks.each do |callback|
src = <<-END;
@@ -26,7 +28,7 @@ def #{callback}_with_conditions (*callbacks, &block)
END
class_eval src, __FILE__, __LINE__
end
-
+
def execute_callback(callback, record, method)
if callback.class == Symbol
record.send callback
View
54 lib/validations.rb
@@ -1,54 +0,0 @@
-module ActiveRecordHook
- module Validations
-
- def self.included(klass)
- class << klass
-
- validations = ActiveRecord::Validations::VALIDATIONS
-
- validations.each do |validation|
- src = <<-END;
- def #{validation}_with_conditions (*validations, &block)
- options = validations.extract_options!
- if block_given?
- validations << block
- end
- validations.each do |validation|
- #{validation}_without_conditions do |record|
- unless (! options[:if].nil? && ! evaluate_condition(options[:if], record)) ||
- (! options[:unless].nil? && evaluate_condition(options[:unless], record))
- execute_validation validation, record, :#{validation}
- end
- end
- end
- end
- alias_method_chain :#{validation}, :conditions
- END
- class_eval src, __FILE__, __LINE__
- end
-
- def execute_validation(validation, record, method)
- if validation.class == Symbol
- record.send validation
-# elsif callback.class == String
-# eval callback, record.send(:binding)
-# elsif callback.class == Proc || callback.class == Method
-# callback.call record
-# else
-# if callback.respond_to?(method)
-# callback.send method, record
-# else
-# raise ActiveRecordError, 'Callbacks must be a symbol denoting the message to send,
-# a string to be evaluated, a block to be invoked or a
-# class object responding to the callback message
-# (#before_create, #after_create, etc).'
-# end
-
- end
- end
-
- end
- end
-
- end
-end
View
2  lib/when.rb
@@ -1,5 +1,3 @@
require File.join(File.dirname(__FILE__), 'callbacks')
-require File.join(File.dirname(__FILE__), 'validations')
ActiveRecord::Base.send :include, ActiveRecordHook::Callbacks
-ActiveRecord::Base.send :include, ActiveRecordHook::Validations
View
65 test/callbacks_test.rb
@@ -6,41 +6,40 @@ def setup
load File.join(File.dirname(__FILE__), 'fixtures', 'company.rb')
end
- basic_callbacks = ['before_create', 'after_create',
- 'before_validation_on_create', 'before_validation',
- 'after_validation_on_create', 'after_validation',
- 'before_save', 'after_save' ]
- update_callbacks = ['before_update', 'after_update',
- 'before_validation_on_update', 'after_validation_on_update']
- destroy_callbacks = ['before_destroy', 'after_destroy']
-
- conditions = [ lambda {|company| company.callback_flag == true}, :flag? ]
+ basic_callbacks = %w(before_create after_create
+ before_validation_on_create before_validation
+ after_validation_on_create after_validation
+ before_save after_save)
+ update_callbacks = %w(before_update after_update
+ before_validation_on_update after_validation_on_update)
+ destroy_callbacks = %w(before_destroy after_destroy)
+ conditions = [lambda {|company| company.callback_flag == true}, :flag?, 'flag?']
conditions.each do |condition|
basic_callbacks.each do |callback|
- define_method "test_#{callback}_with_if_condition_which_returns_true_should_change_company_name" do
- Company.send(callback.to_sym, :change_name, { :if => condition })
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
+ Company.send(callback.to_sym, :change_name, :if => condition)
company = Company.new :name => 'thoughtbot', :callback_flag => true
assert company.save
assert_equal 'new name', company.name
end
- define_method "test_#{callback}_with_if_condition_which_returns_false_should_not_change_company_name" do
- Company.send(callback.to_sym, :change_name, { :if => condition })
+ define_method "test_#{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 = Company.new :name => 'thoughtbot', :callback_flag => false
assert company.save
assert_equal 'thoughtbot', company.name
end
- define_method "test_#{callback}_with_unlesss_condition_which_returns_true_should_not_change_company_name" do
- Company.send(callback.to_sym, :change_name, { :unless => condition })
+ define_method "test_#{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 = Company.new :name => 'thoughtbot', :callback_flag => true
assert company.save
assert_equal 'thoughtbot', company.name
end
- define_method "test_#{callback}_with_unless_condition_which_returns_false_should_change_company_name" do
- Company.send(callback.to_sym, :change_name, { :unless => condition })
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
+ Company.send(callback.to_sym, :change_name, :unless => condition)
company = Company.new :name => 'thoughtbot', :callback_flag => false
assert company.save
assert_equal 'new name', company.name
@@ -48,29 +47,29 @@ def setup
end
update_callbacks.each do |callback|
- define_method "test_#{callback}_with_if_condition_which_returns_true_should_change_company_name" do
- Company.send(callback.to_sym, :change_name, { :if => condition })
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
+ Company.send(callback.to_sym, :change_name, :if => condition)
company = Company.create :name => 'thoughtbot', :callback_flag => true
assert company.save
assert_equal 'new name', company.name
end
- define_method "test_#{callback}_with_if_condition_which_returns_false_should_not_change_company_name" do
- Company.send(callback.to_sym, :change_name, { :if => condition })
+ define_method "test_#{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 = Company.create :name => 'thoughtbot', :callback_flag => false
assert company.save
assert_equal 'thoughtbot', company.name
end
- define_method "test_#{callback}_with_unless_condition_which_returns_true_should_not_change_company_name" do
- Company.send(callback.to_sym, :change_name, { :unless => condition })
+ define_method "test_#{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 = Company.create :name => 'thoughtbot', :callback_flag => true
assert company.save
assert_equal 'thoughtbot', company.name
end
- define_method "test_#{callback}_with_unless_condition_which_returns_false_should_change_company_name" do
- Company.send(callback.to_sym, :change_name, { :unless => condition })
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
+ Company.send(callback.to_sym, :change_name, :unless => condition)
company = Company.create :name => 'thoughtbot', :callback_flag => false
assert company.save
assert_equal 'new name', company.name
@@ -78,32 +77,32 @@ def setup
end
destroy_callbacks.each do |callback|
- define_method "test_#{callback}_with_if_condition_which_returns_true_should_change_company_name" do
- Company.send(callback.to_sym, :change_name, { :if => condition })
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
+ Company.send(callback.to_sym, :change_name, :if => condition)
company = Company.new :name => 'thoughtbot', :callback_flag => true
assert company.save
assert company.destroy
assert_equal 'new name', company.name unless company.frozen?
end
- define_method "test_#{callback}_with_if_condition_which_returns_false_should_not_change_company_name" do
- Company.send(callback.to_sym, :change_name, { :if => condition })
+ define_method "test_#{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 = Company.new :name => 'thoughtbot', :callback_flag => false
assert company.save
assert company.destroy
assert_equal 'thoughtbot', company.name
end
- define_method "test_#{callback}_with_unless_condition_which_returns_true_should_not_change_company_name" do
- Company.send(callback.to_sym, :change_name, { :unless => condition })
+ define_method "test_#{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 = Company.new :name => 'thoughtbot', :callback_flag => true
assert company.save
assert company.destroy
assert_equal 'thoughtbot', company.name
end
- define_method "test_#{callback}_with_unless_condition_which_returns_false_should_change_company_name" do
- Company.send(callback.to_sym, :change_name, { :unless => condition })
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
+ Company.send(callback.to_sym, :change_name, :unless => condition)
company = Company.new :name => 'thoughtbot', :callback_flag => false
assert company.save
assert company.destroy
View
16 test/fixtures/company.rb
@@ -2,15 +2,13 @@ class Company < ActiveRecord::Base
attr_accessor :callback_flag
- protected
-
- def change_name
- self.name = 'new name' unless frozen?
- end
-
- def flag?
- callback_flag == true
- end
+ def change_name
+ self.name = 'new name' unless frozen?
+ end
+
+ def flag?
+ callback_flag == true
+ end
# (1..114).each do |each|
# attr_accessor :"attribute_#{each}"
View
100 test/validations_test.rb
@@ -6,92 +6,74 @@ def setup
load File.join(File.dirname(__FILE__), 'fixtures', 'company.rb')
end
- validations = ['validate', 'validate_on_create']
- conditions = ['lambda {|company| company.callback_flag == true}']
- src = ''
+ basic_validations = %w(validate validate_on_create)
+ update_validations = %w(validate_on_update)
+
+ conditions = [lambda {|company| company.callback_flag == true}]
- conditions.each do |condition|
- validations.each do |validation|
- src << <<-END;
- def test_#{validation}_with_if_condition_which_returns_true_should_change_company_name
- Company.class_eval do
- #{validation} :change_name, :if => #{condition}
- end
- company = Company.new :name => 'thoughtbot', :callback_flag => true
- assert company.save
- assert_equal 'new name', company.name
- end
+ conditions.each do |condition|
+ basic_validations.each do |validation|
+ define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
+ Company.send(validation.to_sym, :change_name, :if => condition)
+
+ company = Company.new :name => 'thoughtbot', :callback_flag => true
+ assert company.save
+ assert_equal 'new name', company.name
+ end
- def test_#{validation}_with_if_condition_which_returns_false_should_not_change_company_name
- Company.class_eval do
- #{validation} :change_name, :if => #{condition}
- end
- company = Company.new :name => 'thoughtbot', :callback_flag => false
- assert company.save
- assert_equal 'thoughtbot', company.name
- end
+ define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
+ Company.send(validation.to_sym, :change_name, :if => condition)
+ company = Company.new :name => 'thoughtbot', :callback_flag => false
+ assert company.save
+ assert_equal 'thoughtbot', company.name
+ end
- def test_#{validation}_with_unless_condition_which_returns_true_should_not_change_company_name
- Company.class_eval do
- #{validation} :change_name, :unless => #{condition}
- end
- company = Company.new :name => 'thoughtbot', :callback_flag => true
- assert company.save
- assert_equal 'thoughtbot', company.name
- end
+ define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
+ Company.send(validation.to_sym, :change_name, :unless => condition)
+ company = Company.new :name => 'thoughtbot', :callback_flag => true
+ assert company.save
+ assert_equal 'thoughtbot', company.name
+ end
- def test_#{validation}_with_unless_condition_which_returns_false_should_change_company_name
- Company.class_eval do
- #{validation} :change_name, :unless => #{condition}
- end
- company = Company.new :name => 'thoughtbot', :callback_flag => false
- assert company.save
- assert_equal 'new name', company.name
- end
- END
+ define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
+ Company.send(validation.to_sym, :change_name, :unless => condition)
+ company = Company.new :name => 'thoughtbot', :callback_flag => false
+ assert company.save
+ assert_equal 'new name', company.name
+ end
end
- src << <<-END;
- def test_validate_on_update_with_if_condition_which_returns_true_should_change_company_name
- Company.class_eval do
- validate_on_update :change_name, :if => #{condition}
- end
+ update_validations.each do |validation|
+ define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
+ Company.send(validation.to_sym, :change_name, :if => condition)
company = Company.create :name => 'thoughtbot', :callback_flag => true
assert company.save
assert_equal 'new name', company.name
end
- def test_validate_on_update_with_if_condition_which_returns_false_should_not_change_company_name
- Company.class_eval do
- validate_on_update :change_name, :if => #{condition}
- end
+ define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
+ Company.send(validation.to_sym, :change_name, :if => condition)
company = Company.create :name => 'thoughtbot', :callback_flag => false
assert company.save
assert_equal 'thoughtbot', company.name
end
- def test_validate_on_update_with_unless_condition_which_returns_true_should_not_change_company_name
- Company.class_eval do
- validate_on_update :change_name, :unless => #{condition}
- end
+ define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
+ Company.send(validation.to_sym, :change_name, :unless => condition)
company = Company.create :name => 'thoughtbot', :callback_flag => true
assert company.save
assert_equal 'thoughtbot', company.name
end
- def test_validate_on_update_with_unless_condition_which_returns_false_should_change_company_name
- Company.class_eval do
- validate_on_update :change_name, :unless => #{condition}
- end
+ define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
+ Company.send(validation.to_sym, :change_name, :unless => condition)
company = Company.create :name => 'thoughtbot', :callback_flag => false
assert company.save
assert_equal 'new name', company.name
end
- END
+ end
end
- class_eval src, __FILE__, __LINE__
-
def teardown
Object.class_eval do
remove_const Company.to_s if const_defined? Company.to_s
Please sign in to comment.
Something went wrong with that request. Please try again.