Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem with find(:first) not accepting options #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/simply_stored/couch/finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def find(*args)
end
when :first
if with_deleted || !soft_deleting_enabled?
CouchPotato.database.view(all_documents(:limit => 1, :include_docs => true)).first
CouchPotato.database.view(all_documents(options.update(:limit => 1, :include_docs => true))).first
else
CouchPotato.database.view(all_documents_without_deleted(:limit => 1, :include_docs => true)).first
CouchPotato.database.view(all_documents_without_deleted(options.update(:limit => 1, :include_docs => true))).first
end
else
raise SimplyStored::Error, "Can't load record without an id" if what.nil?
Expand All @@ -42,7 +42,8 @@ def first(*args)
end

def last(*args)
find(:first, {:order => :desc}, *args)
options = args.last.is_a?(Hash) ? args.pop : {}
find(:first, options.update(:order => :desc), *args)
end

def count(options = {})
Expand All @@ -57,4 +58,4 @@ def count(options = {})
end
end
end
end
end
70 changes: 54 additions & 16 deletions test/finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,40 @@ class FinderTest < Test::Unit::TestCase
end

context "to find one instance" do
should 'return one user when calling first' do
user = User.create(:title => "Mr.")
assert_equal user, User.first
end

should 'understand the order' do
assert_nothing_raised do
User.first(:order => :desc)
context "when multiple instances exist" do
setup do
User.create(:title => "Mr.")
User.create(:title => "Mrs.")
@all = User.all
@first = @all.first
@last = @all.last
end

should 'return the first user for #find' do
assert_equal @first, User.find(:first)
end

should 'return the first user for #first' do
assert_equal @first, User.first
end

should 'return the last user for #find and descending order' do
assert_equal @last, User.find(:first, :order => :desc)
end

should 'return the last user for #last' do
assert_equal @last, User.last
end
end

should 'find the last as a reverse first' do
User.expects(:find).with(:first, :order => :desc)
User.last
end

should 'return nil when no user found' do
assert_nil User.first

context 'When no rows exist' do
should 'return nil for first' do
assert_nil User.first
end
end
end



context "when finding with just an identifier" do
should "find just one instance" do
Expand Down Expand Up @@ -183,6 +197,30 @@ class FinderTest < Test::Unit::TestCase
end
end
end

context 'The #first method' do
should 'pass default arguments to #find' do
User.expects(:find).with(:first)
User.first
end

should 'merge passed options to #find' do
User.expects(:find).with(:first, :limit => 2)
User.first(:limit => 2)
end
end

context 'The #last method' do
should 'pass default arguments to #last' do
User.expects(:find).with(:first, :order => :desc)
User.last
end

should 'merge passed options to #last' do
User.expects(:find).with(:first, :order => :desc, :limit => 2)
User.last(:limit => 2)
end
end
end

end