Skip to content

Commit

Permalink
Merge pull request #1 from kraterllc/master
Browse files Browse the repository at this point in the history
Modifications to pagination
  • Loading branch information
sumskyi committed Jul 2, 2011
2 parents 1e7cee3 + e6ff1ca commit 748f9cf
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 31 deletions.
8 changes: 5 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ HAML pagination templates for Padrino framework:

== Usage

copy it to your mounted app in Padrino app
Copy it to your mounted app in Padrino app. Make sure to change app/helpers/pagination_helper.rb to use your application name.

In your template:

= paginate('posts', 7890, :current_page => 1)
= paginate(:posts, :index, 7890, :current_page => 1)

or with options:

= paginate(:articles, total, :current_page => page, :per_page => 12, :template => :brutal)
= paginate(:articles, :list, total, :current_page => page, :per_page => 12, :template => :brutal)


== Options:

The helper uses the link style of url(:controller, :action, :page => page) so that way it can be flexible for your controller schema.

current_page:: current page (default 1)
per_page:: items per page (default 20)
template:: used template (brutal / classic / digg / extended / punbb)
Expand Down
7 changes: 4 additions & 3 deletions app/helpers/pagination_helper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
PadrinoDybil.helpers do
def paginate(link, total, opts)
def paginate(controller, action, total, opts)
opts = {
:current_page => 1,
:per_page => 20,
:template => 'punbb' # 'extended', 'classic', 'brutal', 'digg'
}.merge!(opts)

opts[:link] = link

opts[:controller] = controller
opts[:action] = action

opts[:total_pages] = (total/opts[:per_page].to_f).ceil

opts[:previous_page] = opts[:current_page] <= 1 ? nil : opts[:current_page] - 1
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/pagination/_brutal.haml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
- (1..total_pages).each do |page|
=link_to page, "/#{link}/#{page}"
=link_to page, url(controller, action, params.merge("page" => page))
10 changes: 5 additions & 5 deletions app/views/shared/pagination/_classic.haml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
- if current_page > 1
=link_to "&lsaquo; First", "/#{link}/1"
=link_to "&lsaquo; First", url(controller, action, params.merge("page" => 1))

- if previous_page
=link_to "&laquo;", "/#{link}/#{previous_page}"
=link_to "&laquo;", url(controller, action, params.merge("page" => previous_page))

- (1..total_pages).each do |page|
- if page == current_page
= page
- else
=link_to page, "/#{link}/#{page}"
=link_to page, url(controller, action, params.merge("page" => page))

- if next_page
=link_to '&raquo;', "/#{link}/#{next_page}"
=link_to '&raquo;', url(controller, action, params.merge("page" => next_page))

- if last_page
=link_to 'Last &rsaquo;', "/#{link}/#{last_page}"
=link_to 'Last &rsaquo;', url(controller, action, params.merge("page" => last_page))
28 changes: 14 additions & 14 deletions app/views/shared/pagination/_digg.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- if previous_page
=link_to '&laquo; Prev', "/#{link}/#{previous_page}"
=link_to '&laquo; Prev', url(controller, action, params.merge("page" => previous_page))
- else
&laquo; Prev

Expand All @@ -8,42 +8,42 @@
- if page == current_page
= page
- else
=link_to page, "/#{link}/#{page}"
=link_to page, url(controller, action, params.merge("page" => page))

- elsif current_page < 9
- (1..10).each do |page|
- if page == current_page
= page
- else
=link_to page, "/#{link}/#{page}"
=link_to page, url(controller, action, params.merge("page" => page))
&hellip;
=link_to total_pages-1, "/#{link}/#{total_pages-1}"
=link_to total_pages, "/#{link}/#{total_pages}"
=link_to total_pages-1, url(controller, action, params.merge("page" => total_pages)-1)
=link_to total_pages, url(controller, action, params.merge("page" => total_pages))

- elsif current_page > total_pages - 8
=link_to 1, "/#{link}/1"
=link_to 2, "/#{link}/2"
=link_to 1, url(controller, action, params.merge("page" => 1))
=link_to 2, url(controller, action, params.merge("page" => 2))
&hellip;
- (total_pages-9..total_pages).each do |page|
- if page == current_page
= page
- else
=link_to page, "/#{link}/#{page}"
=link_to page, url(controller, action, params.merge("page" => page))

- else
=link_to 1, "/#{link}/1"
=link_to 2, "/#{link}/2"
=link_to 1, url(controller, action, params.merge("page" => 1))
=link_to 2, url(controller, action, params.merge("page" => 2))
&hellip;
- (current_page-5..current_page+5).each do |page|
- if page == current_page
= page
- else
=link_to page, "/#{link}/#{page}"
=link_to page, url(controller, action, params.merge("page" => page))
&hellip;
=link_to total_pages-1, "/#{link}/#{total_pages-1}"
=link_to total_pages, "/#{link}/#{total_pages}"
=link_to total_pages-1, url(controller, action, params.merge("page" => total_pages)-1)
=link_to total_pages, url(controller, action, params.merge("page" => total_pages))

- if next_page
=link_to 'Next &raquo;', "/#{link}/#{next_page}"
=link_to 'Next &raquo;', url(controller, action, params.merge("page" => next_page))
- else
Next &raquo;
4 changes: 2 additions & 2 deletions app/views/shared/pagination/_extended.haml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
- if previous_page
=link_to '&laquo; Prev', "/#{link}/#{previous_page}"
=link_to '&laquo; Prev', url(controller, action, params.merge("page" => previous_page))
- else
&laquo; Prev
| Page #{current_page} of #{total_pages}
| items #{current_first_item} &ndash; #{current_last_item} of #{total_items}

- if next_page
=link_to 'Next &raquo', "/#{link}/#{next_page}"
=link_to 'Next &raquo', url(controller, action, params.merge("page" => next_page))
- else
Next &raquo;
6 changes: 3 additions & 3 deletions app/views/shared/pagination/_punbb.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pages:
- if current_page > 3
=link_to 1, "/#{link}/1"
=link_to 1, url(controller, action, params.merge("page" => 1))
- if current_page != 4
&hellip;

Expand All @@ -9,10 +9,10 @@ Pages:
- if page == current_page
= page
- else
=link_to page, "/#{link}/#{page}"
=link_to page, url(controller, action, params.merge("page" => page))

- if current_page <= total_pages-3
- if current_page != total_pages-3
&hellip;
=link_to total_pages, "/#{link}/#{total_pages}"
=link_to total_pages, url(controller, action, params.merge("page" => total_pages))

0 comments on commit 748f9cf

Please sign in to comment.