Permalink
Browse files

Execution global failure callback in model context

  • Loading branch information...
1 parent 0f38c6b commit eb59c5531528ed28c49f6a69e105637fc4a1353f quaternion committed Jan 29, 2012
@@ -28,7 +28,7 @@ def create attributes, row, options={}, callbacks={}
model.instance_exec row, &callbacks[:after_save] if callbacks[:after_save]
model
rescue
- options[:failure].call(model) if options[:failure]
+ model.instance_exec row, &options[:failure] if options[:failure]
model.instance_exec row, &callbacks[:failure] if callbacks[:failure]
raise ActiveRecord::Rollback if options[:failure_strategy] == :rollback
model
@@ -19,7 +19,7 @@ def create attributes, row, options={}, callbacks={}
model.instance_exec row, &callbacks[:after_save] if callbacks[:after_save]
model
rescue
- options[:failure].call(model) if options[:failure]
+ model.instance_exec row, &options[:failure] if options[:failure]
model.instance_exec row, &callbacks[:failure] if callbacks[:failure]
raise if options[:failure_strategy] == :rollback
model
@@ -31,7 +31,7 @@ def create attributes, row, options={}, callbacks={}
model.instance_exec row, &callbacks[:after_save] if callbacks[:after_save]
model
rescue
- options[:failure].call(model) if options[:failure]
+ model.instance_exec row, &options[:failure] if options[:failure]
model.instance_exec row, &callbacks[:failure] if callbacks[:failure]
raise Sequel::Rollback if options[:failure_strategy] == :rollback
model
@@ -1,5 +1,5 @@
class Transfer::Transferer
- attr_reader :dataset, :columns, :klass
+ attr_reader :dataset, :klass
def initialize dataset, klass, &block
@dataset = dataset
View
@@ -39,9 +39,9 @@ def do_action
do_action.should be_instance_of klass
end
- context "with options :failure_strategy =>" do
+ context "with options :failure_strategy" do
context ":ignore" do
- let!(:options) { {:failure_strategy => :ignore} }
+ let(:options) { {:failure_strategy => :ignore} }
it "not raise exception if model#save return false" do
instance_of(klass).save { false }
@@ -54,7 +54,7 @@ def do_action
end
context ":rollback" do
- let!(:options) { {:failure_strategy => :rollback} }
+ let(:options) { {:failure_strategy => :rollback} }
it "not raise exception if model#save return false" do
instance_of(klass).save { false }
@@ -68,31 +68,24 @@ def do_action
end
context "with options :failure" do
- let!(:callback){ lambda{|m|} }
- let!(:options){ {:failure => callback} }
- let!(:model){ klass.new }
-
- before do
- stub(klass).new{ model }
- end
+ let(:row){ {:value => "Johnny"} }
+ let(:callback){ lambda{|r| self.dynamic_value = r[:value]} }
+ let(:options){ {:failure => callback} }
it "should not call by default" do
- mock(callback).call(model).never
- do_action
+ do_action.dynamic_value.should be_nil
end
it "should call callback if model#save return false" do
instance_of(klass).save { false }
- stub(callback).call(model).once
- do_action
+ do_action.dynamic_value.should == "Johnny"
end
it "should call callback if model#save raise exception" do
instance_of(klass).save { raise "force exception!" }
- stub(callback).call(model).once
- do_action
+ do_action.dynamic_value.should == "Johnny"
end
end
- context "with options" do
+ context "with callbacks" do
let(:row){ {:value => "Flesh"} }
let(:callback) { lambda{|row| self.first_name = row[:value]} }
View
@@ -116,11 +116,12 @@ def do_action &block
end
end
- context "with Transfer.failure = lambda{|model|}" do
- let!(:callback){ lambda{|m|} }
+ context "with Transfer.failure &block" do
before do
Transfer.configure do |c|
- c.failure = callback
+ c.failure do |row|
+ self.dynamic_value = row[:fname]
+ end
end
end
@@ -134,20 +135,20 @@ def do_action &block
end
it "should not called callback" do
- dont_allow(callback).call
do_action
+ model.dynamic_value.should be_nil
end
it "should called callback if #save raise exception" do
instance_of(klass).save { raise "force exception!" }
- stub(callback).call(model).once
do_action
+ model.dynamic_value.should == "Johnny"
end
it "should called callback if #save return false" do
instance_of(klass).save { false }
- stub(callback).call(model).once
do_action
+ model.dynamic_value.should == "Johnny"
end
end
end

0 comments on commit eb59c55

Please sign in to comment.