Skip to content

Commit

Permalink
Adding skip method to skip an arbitrary record count
Browse files Browse the repository at this point in the history
Can be used in conjunction with page and per to offset by an
amount less than the page size.
  • Loading branch information
aaronjensen authored and amatsuda committed Aug 17, 2011
1 parent 7532ecc commit 5575beb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.rdoc
Expand Up @@ -61,6 +61,12 @@ Then bundle:
User.page(7).per(50)
Note that the +per+ scope is not directly defined on the models but is just a method defined on the page scope. This is absolutely reasonable because you will never actually use +per_page+ without specifying the +page+ number.

* the +skip+ scope

Occasionally you need to skip a number of records that is not a multiple of the page size.
User.page(7).per(50).skip(3)
Note that the +skip+ scope is not directly defined on the models but is just a method defined on the page scope. This is absolutely reasonable because you can use +offset+ otherwise

=== General configuration options

You can configure the following default values by overriding these values using <tt>Kaminari.configure</tt> method.
Expand Down
4 changes: 4 additions & 0 deletions lib/kaminari/models/page_scope_methods.rb
Expand Up @@ -12,6 +12,10 @@ def per(num)
end
end

def skip(num)
limit(limit_value).offset(offset_value + num.to_i)
end

# Total number of pages
def num_pages
(total_count.to_f / limit_value).ceil
Expand Down
8 changes: 8 additions & 0 deletions spec/models/scopes_spec.rb
Expand Up @@ -55,6 +55,14 @@
end
end

describe '#skip' do
context 'page 1 per 5 skip 1' do
subject { User.page(1).per(5).skip(1) }
it { should have(5).users }
its('first.name') { should == 'user002' }
end
end

describe '#num_pages' do
context 'per 25 (default)' do
subject { User.page }
Expand Down

0 comments on commit 5575beb

Please sign in to comment.