aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/info_request_batch.rb
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2014-01-29 16:10:52 +0000
committerLouise Crow <louise.crow@gmail.com>2014-01-29 16:10:52 +0000
commit184ffeccb7f4579b481db9b7744aa9baed70562f (patch)
tree67c023b029a699a1e727ef6becdc0832e82ea1c5 /app/models/info_request_batch.rb
parente44c8b875fd4ad46b954ef9c31bdb6f0366dcb9e (diff)
parent79b2f672aeae394a2c195d89b70bda27bb3201a4 (diff)
Merge branch 'feature/batch-requests' into rails-3-develop
Conflicts: config/general.yml-example spec/factories.rb
Diffstat (limited to 'app/models/info_request_batch.rb')
-rw-r--r--app/models/info_request_batch.rb73
1 files changed, 73 insertions, 0 deletions
diff --git a/app/models/info_request_batch.rb b/app/models/info_request_batch.rb
new file mode 100644
index 000000000..498ab4951
--- /dev/null
+++ b/app/models/info_request_batch.rb
@@ -0,0 +1,73 @@
+# == 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
+ has_and_belongs_to_many :public_bodies
+
+ 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 => ['user_id = ?
+ AND title = ?
+ AND body = ?
+ AND info_request_batches_public_bodies.public_body_id in (?)',
+ user, title, body, public_body_ids],
+ :include => :public_bodies)
+ end
+
+ # 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 create_batch!
+ unrequestable = []
+ created = []
+ ActiveRecord::Base.transaction do
+ public_bodies.each do |public_body|
+ if public_body.is_requestable?
+ created << create_request!(public_body)
+ else
+ 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
+ end
+
+ # Create and send an FOI request to a public body
+ 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!
+ 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