aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2013-11-27 12:03:28 +0000
committerLouise Crow <louise.crow@gmail.com>2013-12-04 09:32:44 +0000
commit51c80db7a35a41fbbab9b28e86a5d60166791b4c (patch)
treebd2cdf0c16fa6907e4cc06c41c335c630386d375
parentabc1b8069da09d3ef8523ed64c60b2790a41bf01 (diff)
Split the creation of a batch and the associated requests.
We're going to want to actually create and send the requests later.
-rw-r--r--app/controllers/request_controller.rb11
-rw-r--r--app/models/info_request_batch.rb26
-rw-r--r--spec/models/info_request_batch_spec.rb19
3 files changed, 24 insertions, 32 deletions
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb
index bef3575cc..2bf1a2c0c 100644
--- a/app/controllers/request_controller.rb
+++ b/app/controllers/request_controller.rb
@@ -244,11 +244,12 @@ class RequestController < ApplicationController
return render_new_preview
end
- batch_results = InfoRequestBatch.create_batch!(params[:info_request],
- params[:outgoing_message],
- params[:public_body_ids],
- authenticated_user)
- @info_request_batch = batch_results[:batch]
+ @info_request_batch = InfoRequestBatch.create!(:title => params[:info_request][:title],
+ :body => params[:outgoing_message][:body],
+ :public_bodies => @public_bodies,
+ :user => authenticated_user )
+
+ batch_results = @info_request_batch.create_batch!
flash[:notice] = _("<p>Your {{law_used_full}} requests have been <strong>sent</strong>!</p>
<p><strong>We will email you</strong> when there is a response to any of them, or after {{late_number_of_days}} working days if the authorities still haven't
replied by then.</p>
diff --git a/app/models/info_request_batch.rb b/app/models/info_request_batch.rb
index c4075e5e4..d6227808b 100644
--- a/app/models/info_request_batch.rb
+++ b/app/models/info_request_batch.rb
@@ -29,38 +29,30 @@ class InfoRequestBatch < ActiveRecord::Base
:include => :info_requests)
end
- # Create a batch of information requests, returning the batch, and a list of public bodies
+ # Create a batch of information requests, returning a list of public bodies
# that are unrequestable from the initial list of public body ids passed.
- def InfoRequestBatch.create_batch!(info_request_params, outgoing_message_params, public_body_ids, user)
- info_request_batch = nil
+ def create_batch!
unrequestable = []
created = []
- public_bodies = PublicBody.where({:id => public_body_ids}).all
ActiveRecord::Base.transaction do
- info_request_batch = InfoRequestBatch.create!(:title => info_request_params[:title],
- :body => outgoing_message_params[:body],
- :user => user)
public_bodies.each do |public_body|
if public_body.is_requestable?
- created << info_request_batch.create_request!(public_body,
- info_request_params,
- outgoing_message_params,
- user)
+ created << create_request!(public_body)
else
unrequestable << public_body
end
end
end
created.each{ |info_request| info_request.outgoing_messages.first.send_message }
- return {:batch => info_request_batch, :unrequestable => unrequestable}
+ return {:unrequestable => unrequestable}
end
# Create and send an FOI request to a public body
- def create_request!(public_body, info_request_params, outgoing_message_params, user)
- body = OutgoingMessage.fill_in_salutation(outgoing_message_params[:body], public_body)
- info_request = InfoRequest.create_from_attributes(info_request_params,
- outgoing_message_params.merge(:body => body),
- user)
+ def create_request!(public_body)
+ body = OutgoingMessage.fill_in_salutation(self.body, public_body)
+ info_request = InfoRequest.create_from_attributes({:title => self.title},
+ {:body => body},
+ self.user)
info_request.public_body_id = public_body.id
info_request.info_request_batch = self
info_request.save!
diff --git a/spec/models/info_request_batch_spec.rb b/spec/models/info_request_batch_spec.rb
index ba4bcd181..7f6905c8e 100644
--- a/spec/models/info_request_batch_spec.rb
+++ b/spec/models/info_request_batch_spec.rb
@@ -61,26 +61,25 @@ describe InfoRequestBatch, "when finding an existing batch" do
InfoRequestBatch.find_existing(FactoryGirl.create(:user),
@info_request_batch.title,
@info_request_batch.body,
- [@first_request.public_body_id]).should be_nil
+ [@first_body]).should be_nil
end
end
describe InfoRequestBatch, "when creating a batch", :focus => true do
it 'should substitute authority name for the placeholder in each request' do
- info_request_params = {:title => 'A test title'}
- outgoing_message_params = {:body => "Dear [Authority name],\nA message\nYours faithfully,\nRequester",
- }
+ body = "Dear [Authority name],\nA message\nYours faithfully,\nRequester"
first_public_body = FactoryGirl.create(:public_body)
second_public_body = FactoryGirl.create(:public_body)
user = FactoryGirl.create(:user)
- results = InfoRequestBatch.create_batch!(info_request_params,
- outgoing_message_params,
- [first_public_body.id, second_public_body.id],
- user)
- info_requests = results[:batch].info_requests
+ info_request_batch = InfoRequestBatch.create!({:title => 'A test title',
+ :body => body,
+ :public_bodies => [first_public_body,
+ second_public_body],
+ :user => user})
+ results = info_request_batch.create_batch!
[first_public_body, second_public_body].each do |public_body|
- request = info_requests.detect{|info_request| info_request.public_body == public_body}
+ request = info_request_batch.info_requests.detect{|info_request| info_request.public_body == public_body}
request.outgoing_messages.first.body.should == "Dear #{public_body.name},\nA message\nYours faithfully,\nRequester"
end
end