From 55ed70529aefdb3029f5ab72169b2f7909b2f2a3 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 10 Dec 2006 18:35:48 +0000 Subject: [PATCH] added model Comment. comments can now be added on news. git-svn-id: http://redmine.rubyforge.org/svn/trunk@81 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/news_controller.rb | 16 ++++++++++++ app/models/comment.rb | 6 +++++ app/models/news.rb | 1 + app/views/news/show.rhtml | 24 ++++++++++++++++- app/views/projects/list_news.rhtml | 3 ++- db/migrate/010_create_comments.rb | 16 ++++++++++++ db/migrate/011_add_news_comments_count.rb | 9 +++++++ db/migrate/012_add_comments_permissions.rb | 11 ++++++++ lang/de.yml | 6 +++++ lang/en.yml | 6 +++++ lang/es.yml | 6 +++++ lang/fr.yml | 6 +++++ lib/tasks/load_default_data.rake | 4 +-- public/stylesheets/application.css | 2 +- test/fixtures/comments.yml | 10 ++++++++ test/fixtures/news.yml | 2 ++ test/unit/comment_test.rb | 30 ++++++++++++++++++++++ 17 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 app/models/comment.rb create mode 100644 db/migrate/010_create_comments.rb create mode 100644 db/migrate/011_add_news_comments_count.rb create mode 100644 db/migrate/012_add_comments_permissions.rb create mode 100644 test/fixtures/comments.yml create mode 100644 test/unit/comment_test.rb diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb index b50b59dc0..544b6eb8d 100644 --- a/app/controllers/news_controller.rb +++ b/app/controllers/news_controller.rb @@ -27,7 +27,23 @@ class NewsController < ApplicationController flash[:notice] = l(:notice_successful_update) redirect_to :action => 'show', :id => @news end + end + + def add_comment + @comment = Comment.new(params[:comment]) + @comment.author = logged_in_user + if @news.comments << @comment + flash[:notice] = l(:label_comment_added) + redirect_to :action => 'show', :id => @news + else + render :action => 'show' + end end + + def destroy_comment + @news.comments.find(params[:comment_id]).destroy + redirect_to :action => 'show', :id => @news + end def destroy @news.destroy diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 000000000..1ec7db630 --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,6 @@ +class Comment < ActiveRecord::Base + belongs_to :commented, :polymorphic => true, :counter_cache => true + belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' + + validates_presence_of :commented, :author, :comment +end diff --git a/app/models/news.rb b/app/models/news.rb index faafa7eef..f9ba010b0 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -18,6 +18,7 @@ class News < ActiveRecord::Base belongs_to :project belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' + has_many :comments, :as => :commented, :dependent => true, :order => "created_on" validates_presence_of :title, :description diff --git a/app/views/news/show.rhtml b/app/views/news/show.rhtml index 91df09f0f..890184b1a 100644 --- a/app/views/news/show.rhtml +++ b/app/views/news/show.rhtml @@ -13,4 +13,26 @@ <% end %> -<%= link_to_if_authorized l(:button_edit), :controller => 'news', :action => 'edit', :id => @news %> +

<%= link_to_if_authorized l(:button_edit), :controller => 'news', :action => 'edit', :id => @news %>

+ +
+

<%= l(:label_comment_plural) %>

+<% @news.comments.each do |comment| %> +<% next if comment.new_record? %> +

<%= format_time(comment.created_on) %> - <%= comment.author.name %>

+
+ <%= link_to_if_authorized l(:button_delete), {:controller => 'news', :action => 'destroy_comment', :id => @news, :comment_id => comment}, :confirm => l(:text_are_you_sure), :post => true %> +
+<%= simple_format(auto_link(h comment.comment))%> +<% end if @news.comments_count > 0 %> +
+ +<% if authorize_for 'news', 'add_comment' %> +

<%= l(:label_comment_add) %>

+<%= start_form_tag :action => 'add_comment', :id => @news %> +<%= error_messages_for 'comment' %> +


+<%= text_area 'comment', 'comment', :cols => 60, :rows => 6 %>

