Created
          February 24, 2016 22:47 
        
      - 
      
- 
        Save worace/02eb67f7edf5dece1cec to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb | |
| index 591a04a..7142c15 100644 | |
| --- a/app/controllers/articles_controller.rb | |
| +++ b/app/controllers/articles_controller.rb | |
| @@ -1,10 +1,11 @@ | |
| class ArticlesController < ApplicationController | |
| def show | |
| - @article = Article.find(params[:id]) | |
| + @article = Article.includes(:comments).find(params[:id]) | |
| end | |
| def index | |
| - @articles, @tag = Article.search_by_tag_name(params[:tag]) | |
| + @page = [params[:page].to_i, 1].max | |
| + @articles, @tag = Article.search_by_tag_name(params[:tag], @page) | |
| end | |
| def new | |
| diff --git a/app/models/article.rb b/app/models/article.rb | |
| index 739795f..5cff73c 100644 | |
| --- a/app/models/article.rb | |
| +++ b/app/models/article.rb | |
| @@ -39,9 +39,14 @@ class Article < ActiveRecord::Base | |
| Article.select(:id).collect{|a| a.id} | |
| end | |
| - def self.search_by_tag_name(tag_name) | |
| + def self.paginate(page, limit) | |
| + start = (page - 1) * limit | |
| + Article.includes(:tags, :comments).offset(start).limit(limit) | |
| + end | |
| + | |
| + def self.search_by_tag_name(tag_name, page = 1) | |
| if tag_name.blank? | |
| - [Article.all, nil] | |
| + [Article.paginate(page, 10), nil] | |
| else | |
| tag = Tag.find_by_name(tag_name) | |
| tag ? [tag.articles, tag] : [[], nil] | |
| diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb | |
| index 3001e82..480efde 100644 | |
| --- a/app/views/articles/index.html.erb | |
| +++ b/app/views/articles/index.html.erb | |
| @@ -2,9 +2,15 @@ | |
| <%= link_to "New Article", new_article_path, :class => 'new_article' %> | |
| +<div style="margin: 90px 0;"> | |
| +<%= link_to "Mo' Articles", articles_path(page: @page + 1) %> | |
| +</div> | |
| + | |
| +<% cache("artile-index-tag-sidebar") do %> | |
| <div id="sidebar"> | |
| Filter by Tag: <%= tag_links(Tag.all) %> | |
| </div> | |
| +<% end %> | |
| <ul id="articles"> | |
| <% @articles.each do |article| %> | |
| @@ -15,7 +21,7 @@ | |
| <%= edit_icon(article) %> | |
| <%= delete_icon(article) %> | |
| </span> | |
| - <%= pluralize article.comments.count, "Comment" %> | |
| + <%= pluralize article.comments.size, "Comment" %> | |
| </li> | |
| <% end %> | |
| </ul> | |
| diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb | |
| index 552e0e2..1aea333 100644 | |
| --- a/app/views/articles/show.html.erb | |
| +++ b/app/views/articles/show.html.erb | |
| @@ -11,7 +11,7 @@ | |
| <%= link_to "Back to All Articles", articles_path %> | |
| </div> | |
| -<h3><%= pluralize @article.comments.count, "Comment" %></h3> | |
| +<h3><%= pluralize @article.comments.size, "Comment" %></h3> | |
| <div id='comments'> | |
| <% @article.comments.each do |comment| %> | |
| diff --git a/db/schema.rb b/db/schema.rb | |
| index 2e468db..babee1c 100644 | |
| --- a/db/schema.rb | |
| +++ b/db/schema.rb | |
| @@ -11,7 +11,7 @@ | |
| # | |
| # It's strongly recommended that you check this file into your version control system. | |
| -ActiveRecord::Schema.define(version: 20130408190802) do | |
| +ActiveRecord::Schema.define(version: 20160224215511) do | |
| # These are extensions that must be enabled in order to support this database | |
| enable_extension "plpgsql" | |
| @@ -42,6 +42,8 @@ ActiveRecord::Schema.define(version: 20130408190802) do | |
| t.datetime "updated_at" | |
| end | |
| + add_index "comments", ["article_id"], name: "index_comments_on_article_id", using: :btree | |
| + | |
| create_table "taggings", force: true do |t| | |
| t.integer "article_id" | |
| t.integer "tag_id" | |
| @@ -49,6 +51,9 @@ ActiveRecord::Schema.define(version: 20130408190802) do | |
| t.datetime "updated_at" | |
| end | |
| + add_index "taggings", ["article_id"], name: "index_taggings_on_article_id", using: :btree | |
| + add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id", using: :btree | |
| + | |
| create_table "tags", force: true do |t| | |
| t.string "name" | |
| t.datetime "created_at" | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment