Commit 21ff0c0931e42890be35281c0e60abd88b374152

Authored by AntonioTerceiro
1 parent fb17b619

ActionItem22: first implementation of comments

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1115 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/public/content_viewer_controller.rb
... ... @@ -9,13 +9,25 @@ class ContentViewerController < PublicController
9 9 @page = profile.home_page
10 10 if @page.nil?
11 11 render :action => 'no_home_page'
  12 + return
12 13 end
13 14 else
14 15 @page = profile.articles.find_by_path(path)
15 16 if @page.nil?
16 17 render_not_found(@path)
  18 + return
17 19 end
18 20 end
  21 +
  22 + if request.post? && params[:comment]
  23 + @comment = Comment.new(params[:comment])
  24 + @comment.author = user if logged_in?
  25 + @comment.article = @page
  26 + if @comment.save!
  27 + @comment = nil # clear the comment form
  28 + end
  29 + end
  30 + @comments = @page.comments(true)
19 31 end
20 32  
21 33 end
... ...
app/models/article.rb
... ... @@ -7,6 +7,8 @@ class Article < ActiveRecord::Base
7 7  
8 8 belongs_to :last_changed_by, :class_name => Person.name, :foreign_key => 'last_changed_by_id'
9 9  
  10 + has_many :comments
  11 +
10 12 acts_as_taggable
11 13 N_('Tag list')
12 14  
... ...
app/views/content_viewer/_comment.rhtml 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +<div class'comment'>
  2 + <h4><%= comment.title %></h4>
  3 + <div><%= _('By %{author} on %{date}') % { :author => (comment.author ? comment.author.identifier : comment.name), :date => comment.created_on } %></div>
  4 + <div>
  5 + <%= comment.body %>
  6 + </div>
  7 +</div>
... ...
app/views/content_viewer/_comment_form.rhtml 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +<h3><%= _('Post a comment') %></h3>
  2 +
  3 +<% form_tag do %>
  4 +
  5 + <% unless logged_in? %>
  6 +
  7 + <%= labelled_form_field(_('Name'), text_field(:comment, :name)) %>
  8 + <%= labelled_form_field(_('e-mail'), text_field(:comment, :email)) %>
  9 +
  10 + <p>
  11 + <%= _('If you are a registered user, you can login and be automatically recognized.') %>
  12 + </p>
  13 +
  14 + <% end %>
  15 +
  16 + <%= labelled_form_field(_('Title'), text_field(:comment, :title)) %>
  17 + <%= labelled_form_field(_('Enter your comment'), text_area(:comment, :body)) %>
  18 + <%= submit_tag _('Post comment') %>
  19 +<% end %>
... ...
app/views/content_viewer/view_page.rhtml
... ... @@ -24,3 +24,7 @@
24 24 <% end %>
25 25 <% end %>
26 26 -->
  27 +
  28 +<h3><%= @comments.size == 0 ? _('No comments yet') : (n_('One comment', '%{comments} comments', @comments.size)) % { :comments => @comments.size} %></h3>
  29 +<%= render :partial => 'comment', :collection => @comments %>
  30 +<%= render :partial => 'comment_form' %>
... ...
test/functional/content_viewer_controller_test.rb
... ... @@ -53,9 +53,6 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
53 53 uses_host 'anhetegua.net'
54 54 get :view_page, :profile => 'aprofile', :page => ['some_unexisting_page']
55 55 assert_response :missing
56   - # This is an idea of instead of give an error search for the term
57   -# assert_response :redirect
58   -# assert_redirected_to :controller => 'search', :action => 'index'
59 56 end
60 57  
61 58 def test_should_get_not_found_error_for_unexisting_profile
... ... @@ -63,10 +60,30 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
63 60 uses_host 'anhetegua'
64 61 get :view_page, :profile => 'some_unexisting_profile', :page => []
65 62 assert_response :missing
66   -
67   - # This is an idea of instead of give an error search for the term
68   -# assert_response :redirect
69   -# assert_redirected_to :controller => 'search', :action => 'index'
70 63 end
71 64  
  65 + def test_should_be_able_to_post_comment_while_authenticated
  66 + profile = create_user('popstar').person
  67 + page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text')
  68 + page.save!
  69 + profile.home_page = page; profile.save!
  70 +
  71 + assert_difference Comment, :count do
  72 + login_as('ze')
  73 + post :view_page, :profile => 'popstar', :page => [ 'myarticle' ], :comment => { :title => 'crap!', :body => 'I think that this article is crap' }
  74 + end
  75 + end
  76 +
  77 + def test_should_be_able_to_post_comment_while_not_authenticated
  78 + profile = create_user('popstar').person
  79 + page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text')
  80 + page.save!
  81 + profile.home_page = page; profile.save!
  82 +
  83 + assert_difference Comment, :count do
  84 + post :view_page, :profile => 'popstar', :page => [ 'myarticle' ], :comment => { :title => 'crap!', :body => 'I think that this article is crap', :name => 'Anonymous coward', :email => 'coward@anonymous.com' }
  85 + end
  86 + end
  87 +
  88 +
72 89 end
... ...