Commit 21ff0c0931e42890be35281c0e60abd88b374152
1 parent
fb17b619
Exists in
master
and in
28 other branches
ActionItem22: first implementation of comments
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1115 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
6 changed files
with
68 additions
and
7 deletions
Show diff stats
app/controllers/public/content_viewer_controller.rb
@@ -9,13 +9,25 @@ class ContentViewerController < PublicController | @@ -9,13 +9,25 @@ class ContentViewerController < PublicController | ||
9 | @page = profile.home_page | 9 | @page = profile.home_page |
10 | if @page.nil? | 10 | if @page.nil? |
11 | render :action => 'no_home_page' | 11 | render :action => 'no_home_page' |
12 | + return | ||
12 | end | 13 | end |
13 | else | 14 | else |
14 | @page = profile.articles.find_by_path(path) | 15 | @page = profile.articles.find_by_path(path) |
15 | if @page.nil? | 16 | if @page.nil? |
16 | render_not_found(@path) | 17 | render_not_found(@path) |
18 | + return | ||
17 | end | 19 | end |
18 | end | 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 | end | 31 | end |
20 | 32 | ||
21 | end | 33 | end |
app/models/article.rb
@@ -7,6 +7,8 @@ class Article < ActiveRecord::Base | @@ -7,6 +7,8 @@ class Article < ActiveRecord::Base | ||
7 | 7 | ||
8 | belongs_to :last_changed_by, :class_name => Person.name, :foreign_key => 'last_changed_by_id' | 8 | belongs_to :last_changed_by, :class_name => Person.name, :foreign_key => 'last_changed_by_id' |
9 | 9 | ||
10 | + has_many :comments | ||
11 | + | ||
10 | acts_as_taggable | 12 | acts_as_taggable |
11 | N_('Tag list') | 13 | N_('Tag list') |
12 | 14 |
@@ -0,0 +1,7 @@ | @@ -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> |
@@ -0,0 +1,19 @@ | @@ -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,3 +24,7 @@ | ||
24 | <% end %> | 24 | <% end %> |
25 | <% end %> | 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 < Test::Unit::TestCase | @@ -53,9 +53,6 @@ class ContentViewerControllerTest < Test::Unit::TestCase | ||
53 | uses_host 'anhetegua.net' | 53 | uses_host 'anhetegua.net' |
54 | get :view_page, :profile => 'aprofile', :page => ['some_unexisting_page'] | 54 | get :view_page, :profile => 'aprofile', :page => ['some_unexisting_page'] |
55 | assert_response :missing | 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 | end | 56 | end |
60 | 57 | ||
61 | def test_should_get_not_found_error_for_unexisting_profile | 58 | def test_should_get_not_found_error_for_unexisting_profile |
@@ -63,10 +60,30 @@ class ContentViewerControllerTest < Test::Unit::TestCase | @@ -63,10 +60,30 @@ class ContentViewerControllerTest < Test::Unit::TestCase | ||
63 | uses_host 'anhetegua' | 60 | uses_host 'anhetegua' |
64 | get :view_page, :profile => 'some_unexisting_profile', :page => [] | 61 | get :view_page, :profile => 'some_unexisting_profile', :page => [] |
65 | assert_response :missing | 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 | end | 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 | end | 89 | end |