diff options
-rw-r--r-- | app/controllers/admin_public_body_controller.rb | 42 | ||||
-rw-r--r-- | app/models/public_body_change_request.rb | 33 | ||||
-rw-r--r-- | app/views/admin_public_body/_form.html.erb | 4 | ||||
-rw-r--r-- | app/views/admin_public_body_change_requests/_response.html.erb | 15 | ||||
-rw-r--r-- | app/views/admin_public_body_change_requests/add_accepted.txt.erb | 9 | ||||
-rw-r--r-- | app/views/admin_public_body_change_requests/update_accepted.txt.erb | 7 | ||||
-rw-r--r-- | spec/controllers/admin_public_body_controller_spec.rb | 106 | ||||
-rw-r--r-- | spec/factories.rb | 12 | ||||
-rw-r--r-- | spec/models/public_body_change_request_spec.rb | 29 |
9 files changed, 254 insertions, 3 deletions
diff --git a/app/controllers/admin_public_body_controller.rb b/app/controllers/admin_public_body_controller.rb index 88e275960..120419a27 100644 --- a/app/controllers/admin_public_body_controller.rb +++ b/app/controllers/admin_public_body_controller.rb @@ -85,14 +85,33 @@ class AdminPublicBodyController < AdminController def new @public_body = PublicBody.new - render + if params[:change_request_id] + @change_request = PublicBodyChangeRequest.find(params[:change_request_id]) + end + if @change_request + @change_request_user_response = render_to_string(:template => "admin_public_body_change_requests/add_accepted", + :formats => [:txt]) + @public_body.name = @change_request.public_body_name + @public_body.request_email = @change_request.public_body_email + @public_body.last_edit_comment = @change_request.comment_for_public_body + end + render :formats => [:html] end def create I18n.with_locale(I18n.default_locale) do + if params[:change_request_id] + @change_request = PublicBodyChangeRequest.find(params[:change_request_id]) + end params[:public_body][:last_edit_editor] = admin_current_user() @public_body = PublicBody.new(params[:public_body]) if @public_body.save + if @change_request + response_text = params[:response].gsub(_("[Authority URL will be inserted here]"), + public_body_url(@public_body, :only_path => false)) + @change_request.close! + @change_request.send_response(params[:subject], response_text) + end flash[:notice] = 'PublicBody was successfully created.' redirect_to admin_body_show_url(@public_body) else @@ -103,15 +122,32 @@ class AdminPublicBodyController < AdminController def edit @public_body = PublicBody.find(params[:id]) - @public_body.last_edit_comment = "" - render + if params[:change_request_id] + @change_request = PublicBodyChangeRequest.find(params[:change_request_id]) + end + if @change_request + @change_request_user_response = render_to_string(:template => "admin_public_body_change_requests/update_accepted", + :formats => [:txt]) + @public_body.request_email = @change_request.public_body_email + @public_body.last_edit_comment = @change_request.comment_for_public_body + else + @public_body.last_edit_comment = "" + end + render :formats => [:html] end def update + if params[:change_request_id] + @change_request = PublicBodyChangeRequest.find(params[:change_request_id]) + end I18n.with_locale(I18n.default_locale) do params[:public_body][:last_edit_editor] = admin_current_user() @public_body = PublicBody.find(params[:id]) if @public_body.update_attributes(params[:public_body]) + if @change_request + @change_request.close! + @change_request.send_response(params[:subject], params[:response]) + end flash[:notice] = 'PublicBody was successfully updated.' redirect_to admin_body_show_url(@public_body) else diff --git a/app/models/public_body_change_request.rb b/app/models/public_body_change_request.rb index 6eda893fa..c1f395c0c 100644 --- a/app/models/public_body_change_request.rb +++ b/app/models/public_body_change_request.rb @@ -81,6 +81,39 @@ class PublicBodyChangeRequest < ActiveRecord::Base end end + def send_response(subject, response) + ContactMailer.from_admin_message(get_user_name, + get_user_email, + subject, + response.strip.html_safe).deliver + end + + def comment_for_public_body + comments = [_("Requested by: #{get_user_name} (#{get_user_email})")] + if !source_url.blank? + comments << _("Source URL: #{source_url}") + end + if !notes.blank? + comments << _("Notes: #{notes}") + end + comments.join("\n") + end + + def default_response_subject + if self.public_body + _("Your request to update {{public_body_name}} on {{site_name}}", :site_name => AlaveteliConfiguration::site_name, + :public_body_name => public_body.name) + else + _("Your request to add {{public_body_name}} to {{site_name}}", :site_name => AlaveteliConfiguration::site_name, + :public_body_name => public_body_name) + end + end + + def close! + self.is_open = false + self.save! + end + private def body_email_format diff --git a/app/views/admin_public_body/_form.html.erb b/app/views/admin_public_body/_form.html.erb index 5a80386ec..2da13ab01 100644 --- a/app/views/admin_public_body/_form.html.erb +++ b/app/views/admin_public_body/_form.html.erb @@ -95,4 +95,8 @@ <p class="help-block">put URL or other source of new info</p> </div> </div> +<% if @change_request %> + <%= render :partial => 'admin_public_body_change_requests/response' %> + +<% end %> <!--[eoform:public_body]--> diff --git a/app/views/admin_public_body_change_requests/_response.html.erb b/app/views/admin_public_body_change_requests/_response.html.erb new file mode 100644 index 000000000..7fda8b7f8 --- /dev/null +++ b/app/views/admin_public_body_change_requests/_response.html.erb @@ -0,0 +1,15 @@ +<h3>Response to change request (will be emailed to user):</h3> +<%= hidden_field_tag 'change_request_id', @change_request.id %> +<div class="control-group" id="change_request_user_subject"> + <label for="change_request_user_subject_field" class="control-label">Subject of email:</label> + <div class="controls"> + <%= text_field_tag "subject", (params[:subject] || @change_request.default_response_subject), {:id => "change_request_user_subject_field", :class => "span6"} %> + </div> +</div> + +<div class="control-group" id="change_request_user_response"> + <label for="change_request_user_response_field" class="control-label">Response</label> + <div class="controls"> + <%= text_area_tag "response", (params[:response] || h(@change_request_user_response)), {:id => "change_request_user_response_field", :rows => 10, :class => 'span6'} %> + </div> +</div> diff --git a/app/views/admin_public_body_change_requests/add_accepted.txt.erb b/app/views/admin_public_body_change_requests/add_accepted.txt.erb new file mode 100644 index 000000000..fb22466b0 --- /dev/null +++ b/app/views/admin_public_body_change_requests/add_accepted.txt.erb @@ -0,0 +1,9 @@ +<%= _("Dear {{user_name}},", :user_name => @change_request.get_user_name) %> + +<%= _("Thanks for your suggestion to add {{public_body_name}}. It's been added to the site here:", :public_body_name => @change_request.public_body_name) %> + +<%= _("[Authority URL will be inserted here]")%> + +<%= _("Yours,") %> + +<%= _("The {{site_name}} team.", :site_name => site_name) %> diff --git a/app/views/admin_public_body_change_requests/update_accepted.txt.erb b/app/views/admin_public_body_change_requests/update_accepted.txt.erb new file mode 100644 index 000000000..9c29c959b --- /dev/null +++ b/app/views/admin_public_body_change_requests/update_accepted.txt.erb @@ -0,0 +1,7 @@ +<%= _("Dear {{user_name}},", :user_name => @change_request.get_user_name) %> + +<%= _("Thanks for your suggestion to update the email address for {{public_body_name}} to {{public_body_email}}. This has now been done and any new requests will be sent to the new address.", :public_body_name => @change_request.public_body.name, :public_body_email => @change_request.public_body_email) %> + +<%= _("Yours,") %> + +<%= _("The {{site_name}} team.", :site_name => site_name) %> diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index aecbbf42b..095d23245 100644 --- a/spec/controllers/admin_public_body_controller_spec.rb +++ b/spec/controllers/admin_public_body_controller_spec.rb @@ -37,6 +37,33 @@ describe AdminPublicBodyController, "when showing a public body" do end +describe AdminPublicBodyController, 'when showing the form for a new public body' do + + it 'should assign a new public body to the view' do + get :new + assigns[:public_body].should be_a(PublicBody) + end + + context 'when passed a change request id as a param' do + render_views + + it 'should populate the name, email address and last edit comment on the public body' do + change_request = FactoryGirl.create(:add_body_request) + get :new, :change_request_id => change_request.id + assigns[:public_body].name.should == change_request.public_body_name + assigns[:public_body].request_email.should == change_request.public_body_email + assigns[:public_body].last_edit_comment.should match('Notes: Please') + end + + it 'should assign a default response text to the view' do + change_request = FactoryGirl.create(:add_body_request) + get :new, :change_request_id => change_request.id + assigns[:change_request_user_response].should match("Thanks for your suggestion to add A New Body") + end + end + +end + describe AdminPublicBodyController, "when creating a public body" do render_views @@ -85,6 +112,35 @@ describe AdminPublicBodyController, "when creating a public body" do response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id=>body.id) end + context 'when the body is being created as a result of a change request' do + + before do + @change_request = FactoryGirl.create(:add_body_request) + post :create, { :public_body => { :name => "New Quango", + :short_name => "", + :tag_string => "blah", + :request_email => 'newquango@localhost', + :last_edit_comment => 'From test code' }, + :change_request_id => @change_request.id, + :subject => 'Adding a new body', + :response => 'The URL will be [Authority URL will be inserted here]'} + end + + it 'should send a response to the requesting user' do + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.subject.should == 'Adding a new body' + mail.to.should == [@change_request.get_user_email] + mail.body.should =~ /The URL will be http:\/\/test.host\/body\/new_quango/ + end + + it 'should mark the change request as closed' do + PublicBodyChangeRequest.find(@change_request.id).is_open.should be_false + end + + end + end describe AdminPublicBodyController, "when editing a public body" do @@ -102,6 +158,27 @@ describe AdminPublicBodyController, "when editing a public body" do assigns[:public_body].name.should == 'Department for Humpadinking' response.should render_template('edit') end + + context 'when passed a change request id as a param' do + render_views + + before do + @change_request = FactoryGirl.create(:update_body_request) + get :edit, :id => @change_request.public_body_id, :change_request_id => @change_request.id + end + + it 'should populate the email address and last edit comment on the public body' do + change_request = FactoryGirl.create(:update_body_request) + get :edit, :id => change_request.public_body_id, :change_request_id => change_request.id + assigns[:public_body].request_email.should == @change_request.public_body_email + assigns[:public_body].last_edit_comment.should match('Notes: Please') + end + + it 'should assign a default response text to the view' do + assigns[:change_request_user_response].should match("Thanks for your suggestion to update the email address") + end + end + end describe AdminPublicBodyController, "when updating a public body" do @@ -150,6 +227,35 @@ describe AdminPublicBodyController, "when updating a public body" do pb.name.should == "Department for Humpadinking" end end + + context 'when the body is being updated as a result of a change request' do + + before do + @change_request = FactoryGirl.create(:update_body_request) + post :update, { :id => @change_request.public_body_id, + :public_body => { :name => "New Quango", + :short_name => "", + :request_email => 'newquango@localhost', + :last_edit_comment => 'From test code' }, + :change_request_id => @change_request.id, + :subject => 'Body update', + :response => 'Done.'} + end + + it 'should send a response to the requesting user' do + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.subject.should == 'Body update' + mail.to.should == [@change_request.get_user_email] + mail.body.should =~ /Done./ + end + + it 'should mark the change request as closed' do + PublicBodyChangeRequest.find(@change_request.id).is_open.should be_false + end + + end end describe AdminPublicBodyController, "when destroying a public body" do diff --git a/spec/factories.rb b/spec/factories.rb index 7d8f94ac1..8b724ae37 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -144,4 +144,16 @@ FactoryGirl.define do track_query 'Example Query' end + factory :public_body_change_request do + user + source_url 'http://www.example.com' + notes 'Please' + public_body_email 'new@example.com' + factory :add_body_request do + public_body_name 'A New Body' + end + factory :update_body_request do + public_body + end + end end diff --git a/spec/models/public_body_change_request_spec.rb b/spec/models/public_body_change_request_spec.rb index 653a218e3..0c4cea67b 100644 --- a/spec/models/public_body_change_request_spec.rb +++ b/spec/models/public_body_change_request_spec.rb @@ -108,3 +108,32 @@ describe PublicBodyChangeRequest, 'get_public_body_name' do end +describe PublicBodyChangeRequest, 'when creating a comment for the associated public body' do + + it 'should include requesting user, source_url and notes' do + change_request = PublicBodyChangeRequest.new(:user_name => 'Test User', + :user_email => 'test@example.com', + :source_url => 'http://www.example.com', + :notes => 'Some notes') + expected = "Requested by: Test User (test@example.com)\nSource URL: http://www.example.com\nNotes: Some notes" + change_request.comment_for_public_body.should == expected + end + +end + +describe PublicBodyChangeRequest, 'when creating a default subject for a response email' do + + it 'should create an appropriate subject for a request to add a body' do + change_request = PublicBodyChangeRequest.new(:public_body_name => 'Test Body') + change_request.default_response_subject.should == 'Your request to add Test Body to Alaveteli' + end + + it 'should create an appropriate subject for a request to update an email address' do + public_body = FactoryGirl.build(:public_body) + change_request = PublicBodyChangeRequest.new(:public_body => public_body) + change_request.default_response_subject.should == "Your request to update #{public_body.name} on Alaveteli" + + end + +end + |