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
This commit is contained in:
Jean-Philippe Lang 2006-12-10 18:35:48 +00:00
parent 28c6aa4e6d
commit 55ed70529a
17 changed files with 153 additions and 5 deletions

View File

@ -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

6
app/models/comment.rb Normal file
View File

@ -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

View File

@ -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

View File

@ -13,4 +13,26 @@
<% end %>
</div>
<%= link_to_if_authorized l(:button_edit), :controller => 'news', :action => 'edit', :id => @news %>
<p><%= link_to_if_authorized l(:button_edit), :controller => 'news', :action => 'edit', :id => @news %></p>
<div id="comments" style="margin-bottom:16px;">
<h3><%= l(:label_comment_plural) %></h3>
<% @news.comments.each do |comment| %>
<% next if comment.new_record? %>
<h4><%= format_time(comment.created_on) %> - <%= comment.author.name %></h4>
<div style="float:right;">
<small><%= 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 %></small>
</div>
<%= simple_format(auto_link(h comment.comment))%>
<% end if @news.comments_count > 0 %>
</div>
<% if authorize_for 'news', 'add_comment' %>
<h3><%= l(:label_comment_add) %></h3>
<%= start_form_tag :action => 'add_comment', :id => @news %>
<%= error_messages_for 'comment' %>
<p><label for="comment_comment"><%= l(:field_comment) %></label><br />
<%= text_area 'comment', 'comment', :cols => 60, :rows => 6 %></p>
<%= submit_tag l(:button_add) %>
<%= end_form_tag %>
<% end %>

View File

@ -6,7 +6,8 @@
<% for news in @news %>
<li><%= link_to news.title, :controller => 'news', :action => 'show', :id => news %><br />
<% unless news.summary.empty? %><%= news.summary %><br /><% end %>
<em><%= news.author.name %>, <%= format_time(news.created_on) %></em><br />&nbsp;
<em><%= news.author.name %>, <%= format_time(news.created_on) %></em><br />
<%= news.comments_count %> <%= lwr(:label_comment, news.comments_count).downcase %><br />&nbsp;
</li>
<% end %>
</ul>

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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;

10
test/fixtures/comments.yml vendored Normal file
View File

@ -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

View File

@ -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

30
test/unit/comment_test.rb Normal file
View File

@ -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