Commit 098929265cf3d46a7aca73189aebd428fcaa6c76

Authored by Rodrigo Souto
1 parent a53aaa49

[tolerance-time] Edit comment infra-structure

app/controllers/public/content_viewer_controller.rb
... ... @@ -2,6 +2,8 @@ class ContentViewerController < ApplicationController
2 2  
3 3 needs_profile
4 4  
  5 + before_filter :comment_author, :only => :edit_comment
  6 +
5 7 helper ProfileHelper
6 8 helper TagsHelper
7 9  
... ... @@ -106,6 +108,22 @@ class ContentViewerController < ApplicationController
106 108 end
107 109 end
108 110  
  111 + def edit_comment
  112 + path = params[:page].join('/')
  113 + @page = profile.articles.find_by_path(path)
  114 + @form_div = 'opened'
  115 + @comment = Comment.find(params[:id])
  116 + if request.post?
  117 + begin
  118 + @comment.update_attributes(params[:comment])
  119 + session[:notice] = _('Comment updated.')
  120 + redirect_to :action => 'view_page', :profile => profile.identifier, :page => @comment.article.explode_path
  121 + rescue
  122 + session[:notice] = _('Comment could not be updated.')
  123 + end
  124 + end
  125 + end
  126 +
109 127 protected
110 128  
111 129 def add_comment
... ... @@ -173,4 +191,9 @@ class ContentViewerController < ApplicationController
173 191 end
174 192 end
175 193  
  194 + def comment_author
  195 + comment = Comment.find(params[:id])
  196 + render_access_denied if comment.author.blank? || comment.author != user
  197 + end
  198 +
176 199 end
... ...
app/views/content_viewer/_comment_form.rhtml
... ... @@ -39,7 +39,7 @@ function submit_comment_form(button) {
39 39 d.find('input[name=comment[title]], textarea').val('');
40 40 d.find('.comment_form input[name=comment[<%= focus_on %>]]').focus();
41 41 }">
42   - <%= content_tag('a', '', :name => 'comment_form') + _('Post a comment') %>
  42 + <%= content_tag('a', '', :name => 'comment_form') + _('Post a comment') if display_link %>
43 43 </h4>
44 44  
45 45 <% unless pass_without_comment_captcha? %>
... ... @@ -59,7 +59,7 @@ function submit_comment_form(button) {
59 59 </script>
60 60 <% end %>
61 61  
62   -<% form_tag( url_for(@page.view_url.merge({:only_path => true})), { :class => 'comment_form' } ) do %>
  62 +<% form_tag( url, { :class => 'comment_form' } ) do %>
63 63 <%= hidden_field_tag(:confirm, 'false') %>
64 64  
65 65 <%= required_fields_message %>
... ... @@ -84,7 +84,11 @@ function submit_comment_form(button) {
84 84  
85 85 <% button_bar do %>
86 86 <%= submit_button('add', _('Post comment'), :onclick => "submit_comment_form(this); return false") %>
87   - <%= button_to_function :cancel, _('Cancel'), "f=jQuery(this).parents('.post_comment_box'); f.removeClass('opened'); f.addClass('closed'); return false" %>
  87 + <% if cancel_triggers_hide %>
  88 + <%= button_to_function :cancel, _('Cancel'), "f=jQuery(this).parents('.post_comment_box'); f.removeClass('opened'); f.addClass('closed'); return false" %>
  89 + <% else %>
  90 + <%= button('cancel', _('Cancel'), {:action => 'view_page', :profile => profile.identifier, :page => @comment.article.explode_path})%>
  91 + <% end %>
88 92 <% end %>
89 93 <% end %>
90 94  
... ...
app/views/content_viewer/edit_comment.html.erb 0 → 100644
... ... @@ -0,0 +1,3 @@
  1 +<h1><%= _('Edit comment') %></h1>
  2 +
  3 +<%= render :partial => 'comment_form', :locals => {:url => {:action => 'edit_comment', :profile => profile.identifier}, :display_link => false, :cancel_triggers_hide => false} %>
... ...
app/views/content_viewer/view_page.rhtml
... ... @@ -86,7 +86,7 @@
86 86 </ul>
87 87  
88 88 <% if @page.accept_comments? %>
89   - <div id="page-comment-form"><%= render :partial => 'comment_form' %></div>
  89 + <div id="page-comment-form"><%= render :partial => 'comment_form', :locals => {:url => url_for(@page.view_url.merge({:only_path => true})), :display_link => true, :cancel_triggers_hide => true}%></div>
90 90 <script type="text/javascript">
91 91 jQuery( function() {
92 92 jQuery('.article-comment').live('mouseover', function() { jQuery(this).find('.icon-delete:first').show(); });
... ...
config/routes.rb
... ... @@ -121,9 +121,12 @@ ActionController::Routing::Routes.draw do |map|
121 121 # cache stuff - hack
122 122 map.cache 'public/:action/:id', :controller => 'public'
123 123  
  124 + map.connect ':profile/edit_comment/:id/*page', :controller => 'content_viewer', :action => 'edit_comment', :profile => /#{Noosfero.identifier_format}/
  125 +
124 126 # match requests for profiles that don't have a custom domain
125 127 map.homepage ':profile/*page', :controller => 'content_viewer', :action => 'view_page', :profile => /#{Noosfero.identifier_format}/, :conditions => { :if => lambda { |env| !Domain.hosting_profile_at(env[:host]) } }
126 128  
  129 +
127 130 # match requests for content in domains hosted for profiles
128 131 map.connect '*page', :controller => 'content_viewer', :action => 'view_page'
129 132  
... ...
test/factories.rb
... ... @@ -442,7 +442,7 @@ module Noosfero::Factory
442 442  
443 443 def defaults_for_comment(params = {})
444 444 name = "comment_#{rand(1000)}"
445   - { :title => name, :body => "my own comment", :source_id => 1 }.merge(params)
  445 + { :title => name, :body => "my own comment", :source_id => 1, :source_type => 'Article' }.merge(params)
446 446 end
447 447  
448 448 ###############################################
... ...