Skip to content

Commit

Permalink
fix first for paginated AR Relations
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Jan 10, 2013
1 parent 25582e5 commit 6d35492
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/will_paginate/active_record.rb
Expand Up @@ -39,6 +39,17 @@ def limit(num)
end
end

# dirty hack to enable `first` after `limit` behavior above
def first(*args)
if current_page
rel = clone
rel.current_page = nil
rel.first(*args)
else
super
end
end

def offset(value = nil)
if value.nil? then offset_value
else super(value)
Expand Down
12 changes: 12 additions & 0 deletions spec/finders/active_record_spec.rb
Expand Up @@ -92,6 +92,18 @@
rel.offset.should == 6
end

it "supports #first" do
rel = Developer.order('id').page(2).per_page(4)
rel.first.should == users(:dev_5)
rel.first(2).should == users(:dev_5, :dev_6)
end

it "supports #last" do
rel = Developer.order('id').page(2).per_page(4)
rel.last.should == users(:dev_8)
rel.page(3).last.should == users(:poor_jamis)
end

it "keeps pagination data after 'scoped'" do
rel = Developer.page(2).scoped
rel.per_page.should == 10
Expand Down

0 comments on commit 6d35492

Please sign in to comment.