Coder Social home page Coder Social logo

dm-pagination's Introduction

DataMapper::Pager

Better paging solution for DataMapper for DM >= 0.10.1

About

Not to be confused with the dm-pagination gem (a similar gem lacking in functionality) dm-pager is a new, fresh, and feature rich pagination implementation for DataMapper. Due to Github's gem builder being destroyed, we were forced to rename our gem to 'dm-pager' which is now available on Gemcutter.org.

Installation

Install Gemcutter and execute: $ sudo gem install dm-pager

Examples

Page 1 for all items, defaulting to 6 per page: Item.page // => [1, 2, 3, 4, 5, 6]

Page 2 for all items, 6 per page, with only 3 items remaining Item.page 2 // => [7, 8, 9]

Page 3, 2 per page: Item.page 3, :per_page => 2 // => [5, 6]

Accessing the pager instance: Item.page(1, :per_page => 4).pager // => #<DataMapper::Pager:0x1610f20 @per_page=4, @next_page=2, @total_pages=3, @total=10, @current_page=1>

Converting to HTML: Item.page(2).pager.to_html('/items') // => "<div class="pager">..."

Alter the number of intermediate numbered links displayed, defaults to 7 Item.page(2).pager.to_html('/items', :size => 3) // => "<div class="pager">..."

Output

Output is displayed in a format similar to below, although with CSS you may hide anything you wish to remove. There is no need to provide an API for this.

Item.page(2).pager.to_html('/items', :size => 3)
// => First Previous 1 [2] 3 ... Next Last

Item.page.pager.to_html '/items'
// => [1] 2 3 4 5 6 7 ... Next Last

Item.page(5).pager.to_html('/items', :size => 5)
// => First Previous ... 3 4 [5] 6 7 ... Next Last

Markup

Sample query: Item.page(2, :per_page => 2).pager.to_html("/items", :size => 3)

Sample markup:

<div class="pager">
  <a href="/items?page=1" class="first">First</a>
  <a href="/items?page=1" class="previous">Previous</a>
  <ul>
    <li class="page-1 first"><a href="/items?page=1" class="">1</a></li>
    <li class="active page-2"><a href="/items?page=2" class="">2</a></li>
    <li class="page-3 last"><a href="/items?page=3" class="">3</a></li>
    <li class="more">...</li>
  </ul>
  <a href="/items?page=3" class="next">Next</a>
  <a href="/items?page=5" class="last">Last</a>
</div>

Running Specs

Autospec: $ autospec

Rake: $ rake spec $ rake spec:verbose $ rake spec:select SPEC=spec/unit/pager_spec.rb

RSpec: $ spec --color spec

Authors

License

(The MIT License)

Copyright (c) 2009 TJ Holowaychuk [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, an d/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

dm-pagination's People

Contributors

josegonzalez avatar kyledrake avatar marcoow avatar snusnu avatar tj avatar tmann-ttc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

dm-pagination's Issues

Remove css defaults

With the exception of the root .pager class, this is pretty useless. We could leave out all classes within the pager's root div and it would still be really style-able with selectors these days, I think it is mostly cruft to have options for all of these

PostgresError

I'm getting this when running on Heroku because of Postgres database:

PostgresError - ERROR: column "medium.sector" must appear in the GROUP BY clause or be used in an aggregate function

Query: SELECT COUNT(*) FROM "medium" ORDER BY "sector" DESC:

BTW: running it locally with sqlite3 is working like a charm, thanks for the gem!

Centering with css

ul and ol expand full width so maybe just use anchor tags without a list...

For pager, if :last_page or :first_page text is empty, don't show those links

This would provide an easy way to hide the first_page and last_page links

def last_link
  li 'last jump', link_to(total_pages, option(:last_text)) if next_page && !option(:last_text).empty?
end

##
# First link.

def first_link
  li 'first jump', link_to(1, option(:first_text)) if previous_page && !option(:first_text).empty?
end

rename / publish gem

GAH thanks github.. need to rename the gem now since some crappy pagination gem is named dm-pagination on gemcutter

Order

Query order overrides default order set in models.
lib/dm-pager/pagination.rb:
line 43: :order => [:id.desc]

This overrides default ordering set in models, eg:
(inside model class):
default_scope(:default).update(:order => [:created_at.desc])

Append QUERY_STRING for the .to_html method

Let's say I have this:
filter?filter_by=description&filter_string=zen-noh&page=1&submit=Find+Projects

I would like .to_html append this, but replace "&page=1" with whatever is appropriate for the links.

Allow clean uris

For example we may want:
/users/\d+

instead of
/users?page=\d+

:total records count broken for queries with limits

In def page

:total => count(query)

total will not reflect an accurate count of the query if query contains :limit.

For instance, if @posts(:body.ne=>nil) returns 1,333 records and we insert a limit @posts(:body.ne=>nil, :limit=>1000), :total will be 1333, not 1000. This inaccuracy creates extra page links, including last that refer to non-existent pages and records.

I'll file a issue with datamapper on count, but dm-pager should return an accurate set of links regardless.

Add more specs for intermediate pages

we cover first / last and a "middle" page. However we still need some specs to ensure that when on the second page we still start the intermediate links from 1..x, same with the trailing end of the pages 8..10 when :size is 10 should display 8 through 10

to_json support

Is it possible to add a to_json method, returned the entities and pagination information? Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.