Simplify garden.name validation regexp

This commit is contained in:
Brenda Wallace
2019-11-11 17:41:46 +13:00
parent cfaa18daaf
commit a651f079d8
2 changed files with 18 additions and 19 deletions

View File

@@ -22,23 +22,16 @@ class Garden < ApplicationRecord
validates :location, length: { maximum: 255 }
validates :slug, uniqueness: true
before_validation :strip_blanks
validates :name, uniqueness: { scope: :owner_id }
validates :name,
format: {
with: /\A((?!\\n)(?!\R)[\p{L}\p{M}\d\s[:ascii:](?!\\n)])*\z/
},
allow_nil: false,
allow_blank: false,
presence: true,
length: { maximum: 255 }
format: { without: /\n/, message: "must contain no newlines" },
allow_nil: false, allow_blank: false, presence: true,
length: { maximum: 255 }
validates :area,
numericality: {
only_integer: false,
greater_than_or_equal_to: 0
},
numericality: {only_integer: false, greater_than_or_equal_to: 0 },
allow_nil: true
AREA_UNITS_VALUES = {
@@ -76,4 +69,10 @@ class Garden < ApplicationRecord
p.save
end
end
protected
def strip_blanks
self.name = self.name.strip unless name.nil?
end
end

View File

@@ -37,21 +37,21 @@ describe Garden do
garden.should be_valid
end
it "allows some punctuation" do
garden = FactoryBot.build(:garden, name: "best-garden-eva!")
garden.should be_valid
end
it "doesn't allow a name with only spaces" do
garden = FactoryBot.build(:garden, name: " ")
garden.should_not be_valid
end
it "doesn't allow a new line chars in garden names" do
it "doesn't allow new line chars in garden names" do
garden = FactoryBot.build(:garden, name: "My garden\nI am a 1337 hacker")
garden.should_not be_valid
end
it "doesn't allow a new lines in garden names" do
garden = FactoryBot.build(:garden, name: 'My garden\nI am a 1337 hacker')
garden.should_not be_valid
end
it "has an owner" do
garden.owner.should be_an_instance_of Member
end