Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

During saving a model changed_columns and new? should preserve values…

… until all hook finish running
  • Loading branch information...
commit 34867d6490386cf7e5383e649322af38722e2b0e 1 parent f061428
@tamas tamas authored committed
Showing with 31 additions and 5 deletions.
  1. +9 −5 lib/sequel_model/record.rb
  2. +22 −0 spec/sequel_model/record_spec.rb
View
14 lib/sequel_model/record.rb
@@ -199,7 +199,7 @@ def save(*columns)
def save!(*columns)
opts = columns.extract_options!
return save_failure(:save) if before_save == false
- if @new
+ if new?
return save_failure(:create) if before_create == false
ds = model.dataset
if ds.respond_to?(:insert_select) and h = ds.insert_select(@values)
@@ -214,24 +214,28 @@ def save!(*columns)
end
if pk
@this = nil # remove memoized this dataset
- refresh
+ do_refresh = true
end
end
- @new = false
after_create
else
return save_failure(:update) if before_update == false
if columns.empty?
vals = opts[:changed] ? @values.reject{|k,v| !changed_columns.include?(k)} : @values
this.update(vals)
- changed_columns.clear
else # update only the specified columns
this.update(@values.reject{|k, v| !columns.include?(k)})
- changed_columns.reject!{|c| columns.include?(c)}
end
after_update
end
after_save
+ if columns.empty? || new?
+ changed_columns.clear
+ else
+ changed_columns.reject!{|c| columns.include?(c)}
+ end
+ @new = false
+ refresh if defined?(do_refresh) && do_refresh
self
end
View
22 spec/sequel_model/record_spec.rb
@@ -54,6 +54,28 @@ def ds.insert_select(hash)
o.save(:y)
o.changed_columns.should == []
end
+
+ it "should preserve changed_columns' and @new's value until all hook finish running" do
+ res = nil
+ @c.after_save { res = [changed_columns, @new].flatten}
+ o = @c.new(:x => 1, :y => nil)
+ o[:x] = 2
+ o.save
+ res.should == [:x,true]
+
+ res = nil
+ o = @c.load(:id => 23,:x => 1, :y => nil)
+ o[:x] = 2
+ o.save
+ res.should == [:x,false]
+
+ res = nil
+ o = @c.load(:id => 23,:x => 1, :y => nil)
+ o[:x] = 2
+ o[:y] = 22
+ o.save(:x)
+ res.should == [:x,:y,false]
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.