diff options
-rw-r--r-- | app/controllers/admin_comment_controller.rb | 36 | ||||
-rw-r--r-- | app/controllers/admin_request_controller.rb | 24 | ||||
-rw-r--r-- | app/views/admin_comment/edit.html.erb (renamed from app/views/admin_request/edit_comment.html.erb) | 8 | ||||
-rw-r--r-- | app/views/admin_request/_some_annotations.html.erb | 8 | ||||
-rw-r--r-- | app/views/comment/_single_comment.html.erb | 2 | ||||
-rw-r--r-- | config/routes.rb | 10 | ||||
-rw-r--r-- | spec/controllers/admin_comment_controller_spec.rb | 66 |
7 files changed, 119 insertions, 35 deletions
diff --git a/app/controllers/admin_comment_controller.rb b/app/controllers/admin_comment_controller.rb new file mode 100644 index 000000000..0aafb122a --- /dev/null +++ b/app/controllers/admin_comment_controller.rb @@ -0,0 +1,36 @@ +# app/controllers/admin_comment_controller.rb: +# Controller for editing comments from the admin interface. +# +# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. +# Email: hello@mysociety.org; WWW: http://www.mysociety.org/ + +class AdminCommentController < AdminController + + def edit + @comment = Comment.find(params[:id]) + end + + def update + @comment = Comment.find(params[:id]) + + old_body = @comment.body + old_visible = @comment.visible + @comment.visible = params[:comment][:visible] == "true" ? true : false + + if @comment.update_attributes(params[:comment]) + @comment.info_request.log_event("edit_comment", + { :comment_id => @comment.id, + :editor => admin_current_user(), + :old_body => old_body, + :body => @comment.body, + :old_visible => old_visible, + :visible => @comment.visible, + }) + flash[:notice] = 'Comment successfully updated.' + redirect_to admin_request_url(@comment.info_request) + else + render :action => 'edit' + end + end + +end diff --git a/app/controllers/admin_request_controller.rb b/app/controllers/admin_request_controller.rb index 87c07b3bd..6d6bf56e5 100644 --- a/app/controllers/admin_request_controller.rb +++ b/app/controllers/admin_request_controller.rb @@ -93,30 +93,6 @@ class AdminRequestController < AdminController redirect_to admin_requests_url end - def edit_comment - @comment = Comment.find(params[:id]) - end - - def update_comment - @comment = Comment.find(params[:id]) - - old_body = @comment.body - old_visible = @comment.visible - @comment.visible = params[:comment][:visible] == "true" ? true : false - - if @comment.update_attributes(params[:comment]) - @comment.info_request.log_event("edit_comment", - { :comment_id => @comment.id, :editor => admin_current_user(), - :old_body => old_body, :body => @comment.body, - :old_visible => old_visible, :visible => @comment.visible, - }) - flash[:notice] = 'Comment successfully updated.' - redirect_to admin_request_url(@comment.info_request) - else - render :action => 'edit_comment' - end - end - # change user or public body of a request magically def move info_request = InfoRequest.find(params[:id]) diff --git a/app/views/admin_request/edit_comment.html.erb b/app/views/admin_comment/edit.html.erb index a177deb7e..e3cbd7472 100644 --- a/app/views/admin_request/edit_comment.html.erb +++ b/app/views/admin_comment/edit.html.erb @@ -2,12 +2,12 @@ <%= error_messages_for 'comment' %> -<%= form_tag admin_request_update_comment_path(@comment) do %> +<%= form_tag admin_comment_path(@comment), :method => 'put' do %> <p><label for="comment_body">Body of annotation</label><br/> <%= text_area 'comment', 'body', :rows => 10, :cols => 60 %></p> - <p><label for="comment_visible">Visible</label> + <p><label for="comment_visible">Visible</label> <%= select('comment', "visible", [["Yes – show comment",true],["No – hide comment",false]]) %> </p> @@ -16,7 +16,7 @@ <% end %> <p> -<%= link_to 'Show', admin_request_path(@comment.info_request) %> | -<%= link_to 'List all', admin_requests_path %> +<%= link_to 'Show request', admin_request_path(@comment.info_request) %> | +<%= link_to 'List all requests', admin_requests_path %> </p> diff --git a/app/views/admin_request/_some_annotations.html.erb b/app/views/admin_request/_some_annotations.html.erb index 7adcdc5bb..4fd313451 100644 --- a/app/views/admin_request/_some_annotations.html.erb +++ b/app/views/admin_request/_some_annotations.html.erb @@ -5,16 +5,16 @@ <div class="accordion-group"> <div class="accordion-heading"> <%= check_box_tag 'comment_ids[]', comment.id %> - + <a href="#comment_<%= comment.id %>" data-toggle="collapse" data-parent="#comments"><%= chevron_right %></a> - - <%= link_to admin_request_edit_comment_path(comment) do %> + + <%= link_to edit_admin_comment_path(comment) do %> #<%= comment.id %> -- <%= h(comment.user.name) %> <%= admin_value(comment.created_at) %> <% end %> - + <%= comment_visibility(comment) %> <blockquote class="incoming-message"> diff --git a/app/views/comment/_single_comment.html.erb b/app/views/comment/_single_comment.html.erb index cdcb3b85f..07017dabf 100644 --- a/app/views/comment/_single_comment.html.erb +++ b/app/views/comment/_single_comment.html.erb @@ -17,7 +17,7 @@ <p class="event_actions"> <% if !comment.id.nil? %> <% if !@user.nil? && @user.admin_page_links? %> - <%= link_to "Admin", admin_request_edit_comment_path(comment) %> | + <%= link_to "Admin", edit_admin_comment_path(comment) %> | <% end %> <%= link_to "Link to this", comment_path(comment), :class => "link_to_this" %> <!-- | <%= link_to _('Report abuse'), comment_path(comment) %> --> diff --git a/config/routes.rb b/config/routes.rb index 73d8b3730..283861efa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -223,14 +223,20 @@ Alaveteli::Application.routes.draw do end match '/admin/request/edit/:id' => 'admin_request#edit', :as => :admin_request_edit match '/admin/request/update/:id' => 'admin_request#update', :as => :admin_request_update - match '/admin/request/edit_comment/:id' => 'admin_request#edit_comment', :as => :admin_request_edit_comment - match '/admin/request/update_comment/:id' => 'admin_request#update_comment', :as => :admin_request_update_comment match '/admin/request/show_raw_email/:id' => 'admin_request#show_raw_email', :as => :admin_request_show_raw_email match '/admin/request/download_raw_email/:id' => 'admin_request#download_raw_email', :as => :admin_request_download_raw_email match '/admin/request/mark_event_as_clarification' => 'admin_request#mark_event_as_clarification', :as => :admin_request_clarification match '/admin/request/hide/:id' => 'admin_request#hide_request', :as => :admin_request_hide #### + #### AdminComment controller + scope '/admin', :as => 'admin' do + resources :comments, + :controller => 'admin_comment', + :only => [:edit, :update] + end + #### + #### AdminIncomingMessage controller match '/admin/incoming/destroy' => 'admin_incoming_message#destroy', :as => :admin_incoming_destroy match '/admin/incoming/redeliver' => 'admin_incoming_message#redeliver', :as => :admin_incoming_redeliver diff --git a/spec/controllers/admin_comment_controller_spec.rb b/spec/controllers/admin_comment_controller_spec.rb new file mode 100644 index 000000000..f87231e3b --- /dev/null +++ b/spec/controllers/admin_comment_controller_spec.rb @@ -0,0 +1,66 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe AdminCommentController do + + describe :edit do + + before do + @comment = FactoryGirl.create(:comment) + get :edit, :id => @comment.id + end + + it 'renders the edit template' do + expect(response).to render_template('edit') + end + + it 'gets the comment' do + assigns[:comment].should == @comment + end + + end + + describe :update do + + context 'on valid data submission' do + + before do + @comment = FactoryGirl.create(:comment) + atts = FactoryGirl.attributes_for(:comment, :body => 'I am new') + put :update, :id => @comment.id, :comment => atts + end + + it 'gets the comment' do + assigns[:comment].should == @comment + end + + it 'updates the comment' do + Comment.find(@comment.id).body.should == 'I am new' + end + + it 'logs the update event' do + most_recent_event = Comment.find(@comment.id).info_request_events.last + most_recent_event.event_type.should == 'edit_comment' + most_recent_event.comment_id.should == @comment.id + end + + it 'shows a success notice' do + flash[:notice].should == "Comment successfully updated." + end + + it 'redirects to the request page' do + response.should redirect_to(admin_request_path(@comment.info_request)) + end + end + + context 'on invalid data submission' do + + it 'renders the edit template' do + @comment = FactoryGirl.create(:comment) + put :update, :id => @comment.id, :comment => {:body => ''} + response.should render_template('edit') + end + + end + end + +end |