diff options
author | Robin Houston <robin.houston@gmail.com> | 2012-05-24 10:52:22 +0100 |
---|---|---|
committer | Robin Houston <robin.houston@gmail.com> | 2012-06-06 19:34:34 +0100 |
commit | d2e1be54f3e5293fb5291dff7b1f753ddeff3302 (patch) | |
tree | cca6c3b5a7c17224fa686b374e520121c7a79746 /app/models/info_request.rb | |
parent | 1d2ceb988b910acdcd60910398ccd05de1688334 (diff) |
External requests
Add data model support for “external” requests, i.e. requests added
via the API rather than made using the site.
Diffstat (limited to 'app/models/info_request.rb')
-rw-r--r-- | app/models/info_request.rb | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 095a1b1af..2f1270a95 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,22 @@ 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 + if user_id.nil? + errors.add(:external_url, "must be provided for an external request") if external_url.nil? + else + 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 @@custom_states_loaded = false begin |