Skip to content

Commit

Permalink
pagination when either will_paginate or kaminari present
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg committed Jan 30, 2012
1 parent 29d2ea7 commit 293fb3e
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem 'rails_autolink', '>=1.0.4'
gem 'jquery-rails', '>=1.0.0'

# gem 'will_paginate', '>=3.0.2'
# gem 'kaminari', '>=0.0.0'

group :test do
gem 'sqlite3'
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Since ComfyBlog is an engine, it allows you to completely overwrite views. It's
create `app/views/blog/posts/index.html.erb` (could be HAML or whatever you want) and structure
it in any way you want. There's also `show.html.erb` and `_post.html.erb` available.

Pagination is done using either [WillPaginate](https://github.com/mislav/will_paginate) or [Kaminari](https://github.com/amatsuda/kaminari). Add either in your Gemfile.
You can control number of posts per page by adjusting this config:

config.posts_per_page = 10
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/admin/blog/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Admin::Blog::PostsController < Admin::Blog::BaseController
def index
@posts = if defined? WillPaginate
Blog::Post.paginate :page => params[:page]
elsif defined? Kaminari
Blog::Post.page params[:page]
else
Blog::Post.all
end
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/blog/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def index
f.html do
@posts = if defined? WillPaginate
scope.paginate :per_page => ComfyBlog.config.posts_per_page, :page => params[:page]
elsif defined? Kaminari
scope.page(params[:page]).per(ComfyBlog.config.posts_per_page)
else
scope
end
Expand Down
4 changes: 3 additions & 1 deletion app/views/admin/blog/posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<% end %>
</ul>

<% if defined? WillPaginate %>
<% if defined?(WillPaginate) %>
<%= will_paginate @posts %>
<% elsif defined?(Kaminari)%>
<%= paginate @posts %>
<% end %>
4 changes: 3 additions & 1 deletion app/views/blog/posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<%= render :partial => 'post', :object => post %>
<% end %>
<% if defined? WillPaginate %>
<% if defined?(WillPaginate) %>
<%= will_paginate @posts, :previous_label => '« Older Posts', :next_label => 'Newer Posts »', :page_links => false %>
<% elsif defined?(Kaminari) %>
<%= paginate @posts %>
<% end %>
2 changes: 1 addition & 1 deletion test/functional/admin/blog/posts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_get_index
end

def test_get_index_with_pagination
if defined? WillPaginate
if defined?(WillPaginate) || defined?(Kaminari)
get :index, :page => 99
assert_response :success
assert assigns(:posts)
Expand Down
2 changes: 1 addition & 1 deletion test/functional/blog/posts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_get_index_with_unpublished
end

def test_get_index_with_pagination
if defined? WillPaginate
if defined?(WillPaginate) || defined?(Kaminari)
get :index, :page => 99
assert_response :success
assert_equal 0, assigns(:posts).size
Expand Down

0 comments on commit 293fb3e

Please sign in to comment.