aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/info_request.rb18
-rw-r--r--db/migrate/113_add_external_fields_to_info_requests.rb22
2 files changed, 39 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
diff --git a/db/migrate/113_add_external_fields_to_info_requests.rb b/db/migrate/113_add_external_fields_to_info_requests.rb
new file mode 100644
index 000000000..3aea57766
--- /dev/null
+++ b/db/migrate/113_add_external_fields_to_info_requests.rb
@@ -0,0 +1,22 @@
+class AddExternalFieldsToInfoRequests < ActiveRecord::Migration
+ def self.up
+ change_column_null :info_requests, :user_id, true
+ add_column :info_requests, :external_user_name, :string, :null => true
+ add_column :info_requests, :external_url, :string, :null => true
+
+ if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
+ execute "ALTER TABLE info_requests ADD CONSTRAINT info_requests_external_ck CHECK ( (user_id is null) = (external_url is not null) and (external_user_name is not null or external_url is null) )"
+ end
+ end
+
+ def self.down
+ if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
+ execute "ALTER TABLE info_requests DROP CONSTRAINT info_requests_external_ck"
+ end
+
+ remove_column :info_requests, :external_url
+ remove_column :info_requests, :external_user_name
+
+ change_column_null :info_requests, :user_id, false
+ end
+end