Skip to content

Commit

Permalink
Added first/last/all aliases for equivalent find scopes
Browse files Browse the repository at this point in the history
Just a copy from Active Record (with tests). Each is a warpper function for
the equivalent scoped call to find eg first is a wrapper for find(:first)

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
taryn authored and josh committed Aug 21, 2009
1 parent 8bc3a14 commit ce61a6b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
22 changes: 22 additions & 0 deletions activeresource/lib/active_resource/base.rb
Expand Up @@ -611,6 +611,28 @@ def find(*arguments)
end
end


# A convenience wrapper for <tt>find(:first, *args)</tt>. You can pass
# in all the same arguments to this method as you can to
# <tt>find(:first)</tt>.
def first(*args)
find(:first, *args)
end

# A convenience wrapper for <tt>find(:last, *args)</tt>. You can pass
# in all the same arguments to this method as you can to
# <tt>find(:last)</tt>.
def last(*args)
find(:last, *args)
end

# This is an alias for find(:all). You can pass in all the same
# arguments to this method as you can to <tt>find(:all)</tt>
def all(*args)
find(:all, *args)
end


# Deletes the resources with the ID in the +id+ parameter.
#
# ==== Options
Expand Down
38 changes: 38 additions & 0 deletions activeresource/test/cases/finder_test.rb
Expand Up @@ -126,18 +126,56 @@ def test_find_all
assert_equal "David", all.last.name
end

def test_all
all = Person.all
assert_equal 2, all.size
assert_kind_of Person, all.first
assert_equal "Matz", all.first.name
assert_equal "David", all.last.name
end

def test_all_with_params
all = StreetAddress.all(:params => { :person_id => 1 })
assert_equal 1, all.size
assert_kind_of StreetAddress, all.first
end

def test_find_first
matz = Person.find(:first)
assert_kind_of Person, matz
assert_equal "Matz", matz.name
end

def test_first
matz = Person.first
assert_kind_of Person, matz
assert_equal "Matz", matz.name
end

def test_first_with_params
addy = StreetAddress.first(:params => { :person_id => 1 })
assert_kind_of StreetAddress, addy
assert_equal '12345 Street', addy.street
end

def test_find_last
david = Person.find(:last)
assert_kind_of Person, david
assert_equal 'David', david.name
end

def test_last
david = Person.last
assert_kind_of Person, david
assert_equal 'David', david.name
end

def test_last_with_params
addy = StreetAddress.last(:params => { :person_id => 1 })
assert_kind_of StreetAddress, addy
assert_equal '12345 Street', addy.street
end

def test_find_by_id_not_found
assert_raise(ActiveResource::ResourceNotFound) { Person.find(99) }
assert_raise(ActiveResource::ResourceNotFound) { StreetAddress.find(1) }
Expand Down

0 comments on commit ce61a6b

Please sign in to comment.