Skip to content

Commit

Permalink
Added Model#reload as alias to Model#refresh.
Browse files Browse the repository at this point in the history
git-svn-id: https://ruby-sequel.googlecode.com/svn/trunk/sequel_model@765 ff5234a3-732a-0410-8ecb-13b9ad946718
  • Loading branch information
ciconia committed Jan 17, 2008
1 parent 054daea commit ed2eb7f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
=== SVN

* Added Model#reload as alias to Model#refresh.

* Changed Model.create to accept a block (#126).

* Rewrote validations.
Expand Down
17 changes: 1 addition & 16 deletions TODO
@@ -1,9 +1,3 @@
* Model#reload as alias to Model#refresh.

* Rewrite validations.

* Model#save! to save regardless of validations. Model#save should check valid?.

* Observer pattern:

class MyObserver < Sequel::Observer
Expand All @@ -14,14 +8,7 @@
end
end

# block form of Model.new:

MyModel.new do |model
model.field = value
model.save
end

# Smarter one_to_one: stuff like:
* Smarter one_to_one: stuff like:

# assuming the Author's primary key is :name
post = Post.create(:name => 'The Lover', :author => Author['A.B. Yehoshua'])
Expand All @@ -33,6 +20,4 @@
post = Post.create(:name => 'The Lover', :author => 'A.B. Yehoshua') #=> fetches the author record
post.set(:author => 'A.B. Yehoshua')

* adapter specs for Model

* many_to_many smart relationships.
1 change: 1 addition & 0 deletions lib/sequel_model/record.rb
Expand Up @@ -251,6 +251,7 @@ def refresh
@values = this.first || raise(Error, "Record not found")
self
end
alias_method :reload, :refresh

# Like delete but runs hooks before and after delete.
def destroy
Expand Down
30 changes: 30 additions & 0 deletions spec/record_spec.rb
Expand Up @@ -467,3 +467,33 @@ def columns; [:x]; end
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (333)", "SELECT * FROM items WHERE (id IN ('INSERT INTO items (x) VALUES (333)')) LIMIT 1"]
end
end

describe Sequel::Model, "#refresh" do
setup do
MODEL_DB.reset
@c = Class.new(Sequel::Model(:items)) do
def columns; [:x]; end
end
end

specify "should reload the instance values from the database" do
@m = @c.new(:id => 555)
@m[:x] = 'blah'
@m.this.should_receive(:first).and_return({:x => 'kaboom', :id => 555})
@m.refresh
@m[:x].should == 'kaboom'
end

specify "should raise if the instance is not found" do
@m = @c.new(:id => 555)
@m.this.should_receive(:first).and_return(nil)
proc {@m.refresh}.should raise_error(Sequel::Error)
end

specify "should be aliased by #reload" do
@m = @c.new(:id => 555)
@m.this.should_receive(:first).and_return({:x => 'kaboom', :id => 555})
@m.reload
@m[:x].should == 'kaboom'
end
end

0 comments on commit ed2eb7f

Please sign in to comment.