Browse Source

Add model validation

pull/1684/head
Jack S 3 years ago
parent
commit
f8b46e4faf
  1. 2
      app/components/data_sharing_agreement_banner_component.html.erb
  2. 8
      app/models/log.rb
  3. 2
      config/locales/en.yml
  4. 21
      spec/shared/shared_log_examples.rb

2
app/components/data_sharing_agreement_banner_component.html.erb

@ -1,7 +1,7 @@
<% if display_banner? %> <% if display_banner? %>
<%= govuk_notification_banner(title_text: "Important") do %> <%= govuk_notification_banner(title_text: "Important") do %>
<p class="govuk-notification-banner__heading govuk-!-width-full" style="max-width: fit-content"> <p class="govuk-notification-banner__heading govuk-!-width-full" style="max-width: fit-content">
Your organisation must accept the Data Sharing Agreement before you can create any logs. <%= I18n.t("validations.organisation.data_sharing_agreement_not_signed") %>
<p> <p>
<%= govuk_link_to( <%= govuk_link_to(
"Read the Data Sharing Agreement", "Read the Data Sharing Agreement",

8
app/models/log.rb

@ -9,6 +9,7 @@ class Log < ApplicationRecord
belongs_to :bulk_upload, optional: true belongs_to :bulk_upload, optional: true
before_save :update_status! before_save :update_status!
before_validation :verify_dsa_signed, on: :create
STATUS = { STATUS = {
"not_started" => 0, "not_started" => 0,
@ -177,6 +178,13 @@ class Log < ApplicationRecord
private private
def verify_dsa_signed
return unless owning_organisation
return if owning_organisation.data_sharing_agreement.present?
errors.add :owning_organisation, I18n.t("validations.organisation.data_sharing_agreement_not_signed")
end
# Handle logs that are older than previous collection start date # Handle logs that are older than previous collection start date
def older_than_previous_collection_year? def older_than_previous_collection_year?
return false unless startdate return false unless startdate

2
config/locales/en.yml

@ -105,7 +105,6 @@ en:
confirm_soft_errors: confirm_soft_errors:
blank: You must select if there are errors in these fields blank: You must select if there are errors in these fields
activerecord: activerecord:
attributes: attributes:
user: user:
@ -178,6 +177,7 @@ en:
validations: validations:
organisation: organisation:
data_sharing_agreement_not_signed: Your organisation must accept the Data Sharing Agreement before you can create any logs.
name_missing: "Enter the name of the organisation" name_missing: "Enter the name of the organisation"
provider_type_missing: "Select the organisation type" provider_type_missing: "Select the organisation type"
stock_owner: stock_owner:

21
spec/shared/shared_log_examples.rb

@ -104,5 +104,26 @@ RSpec.shared_examples "shared log examples" do |log_type|
end end
end end
end end
describe "#verify_dsa_signed" do
it "is valid if the DSA is signed" do
log = build(log_type, :in_progress, owning_organisation: create(:organisation))
expect(log).to be_valid
end
it "is not valid if the DSA is not signed" do
log = build(log_type, owning_organisation: create(:organisation, :without_dsa))
expect(log).not_to be_valid
expect(log.errors[:owning_organisation]).to eq(["Your organisation must accept the Data Sharing Agreement before you can create any logs."])
end
it "is valid when owning_organisation nil" do
log = build(log_type, owning_organisation: nil)
expect(log).to be_valid
end
end
end end
# rubocop:enable RSpec/AnyInstance # rubocop:enable RSpec/AnyInstance

Loading…
Cancel
Save