aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/info_request.rb
diff options
context:
space:
mode:
authorRobin Houston <robin.houston@gmail.com>2012-06-20 13:58:05 +0100
committerRobin Houston <robin.houston@gmail.com>2012-06-20 13:58:05 +0100
commita7cc84b9b2b430644fe23e6328d7ab289e7abf0a (patch)
treef327332c83d35741405be98641fa5b474db2179f /app/models/info_request.rb
parent6ef57090e436e01bc5de801bc7acb6bfadb5c490 (diff)
parent89459d3902583fa3d6dad78462d2bf2fa6f94db6 (diff)
Merge branch 'feature/public-body-api' into develop
Diffstat (limited to 'app/models/info_request.rb')
-rw-r--r--app/models/info_request.rb43
1 files changed, 40 insertions, 3 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index b02be7ce9..cf82bd854 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -33,7 +33,7 @@ class InfoRequest < ActiveRecord::Base
validates_format_of :title, :with => /[a-zA-Z]/, :message => N_("Please write a summary with some text in it"), :if => Proc.new { |info_request| !info_request.title.nil? && !info_request.title.empty? }
belongs_to :user
- #validates_presence_of :user_id # breaks during construction of new ones :(
+ validate :must_be_internal_or_external
belongs_to :public_body
validates_presence_of :public_body_id
@@ -104,6 +104,43 @@ class InfoRequest < ActiveRecord::Base
errors.add(:described_state, "is not a valid state") if
!InfoRequest.enumerate_states.include? described_state
end
+
+ # The request must either be internal, in which case it has
+ # a foreign key reference to a User object and no external_url or external_user_name,
+ # or else be external in which case it has no user_id but does have an external_url,
+ # and may optionally also have an external_user_name.
+ #
+ # External requests are requests that have been added using the API, whereas internal
+ # requests are requests made using the site.
+ def must_be_internal_or_external
+ # We must permit user_id and external_user_name both to be nil, because the system
+ # allows a request to be created by a non-logged-in user.
+ if !user_id.nil?
+ errors.add(:external_user_name, "must be null for an internal request") if !external_user_name.nil?
+ errors.add(:external_url, "must be null for an internal request") if !external_url.nil?
+ end
+ end
+
+ def is_external?
+ !external_url.nil?
+ end
+
+ def user_name
+ is_external? ? external_user_name : user.name
+ end
+
+ def user_name_slug
+ if is_external?
+ if external_user_name.nil?
+ fake_slug = "anonymous"
+ else
+ fake_slug = external_user_name.parameterize
+ end
+ public_body.url_name + "_"+fake_slug
+ else
+ user.url_name
+ end
+ end
@@custom_states_loaded = false
begin
@@ -232,7 +269,7 @@ public
return self.magic_email("request-")
end
def incoming_name_and_email
- return TMail::Address.address_from_name_and_email(self.user.name, self.incoming_email).to_s
+ return TMail::Address.address_from_name_and_email(self.user_name, self.incoming_email).to_s
end
# Subject lines for emails about the request
@@ -453,7 +490,7 @@ public
self.save!
end
self.info_request_events.each { |event| event.xapian_mark_needs_index } # for the "waiting_classification" index
- RequestMailer.deliver_new_response(self, incoming_message)
+ RequestMailer.deliver_new_response(self, incoming_message) if !is_external?
end