Skip to content

Commit

Permalink
Merge pull request #325 from wearefuturegov/feature/fix-organisation-…
Browse files Browse the repository at this point in the history
…name-requirement-bug

Fix organisation name requirement bug
  • Loading branch information
apricot13 authored Apr 22, 2024
2 parents 2ad156a + 9f48754 commit acb848d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/models/organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions app/views/organisations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<%= render "shared/errors", model: @organisation %>

<div class="field-section field-section--two-cols">
<div class="field field--required">
<div class="field">
<%= 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" %>
</div>
</div>

Expand Down
22 changes: 20 additions & 2 deletions spec/models/organisation_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit acb848d

Please sign in to comment.