+<%= submit_tag l(:button_add) %> +<%= end_form_tag %> +<% end %> \ No newline at end of file diff --git a/app/views/projects/list_news.rhtml b/app/views/projects/list_news.rhtml index 6880de32f..59b737fc5 100644 --- a/app/views/projects/list_news.rhtml +++ b/app/views/projects/list_news.rhtml @@ -6,7 +6,8 @@ <% for news in @news %>
  • <%= link_to news.title, :controller => 'news', :action => 'show', :id => news %>
    <% unless news.summary.empty? %><%= news.summary %>
    <% end %> - <%= news.author.name %>, <%= format_time(news.created_on) %>
      + <%= news.author.name %>, <%= format_time(news.created_on) %>
    + <%= news.comments_count %> <%= lwr(:label_comment, news.comments_count).downcase %>
     
  • <% end %> diff --git a/db/migrate/010_create_comments.rb b/db/migrate/010_create_comments.rb new file mode 100644 index 000000000..322a019bc --- /dev/null +++ b/db/migrate/010_create_comments.rb @@ -0,0 +1,16 @@ +class CreateComments < ActiveRecord::Migration + def self.up + create_table :comments do |t| + t.column :commented_type, :string, :limit => 30, :default => "", :null => false + t.column :commented_id, :integer, :default => 0, :null => false + t.column :author_id, :integer, :default => 0, :null => false + t.column :comment, :text, :default => "", :null => false + t.column :created_on, :datetime, :null => false + t.column :updated_on, :datetime, :null => false + end + end + + def self.down + drop_table :comments + end +end diff --git a/db/migrate/011_add_news_comments_count.rb b/db/migrate/011_add_news_comments_count.rb new file mode 100644 index 000000000..a24743999 --- /dev/null +++ b/db/migrate/011_add_news_comments_count.rb @@ -0,0 +1,9 @@ +class AddNewsCommentsCount < ActiveRecord::Migration + def self.up + add_column :news, :comments_count, :integer, :default => 0, :null => false + end + + def self.down + remove_column :news, :comments_count + end +end diff --git a/db/migrate/012_add_comments_permissions.rb b/db/migrate/012_add_comments_permissions.rb new file mode 100644 index 000000000..37f075082 --- /dev/null +++ b/db/migrate/012_add_comments_permissions.rb @@ -0,0 +1,11 @@ +class AddCommentsPermissions < ActiveRecord::Migration + def self.up + Permission.create :controller => "news", :action => "add_comment", :description => "label_comment_add", :sort => 1130, :is_public => false, :mail_option => 0, :mail_enabled => 0 + Permission.create :controller => "news", :action => "destroy_comment", :description => "label_comment_delete", :sort => 1133, :is_public => false, :mail_option => 0, :mail_enabled => 0 + end + + def self.down + Permission.find(:first, :conditions => ["controller=? and action=?", 'news', 'add_comment']).destroy + Permission.find(:first, :conditions => ["controller=? and action=?", 'news', 'destroy_comment']).destroy + end +end diff --git a/lang/de.yml b/lang/de.yml index 3cff24ed3..95c2612b5 100644 --- a/lang/de.yml +++ b/lang/de.yml @@ -135,6 +135,7 @@ field_onthefly: On-the-fly Benutzerkreation field_start_date: Beginn field_done_ratio: %% Getan field_hide_mail: Mein email address verstecken +field_comment: Anmerkung label_user: Benutzer label_user_plural: Benutzer @@ -259,6 +260,11 @@ label_internal: Intern label_last_changes: %d änderungen des Letzten label_change_view_all: Alle änderungen ansehen label_personalize_page: Diese Seite personifizieren +label_comment: Anmerkung +label_comment_plural: Anmerkungen +label_comment_add: Anmerkung addieren +label_comment_added: Anmerkung fügte hinzu +label_comment_delete: Anmerkungen löschen button_login: Einloggen button_submit: Einreichen diff --git a/lang/en.yml b/lang/en.yml index d64edea78..4ea41e0e5 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -135,6 +135,7 @@ field_onthefly: On-the-fly user creation field_start_date: Start field_done_ratio: %% Done field_hide_mail: Hide my email address +field_comment: Comment label_user: User label_user_plural: Users @@ -259,6 +260,11 @@ label_internal: Internal label_last_changes: last %d changes label_change_view_all: View all changes label_personalize_page: Personalize this page +label_comment: Comment +label_comment_plural: Comments +label_comment_add: Add a comment +label_comment_added: Comment added +label_comment_delete: Delete comments button_login: Login button_submit: Submit diff --git a/lang/es.yml b/lang/es.yml index 13894955c..5bdb0e447 100644 --- a/lang/es.yml +++ b/lang/es.yml @@ -135,6 +135,7 @@ field_onthefly: Creación del usuario On-the-fly field_start_date: Comienzo field_done_ratio: %% Realizado field_hide_mail: Ocultar mi email address +field_comment: Comentario label_user: Usuario label_user_plural: Usuarios @@ -259,6 +260,11 @@ label_internal: Interno label_last_changes: %d cambios del último label_change_view_all: Ver todos los cambios label_personalize_page: Personalizar esta página +label_comment: Comentario +label_comment_plural: Comentarios +label_comment_add: Agregar un comentario +label_comment_added: Comentario agregó +label_comment_delete: Suprimir comentarios button_login: Conexión button_submit: Someter diff --git a/lang/fr.yml b/lang/fr.yml index cdfabd7dd..630acd152 100644 --- a/lang/fr.yml +++ b/lang/fr.yml @@ -136,6 +136,7 @@ field_start_date: Début field_done_ratio: %% Réalisé field_auth_source: Mode d'authentification field_hide_mail: Cacher mon adresse mail +field_comment: Commentaire label_user: Utilisateur label_user_plural: Utilisateurs @@ -260,6 +261,11 @@ label_internal: Interne label_last_changes: %d derniers changements label_change_view_all: Voir tous les changements label_personalize_page: Personnaliser cette page +label_comment: Commentaire +label_comment_plural: Commentaires +label_comment_add: Ajouter un commentaire +label_comment_added: Commentaire ajouté +label_comment_delete: Supprimer les commentaires button_login: Connexion button_submit: Soumettre diff --git a/lib/tasks/load_default_data.rake b/lib/tasks/load_default_data.rake index d672802db..8f122a4d9 100644 --- a/lib/tasks/load_default_data.rake +++ b/lib/tasks/load_default_data.rake @@ -26,11 +26,11 @@ begin manager.permissions = Permission.find(:all, :conditions => ["is_public=?", false]) developper = Role.create :name => l(:default_role_developper) - perms = [150, 320, 321, 322, 420, 421, 422, 1050, 1060, 1070, 1075, 1220, 1221, 1222, 1223, 1224, 1320, 1322, 1061, 1057] + perms = [150, 320, 321, 322, 420, 421, 422, 1050, 1060, 1070, 1075, 1130, 1220, 1221, 1222, 1223, 1224, 1320, 1322, 1061, 1057] developper.permissions = Permission.find(:all, :conditions => ["sort IN (#{perms.join(',')})"]) reporter = Role.create :name => l(:default_role_reporter) - perms = [1050, 1060, 1070, 1057] + perms = [1050, 1060, 1070, 1057, 1130] reporter.permissions = Permission.find(:all, :conditions => ["sort IN (#{perms.join(',')})"]) # trackers diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index 7af699a8b..9b0927a39 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -446,7 +446,7 @@ img.calendar-trigger { margin-left: 4px; } -#history h4 { +#history h4, #comments h4 { font-size: 1em; margin-bottom: 12px; margin-top: 20px; diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml new file mode 100644 index 000000000..24a4546aa --- /dev/null +++ b/test/fixtures/comments.yml @@ -0,0 +1,10 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +comments_001: + commented_type: News + commented_id: 1 + id: 1 + author_id: 1 + comment: my first comment + created_on: 2006-12-10 18:10:10 +01:00 + updated_on: 2006-12-10 18:10:10 +01:00 + \ No newline at end of file diff --git a/test/fixtures/news.yml b/test/fixtures/news.yml index 1bef9184e..2c2e2c134 100644 --- a/test/fixtures/news.yml +++ b/test/fixtures/news.yml @@ -10,6 +10,7 @@ news_001: Visit http://ecookbook.somenet.foo/ summary: First version was released... author_id: 2 + comments_count: 1 news_002: created_on: 2006-07-19 22:42:58 +02:00 project_id: 1 @@ -18,3 +19,4 @@ news_002: description: eCookbook 1.0 have downloaded 100,000 times summary: eCookbook 1.0 have downloaded 100,000 times author_id: 2 + comments_count: 0 diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb new file mode 100644 index 000000000..4435f0a7f --- /dev/null +++ b/test/unit/comment_test.rb @@ -0,0 +1,30 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class CommentTest < Test::Unit::TestCase + fixtures :users, :news, :comments + + def setup + @jsmith = User.find(2) + @news = News.find(1) + end + + def test_create + comment = Comment.new(:commented => @news, :author => @jsmith, :comment => "my comment") + assert comment.save + @news.reload + assert_equal 2, @news.comments_count + end + + def test_validate + comment = Comment.new(:commented => @news) + assert !comment.save + assert_equal 2, comment.errors.length + end + + def test_destroy + comment = Comment.find(1) + assert comment.destroy + @news.reload + assert_equal 0, @news.comments_count + end +end