Deprecate passing offset to find_nth

All uses of the `offset` are passing `offset_index`. Better to push
down the `offset` consideration into `find_nth`.

This also works toward enabling `find_nth_with_limit` to take
advantage of the `loaded?` state of the relation.
This commit is contained in:
Ben Woosley 2015-10-23 14:59:31 -05:00
parent 1e42a4dfb4
commit 16a476e4f8
2 changed files with 21 additions and 8 deletions

@ -1,3 +1,8 @@
* Deprecate sending the `offset` argument to `find_nth`. Please use the
`offset` method on relation instead.
*Ben Woosley*
## Rails 5.0.0.beta1 (December 18, 2015) ##
* Order the result of `find(ids)` to match the passed array, if the relation

@ -119,7 +119,7 @@ def first(limit = nil)
if limit
find_nth_with_limit(offset_index, limit)
else
find_nth(0, offset_index)
find_nth 0
end
end
@ -169,7 +169,7 @@ def last!
# Person.offset(3).second # returns the second object from OFFSET 3 (which is OFFSET 4)
# Person.where(["user_name = :u", { u: user_name }]).second
def second
find_nth(1, offset_index)
find_nth 1
end
# Same as #second but raises ActiveRecord::RecordNotFound if no record
@ -185,7 +185,7 @@ def second!
# Person.offset(3).third # returns the third object from OFFSET 3 (which is OFFSET 5)
# Person.where(["user_name = :u", { u: user_name }]).third
def third
find_nth(2, offset_index)
find_nth 2
end
# Same as #third but raises ActiveRecord::RecordNotFound if no record
@ -201,7 +201,7 @@ def third!
# Person.offset(3).fourth # returns the fourth object from OFFSET 3 (which is OFFSET 6)
# Person.where(["user_name = :u", { u: user_name }]).fourth
def fourth
find_nth(3, offset_index)
find_nth 3
end
# Same as #fourth but raises ActiveRecord::RecordNotFound if no record
@ -217,7 +217,7 @@ def fourth!
# Person.offset(3).fifth # returns the fifth object from OFFSET 3 (which is OFFSET 7)
# Person.where(["user_name = :u", { u: user_name }]).fifth
def fifth
find_nth(4, offset_index)
find_nth 4
end
# Same as #fifth but raises ActiveRecord::RecordNotFound if no record
@ -233,7 +233,7 @@ def fifth!
# Person.offset(3).forty_two # returns the forty-second object from OFFSET 3 (which is OFFSET 44)
# Person.where(["user_name = :u", { u: user_name }]).forty_two
def forty_two
find_nth(41, offset_index)
find_nth 41
end
# Same as #forty_two but raises ActiveRecord::RecordNotFound if no record
@ -488,17 +488,25 @@ def find_take
end
end
def find_nth(index, offset)
def find_nth(index, offset = nil)
if loaded?
@records[index]
else
if offset
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Passing an offset argument to find_nth is deprecated,
please use Relation#offset instead.
MSG
else
offset = offset_index
end
offset += index
@offsets[offset] ||= find_nth_with_limit(offset, 1).first
end
end
def find_nth!(index)
find_nth(index, offset_index) or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql(@klass.arel_engine)}]")
find_nth(index) or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql(@klass.arel_engine)}]")
end
def find_nth_with_limit(offset, limit)