Improve validation of Account#public_name, #biography
This commit is contained in:
parent
f93141d0f4
commit
c78c4ebc23
|
@ -50,6 +50,8 @@ class Account < ApplicationRecord
|
||||||
|
|
||||||
after_initialize :generate_username
|
after_initialize :generate_username
|
||||||
|
|
||||||
|
before_validation :turn_blanks_into_nils
|
||||||
|
|
||||||
before_create :generate_guest_token
|
before_create :generate_guest_token
|
||||||
|
|
||||||
###############
|
###############
|
||||||
|
@ -64,11 +66,6 @@ class Account < ApplicationRecord
|
||||||
format: USERNAME_RE,
|
format: USERNAME_RE,
|
||||||
uniqueness: { case_sensitive: false }
|
uniqueness: { case_sensitive: false }
|
||||||
|
|
||||||
validates :public_name,
|
|
||||||
allow_nil: true,
|
|
||||||
allow_blank: false,
|
|
||||||
presence: true
|
|
||||||
|
|
||||||
validates :biography, length: { maximum: 10_000 }
|
validates :biography, length: { maximum: 10_000 }
|
||||||
|
|
||||||
###########
|
###########
|
||||||
|
@ -119,4 +116,9 @@ private
|
||||||
def generate_guest_token
|
def generate_guest_token
|
||||||
self.guest_token = SecureRandom.hex
|
self.guest_token = SecureRandom.hex
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def turn_blanks_into_nils
|
||||||
|
self.public_name = nil if public_name.blank?
|
||||||
|
self.biography = nil if biography.blank?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -96,18 +96,42 @@ RSpec.describe Account do
|
||||||
end
|
end
|
||||||
|
|
||||||
it { is_expected.to allow_value nil }
|
it { is_expected.to allow_value nil }
|
||||||
|
it { is_expected.to allow_value '' }
|
||||||
it { is_expected.not_to allow_value '' }
|
it { is_expected.to allow_value ' ' }
|
||||||
it { is_expected.not_to allow_value ' ' }
|
|
||||||
|
|
||||||
it { is_expected.to allow_value Faker::Name.name }
|
it { is_expected.to allow_value Faker::Name.name }
|
||||||
it { is_expected.to allow_value Faker::Name.first_name }
|
it { is_expected.to allow_value Faker::Name.first_name }
|
||||||
it { is_expected.to allow_value 'Foo Bar' }
|
it { is_expected.to allow_value 'Foo Bar' }
|
||||||
|
|
||||||
|
context 'when it was set to blank value' do
|
||||||
|
subject { create :personal_account, public_name: ' ' * rand(100) }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect(subject.public_name).to eq nil
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#biography' do
|
describe '#biography' do
|
||||||
it { is_expected.not_to validate_presence_of :biography }
|
def allow_value(*)
|
||||||
|
super.for :biography
|
||||||
|
end
|
||||||
|
|
||||||
it { is_expected.to validate_length_of(:biography).is_at_most(10_000) }
|
it { is_expected.to validate_length_of(:biography).is_at_most(10_000) }
|
||||||
|
|
||||||
|
it { is_expected.to allow_value nil }
|
||||||
|
it { is_expected.to allow_value '' }
|
||||||
|
it { is_expected.to allow_value ' ' }
|
||||||
|
|
||||||
|
it { is_expected.to allow_value Faker::Lorem.sentence }
|
||||||
|
|
||||||
|
context 'when it was set to blank value' do
|
||||||
|
subject { create :personal_account, biography: ' ' * rand(100) }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect(subject.biography).to eq nil
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#has_role?' do
|
describe '#has_role?' do
|
||||||
|
|
Reference in New Issue