From 9a5c01d781129aeb1ab2b18771ac131ecdfb56c1 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Thu, 24 Oct 2013 14:35:25 +0100 Subject: Add InfoRequestBatch model. Re-annotate models. Index InfoRequestBatches by user - we'll display the batches for a user when they view their own requests. --- app/models/info_request_batch.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 app/models/info_request_batch.rb (limited to 'app/models/info_request_batch.rb') diff --git a/app/models/info_request_batch.rb b/app/models/info_request_batch.rb new file mode 100644 index 000000000..af6956095 --- /dev/null +++ b/app/models/info_request_batch.rb @@ -0,0 +1,20 @@ +# == Schema Information +# Schema version: 20131024114346 +# +# Table name: info_request_batches +# +# id :integer not null, primary key +# title :text not null +# user_id :integer not null +# created_at :datetime +# updated_at :datetime +# + +class InfoRequestBatch < ActiveRecord::Base + has_many :info_requests + belongs_to :user + + validates_presence_of :user + validates_presence_of :title + +end -- cgit v1.2.3 From ce262657761c1c47c47e2f6cc7bf683543ebbdb7 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Thu, 24 Oct 2013 17:03:52 +0100 Subject: Add check for double submission. --- app/models/info_request_batch.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'app/models/info_request_batch.rb') diff --git a/app/models/info_request_batch.rb b/app/models/info_request_batch.rb index af6956095..05175a889 100644 --- a/app/models/info_request_batch.rb +++ b/app/models/info_request_batch.rb @@ -16,5 +16,17 @@ class InfoRequestBatch < ActiveRecord::Base validates_presence_of :user validates_presence_of :title + validates_presence_of :body + + # When constructing a new batch, use this to check user hasn't double submitted. + def InfoRequestBatch.find_existing(user, title, body, public_body_ids) + find(:first, :conditions => ['info_request_batches.user_id = ? + AND info_request_batches.title = ? + AND body = ? + AND info_requests.public_body_id in (?)', + user, title, body, public_body_ids], + :include => :info_requests) + end + end -- cgit v1.2.3 From 97b7bc835eaa59da76c50db2c3105e4adcf89a8d Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Mon, 28 Oct 2013 12:03:52 +0000 Subject: Move creation of batch to InfoRequestBatch. --- app/models/info_request_batch.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'app/models/info_request_batch.rb') diff --git a/app/models/info_request_batch.rb b/app/models/info_request_batch.rb index 05175a889..3c6addb70 100644 --- a/app/models/info_request_batch.rb +++ b/app/models/info_request_batch.rb @@ -29,4 +29,25 @@ class InfoRequestBatch < ActiveRecord::Base end + def InfoRequestBatch.create_batch!(info_request_params, outgoing_message_params, public_body_ids, user) + info_request_batch = InfoRequestBatch.create!(:title => info_request_params[:title], + :body => outgoing_message_params[:body], + :user => user) + public_bodies = PublicBody.where({:id => public_body_ids}).all + unrequestable = [] + public_bodies.each do |public_body| + if public_body.is_requestable? + info_request = InfoRequest.create_from_attributes(info_request_params, + outgoing_message_params, + 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 + else + unrequestable << public_body + end + end + return {:batch => info_request_batch, :unrequestable => unrequestable} + end end -- cgit v1.2.3 From 90fd2c06e48c17909b9a7d2aae6d01c41047b4de Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Mon, 28 Oct 2013 13:33:08 +0000 Subject: Add the specific salutations to each request. Also, wrap model creation in a transaction and do the message sending separately - we may ultimately want to do this outside the request cycle. --- app/models/info_request_batch.rb | 55 +++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 20 deletions(-) (limited to 'app/models/info_request_batch.rb') diff --git a/app/models/info_request_batch.rb b/app/models/info_request_batch.rb index 3c6addb70..1a2605e36 100644 --- a/app/models/info_request_batch.rb +++ b/app/models/info_request_batch.rb @@ -28,26 +28,41 @@ class InfoRequestBatch < ActiveRecord::Base :include => :info_requests) end - + # Create a batch of information requests, returning the batch, and 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 = InfoRequestBatch.create!(:title => info_request_params[:title], - :body => outgoing_message_params[:body], - :user => user) - public_bodies = PublicBody.where({:id => public_body_ids}).all - unrequestable = [] - public_bodies.each do |public_body| - if public_body.is_requestable? - info_request = InfoRequest.create_from_attributes(info_request_params, - outgoing_message_params, - 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 - else - unrequestable << public_body - end - end - return {:batch => info_request_batch, :unrequestable => unrequestable} + info_request_batch = nil + 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) + 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} + 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) + info_request.public_body_id = public_body.id + info_request.info_request_batch = self + info_request.save! + info_request end end -- cgit v1.2.3 From abc1b8069da09d3ef8523ed64c60b2790a41bf01 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 27 Nov 2013 10:59:43 +0000 Subject: Add association between batches and public bodies. --- app/models/info_request_batch.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'app/models/info_request_batch.rb') diff --git a/app/models/info_request_batch.rb b/app/models/info_request_batch.rb index 1a2605e36..c4075e5e4 100644 --- a/app/models/info_request_batch.rb +++ b/app/models/info_request_batch.rb @@ -13,6 +13,7 @@ class InfoRequestBatch < ActiveRecord::Base has_many :info_requests belongs_to :user + has_and_belongs_to_many :public_bodies validates_presence_of :user validates_presence_of :title -- cgit v1.2.3 From 51c80db7a35a41fbbab9b28e86a5d60166791b4c Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 27 Nov 2013 12:03:28 +0000 Subject: Split the creation of a batch and the associated requests. We're going to want to actually create and send the requests later. --- app/models/info_request_batch.rb | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) (limited to 'app/models/info_request_batch.rb') 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! -- cgit v1.2.3 From d898b9308f98f84c4f44bcc3316193e80b007b23 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 27 Nov 2013 12:05:11 +0000 Subject: Use public bodies not requests in find_existing The requests may not have been created at this point. --- app/models/info_request_batch.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'app/models/info_request_batch.rb') diff --git a/app/models/info_request_batch.rb b/app/models/info_request_batch.rb index d6227808b..745cfe7d2 100644 --- a/app/models/info_request_batch.rb +++ b/app/models/info_request_batch.rb @@ -21,12 +21,12 @@ class InfoRequestBatch < ActiveRecord::Base # When constructing a new batch, use this to check user hasn't double submitted. def InfoRequestBatch.find_existing(user, title, body, public_body_ids) - find(:first, :conditions => ['info_request_batches.user_id = ? - AND info_request_batches.title = ? + find(:first, :conditions => ['user_id = ? + AND title = ? AND body = ? - AND info_requests.public_body_id in (?)', + AND info_request_batches_public_bodies.public_body_id in (?)', user, title, body, public_body_ids], - :include => :info_requests) + :include => :public_bodies) end # Create a batch of information requests, returning a list of public bodies -- cgit v1.2.3 From e55d7850d36b147275c2f8ffcacccbcf97e0a1d5 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 27 Nov 2013 17:40:59 +0000 Subject: Simplify return value, set sent_at in create_batch! --- app/models/info_request_batch.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'app/models/info_request_batch.rb') diff --git a/app/models/info_request_batch.rb b/app/models/info_request_batch.rb index 745cfe7d2..7e7390736 100644 --- a/app/models/info_request_batch.rb +++ b/app/models/info_request_batch.rb @@ -42,9 +42,12 @@ class InfoRequestBatch < ActiveRecord::Base unrequestable << public_body end end + self.sent_at = Time.now + self.save! end created.each{ |info_request| info_request.outgoing_messages.first.send_message } - return {:unrequestable => unrequestable} + + return unrequestable end # Create and send an FOI request to a public body -- cgit v1.2.3 From d0a78300b84021e50268c1a7f947f0ddd5d35f51 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 27 Nov 2013 18:39:03 +0000 Subject: Send batch requests as a cron job. --- app/models/info_request_batch.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'app/models/info_request_batch.rb') diff --git a/app/models/info_request_batch.rb b/app/models/info_request_batch.rb index 7e7390736..498ab4951 100644 --- a/app/models/info_request_batch.rb +++ b/app/models/info_request_batch.rb @@ -61,4 +61,13 @@ class InfoRequestBatch < ActiveRecord::Base info_request.save! info_request end + + def InfoRequestBatch.send_batches() + find_each(:conditions => "sent_at IS NULL") do |info_request_batch| + unrequestable = info_request_batch.create_batch! + mail_message = InfoRequestBatchMailer.batch_sent(info_request_batch, + unrequestable, + info_request_batch.user).deliver + end + end end -- cgit v1.2.3