1
0
Fork 0

Validate AccountRole#deleted_at

This commit is contained in:
Alex Kotov 2019-02-02 09:42:35 +05:00
parent 3881972965
commit 3fac766bef
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
2 changed files with 29 additions and 0 deletions

View file

@ -5,4 +5,14 @@ class AccountRole < ApplicationRecord
belongs_to :role belongs_to :role
scope :active, -> { where(deleted_at: nil) } scope :active, -> { where(deleted_at: nil) }
validate :deleted_at_is_not_in_future
private
def deleted_at_is_not_in_future
return if deleted_at.nil?
errors.add :deleted_at unless deleted_at <= Time.zone.now + 10
end
end end

View file

@ -7,4 +7,23 @@ RSpec.describe AccountRole do
it { is_expected.to belong_to :role } it { is_expected.to belong_to :role }
pending '.active' pending '.active'
describe '#deleted_at' do
def allow_value(*)
super.for :deleted_at
end
it { is_expected.to allow_value nil }
it { is_expected.to allow_value Faker::Time.backward.utc }
it { is_expected.to allow_value Time.zone.now }
it { is_expected.to allow_value 1.minute.ago }
it { is_expected.to allow_value 1.hour.ago }
it { is_expected.to allow_value 1.day.ago }
it { is_expected.not_to allow_value Faker::Time.forward.utc + 1.minute }
it { is_expected.not_to allow_value 1.minute.from_now }
it { is_expected.not_to allow_value 1.hour.from_now }
it { is_expected.not_to allow_value 1.day.from_now }
end
end end