From 9f48754db419ce0a828cfeab6d1b209aaf5a007d Mon Sep 17 00:00:00 2001 From: Han Date: Mon, 22 Apr 2024 14:20:28 +0100 Subject: [PATCH] Fix organisaiton name requirement bug --- app/models/organisation.rb | 4 ++-- app/views/organisations/_form.html.erb | 4 ++-- spec/models/organisation_spec.rb | 22 ++++++++++++++++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/models/organisation.rb b/app/models/organisation.rb index f9158449..6ee5f2ab 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -2,8 +2,8 @@ class Organisation < ApplicationRecord has_many :services has_many :users - validates :name, presence: true, uniqueness: true - validates :name, length: { minimum: 2, maximum: 100 }, if: -> { name.present? } + # validates :name, presence: true, + validates :name, length: { minimum: 2, maximum: 100 }, uniqueness: true, if: -> { name.present? } attr_accessor :skip_mongo_callbacks after_commit :update_index, if: -> { skip_mongo_callbacks != true } diff --git a/app/views/organisations/_form.html.erb b/app/views/organisations/_form.html.erb index 384a04a3..a84753c8 100644 --- a/app/views/organisations/_form.html.erb +++ b/app/views/organisations/_form.html.erb @@ -3,9 +3,9 @@ <%= render "shared/errors", model: @organisation %>
-
+
<%= f.label :name, "Organisation name", class: "field__label" %> - <%= f.text_field :name, required: true, class: "field__input one-half" %> + <%= f.text_field :name, class: "field__input one-half" %>
diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 4815d7f3..30c5563d 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -1,6 +1,24 @@ require 'rails_helper' RSpec.describe Organisation, type: :model do - it { should validate_presence_of :name } - it { should validate_uniqueness_of :name } + it { should allow_value(nil).for(:name) } + it { should allow_value('Some Name').for(:name) } + it { should validate_uniqueness_of(:name) } + it { should validate_length_of(:name).is_at_least(2).is_at_most(100).allow_nil } + + # This tests the behavior that multiple organisations can have a nil name. + it 'allows nil names' do + first = Organisation.create!(name: nil) + second = Organisation.new(name: nil) + + expect(second).to be_valid + end + + + it 'Does not allow duplicate names' do + first = Organisation.create!(name: "Organisation 1") + second = Organisation.new(name: "Organisation 1") + + expect(second).to be_invalid + end end