From 45dd308186517735e0a3c9233ac23f7cf733cbbb Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Mon, 9 Apr 2012 16:46:08 +0200 Subject: [PATCH] Display comments count --- app/models/comment.rb | 2 +- app/views/asciicasts/_preview.html.erb | 4 ++++ db/migrate/20120409142151_add_comments_count_to_asciicast.rb | 5 +++++ db/schema.rb | 3 ++- 4 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20120409142151_add_comments_count_to_asciicast.rb diff --git a/app/models/comment.rb b/app/models/comment.rb index 0209dd9..d2adef4 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -5,7 +5,7 @@ class Comment < ActiveRecord::Base validates :user_id, :presence => true belongs_to :user - belongs_to :asciicast + belongs_to :asciicast, :counter_cache => true attr_accessible :body diff --git a/app/views/asciicasts/_preview.html.erb b/app/views/asciicasts/_preview.html.erb index e5fe85b..3153761 100644 --- a/app/views/asciicasts/_preview.html.erb +++ b/app/views/asciicasts/_preview.html.erb @@ -19,4 +19,8 @@ <% if asciicast.description.present? %>

<%= asciicast.description %>

<% end %> + + <% if asciicast.comments_count > 0 %> +

<%= link_to "#{asciicast.comments_count} comments", asciicast_path(asciicast, :anchor => 'comments') %>

+ <% end %> diff --git a/db/migrate/20120409142151_add_comments_count_to_asciicast.rb b/db/migrate/20120409142151_add_comments_count_to_asciicast.rb new file mode 100644 index 0000000..1bcb712 --- /dev/null +++ b/db/migrate/20120409142151_add_comments_count_to_asciicast.rb @@ -0,0 +1,5 @@ +class AddCommentsCountToAsciicast < ActiveRecord::Migration + def change + add_column :asciicasts, :comments_count, :integer, :null => false, :default => 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 28b549c..737b87b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120406152447) do +ActiveRecord::Schema.define(:version => 20120409142151) do create_table "asciicasts", :force => true do |t| t.integer "user_id" @@ -35,6 +35,7 @@ ActiveRecord::Schema.define(:version => 20120406152447) do t.boolean "featured", :default => false t.string "username" t.text "snapshot" + t.integer "comments_count", :default => 0, :null => false end add_index "asciicasts", ["created_at"], :name => "index_asciicasts_on_created_at"