Skip to content

Commit

Permalink
During saving a model changed_columns and new? should preserve values…
Browse files Browse the repository at this point in the history
… until all hook finish running
  • Loading branch information
tamas authored and jeremyevans committed Jan 25, 2009
1 parent f061428 commit 34867d6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/sequel_model/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
22 changes: 22 additions & 0 deletions spec/sequel_model/record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 34867d6

Please sign in to comment.