diff options
-rw-r--r-- | app/controllers/info_request_batch_controller.rb | 7 | ||||
-rw-r--r-- | app/controllers/request_controller.rb | 40 | ||||
-rw-r--r-- | app/models/info_request.rb | 14 | ||||
-rw-r--r-- | config/routes.rb | 2 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 8 |
5 files changed, 52 insertions, 19 deletions
diff --git a/app/controllers/info_request_batch_controller.rb b/app/controllers/info_request_batch_controller.rb new file mode 100644 index 000000000..1e1030b53 --- /dev/null +++ b/app/controllers/info_request_batch_controller.rb @@ -0,0 +1,7 @@ +class InfoRequestBatchController < ApplicationController + + def show + @info_request_batch = InfoRequestBatch.find(params[:id]) + end + +end diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb index 821fcb74a..eac142fac 100644 --- a/app/controllers/request_controller.rb +++ b/app/controllers/request_controller.rb @@ -205,16 +205,11 @@ class RequestController < ApplicationController return render_new_compose(batch=true) end - @info_request = InfoRequest.new(params[:info_request]) + @info_request = InfoRequest.create_from_attributes(params[:info_request], + params[:outgoing_message], + authenticated_user) + @outgoing_message = @info_request.outgoing_messages.first @info_request.is_batch_request_template = true - @outgoing_message = OutgoingMessage.new(params[:outgoing_message].merge({ - :status => 'ready', - :message_type => 'initial_request', - :what_doing => 'normal_sort' - })) - @info_request.outgoing_messages << @outgoing_message - @outgoing_message.info_request = @info_request - @info_request.user = authenticated_user if !@info_request.valid? # We don't want the error "Outgoing messages is invalid", as in this # case the list of errors will also contain a more specific error @@ -229,10 +224,21 @@ class RequestController < ApplicationController return render_new_preview end - # TODO: create info requests and associated outgoing messages from this - # template request, and send those that can be sent, giving messages about bodies + # TODO: give messages about bodies # that are no longer requestable - + @info_request_batch = InfoRequestBatch.create!(:title => params[:info_request][:title], + :user => authenticated_user) + @public_bodies = PublicBody.where({:id => params[:public_body_ids]}).all + @public_bodies.each do |public_body| + info_request = InfoRequest.create_from_attributes(params[:info_request], + params[:outgoing_message], + authenticated_user) + info_request.public_body_id = public_body.id + info_request.info_request_batch = @info_request_batch + info_request.save! + info_request.outgoing_messages.first.send_message + end + redirect_to info_request_batch_path(@info_request_batch) end # Page new form posts to @@ -289,13 +295,9 @@ class RequestController < ApplicationController @existing_request = InfoRequest.find_by_existing_request(params[:info_request][:title], params[:info_request][:public_body_id], params[:outgoing_message][:body]) # Create both FOI request and the first request message - @info_request = InfoRequest.new(params[:info_request]) - @outgoing_message = OutgoingMessage.new(params[:outgoing_message].merge({ - :status => 'ready', - :message_type => 'initial_request' - })) - @info_request.outgoing_messages << @outgoing_message - @outgoing_message.info_request = @info_request + @info_request = InfoRequest.create_from_attributes(params[:info_request], + params[:outgoing_message]) + @outgoing_message = @info_request.outgoing_messages.first # Maybe we lost the address while they're writing it if !@info_request.public_body.is_requestable? diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 3c3afe0ea..b990f4b41 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -936,6 +936,20 @@ public self.idhash = InfoRequest.hash_from_id(self.id) end + def InfoRequest.create_from_attributes(info_request_atts, outgoing_message_atts, user=nil) + info_request = new(info_request_atts) + default_message_params = { + :status => 'ready', + :message_type => 'initial_request', + :what_doing => 'normal_sort' + } + outgoing_message = OutgoingMessage.new(outgoing_message_atts.merge(default_message_params)) + info_request.outgoing_messages << outgoing_message + outgoing_message.info_request = info_request + info_request.user = user + info_request + end + def InfoRequest.hash_from_id(id) return Digest::SHA1.hexdigest(id.to_s + AlaveteliConfiguration::incoming_email_secret)[0,8] end diff --git a/config/routes.rb b/config/routes.rb index 2c4987db6..8a7de6fdd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -65,6 +65,8 @@ Alaveteli::Application.routes.draw do resource :report, :only => [:new, :create] end + resources :info_request_batch, :only => :show + #### User controller # Use /profile for things to do with the currently signed in user. # Use /user/XXXX for things that anyone can see about that user. diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index df32a1397..6652d8f1f 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -2565,6 +2565,14 @@ describe RequestController, "#new_batch", :focus => true do response.should render_template('new') end + it 'should create an info request batch and redirect to the new batch on success' do + params = @default_post_params.merge(:preview => 0) + post :new_batch, params, { :user_id => @user.id } + new_info_request_batch = assigns[:info_request_batch] + new_info_request_batch.should_not be_nil + response.should redirect_to(info_request_batch_path(new_info_request_batch)) + end + context "when the user is banned" do before do |