diff options
author | francis <francis> | 2008-08-26 22:54:45 +0000 |
---|---|---|
committer | francis <francis> | 2008-08-26 22:54:45 +0000 |
commit | 89f5ef7be8e464578ed9add6b5978ddf0ff39f09 (patch) | |
tree | 6b35009442ee557883fbb14a7c63a2fb19776191 | |
parent | 31ee078910973606e05a469e2fdcb795c0403ebc (diff) |
Prevent double posting of comments, and test code for that.
-rw-r--r-- | app/controllers/comment_controller.rb | 6 | ||||
-rw-r--r-- | app/helpers/link_to_helper.rb | 11 | ||||
-rw-r--r-- | app/models/comment.rb | 15 | ||||
-rw-r--r-- | app/views/comment/new.rhtml | 9 | ||||
-rw-r--r-- | app/views/request/show.rhtml | 2 | ||||
-rw-r--r-- | spec/controllers/comment_controller_spec.rb | 31 | ||||
-rw-r--r-- | todo.txt | 5 |
7 files changed, 44 insertions, 35 deletions
diff --git a/app/controllers/comment_controller.rb b/app/controllers/comment_controller.rb index 7192eb84b..fe2b31daf 100644 --- a/app/controllers/comment_controller.rb +++ b/app/controllers/comment_controller.rb @@ -4,7 +4,7 @@ # Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: comment_controller.rb,v 1.2 2008-08-26 16:03:36 francis Exp $ +# $Id: comment_controller.rb,v 1.3 2008-08-26 22:54:45 francis Exp $ class CommentController < ApplicationController @@ -20,10 +20,10 @@ class CommentController < ApplicationController end # XXX this check should theoretically be a validation rule in the model - #@existing_comment = Comment.find_by_existing_comment(params[:info_request][:title], params[:info_request][:public_body_id], params[:outgoing_message][:body]) + @existing_comment = Comment.find_by_existing_comment(@info_request.id, params[:comment][:body]) # See if values were valid or not - if !@comment.valid? || params[:reedit] + if !@existing_comment.nil? || !@comment.valid? || params[:reedit] render :action => 'new' return end diff --git a/app/helpers/link_to_helper.rb b/app/helpers/link_to_helper.rb index 96e489e26..722c6a2d7 100644 --- a/app/helpers/link_to_helper.rb +++ b/app/helpers/link_to_helper.rb @@ -5,7 +5,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: link_to_helper.rb,v 1.36 2008-08-13 01:39:41 francis Exp $ +# $Id: link_to_helper.rb,v 1.37 2008-08-26 22:54:46 francis Exp $ module LinkToHelper @@ -73,13 +73,16 @@ module LinkToHelper link_to h(user.name), user_url(user) end end - def user_or_you_capital_link(user) + def user_or_you_capital(user) if @user && user == @user - link_to h("You"), user_url(user) + return h("You") else - link_to h(user.name), user_url(user) + return h(user.name) end end + def user_or_you_capital_link(user) + link_to user_or_you_capital(user), user_url(user) + end def user_admin_url(user) return admin_url('user/show/' + user.id.to_s) end diff --git a/app/models/comment.rb b/app/models/comment.rb index a8e338a70..0d219a9a8 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -19,7 +19,7 @@ # Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: comment.rb,v 1.2 2008-08-22 04:18:53 francis Exp $ +# $Id: comment.rb,v 1.3 2008-08-26 22:54:46 francis Exp $ class Comment < ActiveRecord::Base belongs_to :user @@ -58,6 +58,19 @@ class Comment < ActiveRecord::Base text = text.gsub(/\n/, '<br>') return text end + + # When posting a new comment, use this to check user hasn't double submitted. + def Comment.find_by_existing_comment(info_request_id, body) + # XXX can add other databases here which have regexp_replace + if ActiveRecord::Base.connection.adapter_name == "PostgreSQL" + # Exclude spaces from the body comparison using regexp_replace + return Comment.find(:first, :conditions => [ "info_request_id = ? and regexp_replace(body, '[[:space:]]', '', 'g') = regexp_replace(?, '[[:space:]]', '', 'g')", info_request_id, body ]) + else + # For other databases (e.g. SQLite) not the end of the world being space-sensitive for this check + return Comment.find(:first, :conditions => [ "info_request_id = ? and body = ?", info_request_id, body ]) + end + end + end diff --git a/app/views/comment/new.rhtml b/app/views/comment/new.rhtml index 22e260c1b..b377f82fa 100644 --- a/app/views/comment/new.rhtml +++ b/app/views/comment/new.rhtml @@ -1,5 +1,14 @@ <% @title = "Make an annotation on '" + h(@info_request.title) + "'" %> +<% if @existing_comment %> + <div class="errorExplanation" id="errorExplanation"><ul> + <li> + <%= user_or_you_capital(@existing_comment.user) %> already + made this annotation on <%=simple_date(@existing_comment.created_at)%>. + </li> + </ul></div> +<% end %> + <%= foi_error_messages_for :comment %> <h1>Add an annotation</h1> diff --git a/app/views/request/show.rhtml b/app/views/request/show.rhtml index e10550022..15d70a8f4 100644 --- a/app/views/request/show.rhtml +++ b/app/views/request/show.rhtml @@ -98,7 +98,6 @@ <%= render :partial => 'correspondence', :locals => { :info_request_event => info_request_event } %> <% end %> - <!-- <div id="comment_form"> <h2>Add an annotation</h2> @@ -148,7 +147,6 @@ <%= render :partial => 'comment/comment_form', :locals => { } %> </div> - --> </div> diff --git a/spec/controllers/comment_controller_spec.rb b/spec/controllers/comment_controller_spec.rb index 6e0baf3b7..d7a8c91eb 100644 --- a/spec/controllers/comment_controller_spec.rb +++ b/spec/controllers/comment_controller_spec.rb @@ -45,30 +45,15 @@ describe CommentController, "when commenting on a request" do response.should redirect_to(:controller => 'request', :action => 'show', :url_title => info_requests(:naughty_chicken_request).url_title, :anchor => 'comment-' + comment.id.to_s) end -# it "should give an error if the same request is submitted twice" do -# session[:user_id] = users(:bob_smith_user).id -# -# # We use raw_body here, so white space is the same -# post :new, :info_request => { :public_body_id => info_requests(:fancy_dog_request).public_body_id, -# :title => info_requests(:fancy_dog_request).title }, -# :outgoing_message => { :body => info_requests(:fancy_dog_request).outgoing_messages[0].raw_body}, -# :submitted_new_request => 1, :preview => 0, :mouse_house => 1 -# response.should render_template('new') -# end + it "should give an error if the same request is submitted twice" do + session[:user_id] = users(:silly_name_user).id -# it "should give an error if the same request is submitted twice with extra whitespace in the body" do -# # This only works for PostgreSQL databases which have regexp_replace - -# # see model method InfoRequest.find_by_existing_request for more info -# if ActiveRecord::Base.connection.adapter_name == "PostgreSQL" -# session[:user_id] = users(:bob_smith_user).id -# -# post :new, :info_request => { :public_body_id => info_requests(:fancy_dog_request).public_body_id, -# :title => info_requests(:fancy_dog_request).title }, -# :outgoing_message => { :body => "\n" + info_requests(:fancy_dog_request).outgoing_messages[0].body + " "}, -# :submitted_new_request => 1, :preview => 0, :mouse_house => 1 -# response.should render_template('new') -# end -# end + post :new, :url_title => info_requests(:fancy_dog_request).url_title, + :comment => { :body => comments(:silly_comment).body }, + :type => 'request', :submitted_comment => 1, :preview => 0 + + response.should render_template('new') + end end @@ -27,13 +27,14 @@ doing file RESPONSE/Internal documents/Briefing with Contact Islington/Contact I Search for other extensions that we have now Comments interleaved with body + - Improve display of preview, is rubbish + - "Add an annotation" text appears in sidebar :( + - Email people when comments on their own request - Email people when responses to their own comment, or warn them they need to sign up - - Don't allow double posting (submitting same comment twice, add spec test) - Check CSS class/id names used in all comment HTML - Think about the "nonsense" text a lot more - - Improve display of preview, is rubbish - Test spell checker - List all comments in admin interface? and/or flag bad comments |