Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ gemspec
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.

gem 'kaminari'

group :test do
gem "capybara", "~> 2.2.0"
gem 'kaminari-rspec'
end

# To use debugger
gem 'byebug'
gem 'pry'
2 changes: 1 addition & 1 deletion app/controllers/blog_posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class BlogPostsController < ApplicationController
before_filter :find_blog_post!, :only => :show

def index
@blog_posts = BlogPost.all
@blog_posts = BlogPost.page(params[:page])
end

def filter
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/blog_posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
</tr>
<% end %>
</table>
<%= paginate(@blog_posts) %>
<% else %>
<p class="simple-blog-posts-missing"><%= t('.no_published_posts') %></p>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions app/views/blog_posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<% end %>
</div>
<% end %>
<%= paginate(@blog_posts) %>
<% else %>
<p class="simple-blog-posts-missing"><%= t('.no_published_posts') %></p>
<% end %>
Expand Down
10 changes: 10 additions & 0 deletions config/initializers/kaminari_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Kaminari.configure do |config|
config.default_per_page = 10
config.max_per_page = nil
config.window = 4
config.outer_window = 0
config.left = 0
config.right = 0
config.page_method_name = :page
config.param_name = :page
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace :admin do
resources :blog_posts do
get "/get_tags" => "blog_posts#get_tags", as: :get_tags, on: :collection
get '/blog/:page', action: :index, on: :collection
end
resources :blog_images
end
Expand Down
1 change: 1 addition & 0 deletions spec/controllers/admin/blog_posts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ module Admin
get :index
expect(assigns(:blog_posts)).to eq([blog_post])
end

end

describe "#destroy" do
Expand Down
1 change: 1 addition & 0 deletions spec/controllers/blog_posts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
get :index
expect(response).to render_template('blog_posts/index')
end

end

describe "#filter" do
Expand Down
8 changes: 4 additions & 4 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

ActiveRecord::Schema.define(version: 20150115103932) do

create_table "blog_images", force: true do |t|
create_table "blog_images", force: :cascade do |t|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why this is changed...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure either, I didn't add another controller.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why: http://edgeguides.rubyonrails.org/4_2_release_notes.html#active-record-notable-changes

SchemaDumper uses force: :cascade on create_table. This makes it possible to reload a schema when foreign keys are in place.

t.integer "blog_post_id"
t.string "image"
end

create_table "blog_posts", force: true do |t|
create_table "blog_posts", force: :cascade do |t|
t.string "title"
t.text "body"
t.text "description"
Expand All @@ -30,7 +30,7 @@
t.string "language"
end

create_table "taggings", force: true do |t|
create_table "taggings", force: :cascade do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
Expand All @@ -43,7 +43,7 @@
add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"

create_table "tags", force: true do |t|
create_table "tags", force: :cascade do |t|
t.string "name"
t.integer "taggings_count", default: 0
end
Expand Down
14 changes: 14 additions & 0 deletions spec/features/paginate_posts_index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'spec_helper'

describe 'Paginate posts index' do
describe 'index is paginated' do
before do
POSTS_PER_PAGE = 25
POSTS_PER_PAGE.times { create(:blog_post) }
visit blog_posts_path
end
it "limits the number of posts to #{BlogPost::POSTS_PER_PAGE}" do
expect(page).to have_selector('.blog-post', count: BlogPost::POSTS_PER_PAGE)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should have an it block that describes your test. For example:

it "limits the number of posts to #{BlogPost::POSTS_PER_PAGE}" do
  expect(page).to have_selector('.blog-post', count: BlogPost::POSTS_PER_PAGE)
end

end
end