Validates nonprofit's timezone

This commit is contained in:
Clarissa Borges 2021-05-19 17:45:52 -03:00 committed by Eric Schultz
parent dc1e4c8a85
commit fbc3d8e51d
2 changed files with 21 additions and 0 deletions

View file

@ -106,6 +106,7 @@ class Nonprofit < ApplicationRecord
validates_presence_of :user_id, on: :create, unless: -> {register_np_only}
validate :user_is_valid, on: :create, unless: -> {register_np_only}
validate :user_registerable_as_admin, on: :create, unless: -> {register_np_only}
validate :timezone_is_valid
scope :vetted, -> { where(vetted: true) }
scope :identity_verified, -> { where(verification_status: 'verified') }
@ -306,6 +307,12 @@ private
def user_is_valid
(user && user.is_a?(User)) || errors.add(:user_id, "is not a valid user")
end
def timezone_is_valid
timezone.blank? or
ActiveSupport::TimeZone.all.map{ |t| t.tzinfo.name }.include?(timezone) or
errors.add(:timezone, 'is not a valid timezone')
end
end

View file

@ -204,6 +204,20 @@ RSpec.describe Nonprofit, type: :model do
nonprofit.valid?
expect(nonprofit.errors['slug'].first).to match /.*could not be created.*/
end
describe 'timezone validations' do
it 'does not fail if the timezone is nil' do
expect { create(:nm_justice, timezone: nil) }.not_to raise_error(ActiveRecord::RecordInvalid)
end
it 'does not fail if the timezone is readable by postgres' do
expect { create(:nm_justice, timezone: 'America/Chicago') }.not_to raise_error(ActiveRecord::RecordInvalid)
end
it 'raises error if the timezone is invalid' do
expect { create(:nm_justice, timezone: 'Central Time (US & Canada)') }.to raise_error(ActiveRecord::RecordInvalid)
end
end
end
end
end