1
0
Fork 0

Add attribute AccountRole#expires_at

This commit is contained in:
Alex Kotov 2019-02-08 11:42:32 +05:00
parent 356c439ab5
commit 77aaa43b64
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
5 changed files with 82 additions and 3 deletions

View file

@ -4,7 +4,17 @@ class AccountRole < ApplicationRecord
belongs_to :account
belongs_to :role
scope :active, -> { where(deleted_at: nil) }
scope :active, -> { not_deleted.not_expired }
scope :not_deleted, -> { where(deleted_at: nil) }
scope :not_expired, lambda {
where(
arel_table[:expires_at].eq(nil).or(
arel_table[:expires_at].gt(Time.zone.now),
),
)
}
validate :deleted_at_is_not_in_future

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddExpiresAtToAccountRoles < ActiveRecord::Migration[6.0]
def change
add_column :account_roles, :expires_at, :datetime
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_02_02_041009) do
ActiveRecord::Schema.define(version: 2019_02_08_062215) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -21,6 +21,7 @@ ActiveRecord::Schema.define(version: 2019_02_02_041009) do
t.bigint "account_id", null: false
t.bigint "role_id", null: false
t.datetime "deleted_at"
t.datetime "expires_at"
t.index ["account_id", "role_id"], name: "index_account_roles_on_account_id_and_role_id", unique: true
t.index ["account_id"], name: "index_account_roles_on_account_id"
t.index ["role_id"], name: "index_account_roles_on_role_id"

View file

@ -7,6 +7,8 @@ RSpec.describe AccountRole do
it { is_expected.to belong_to :role }
pending '.active'
pending '.not_deleted'
pending '.not_expired'
describe '#deleted_at' do
def allow_value(*)
@ -14,9 +16,9 @@ RSpec.describe AccountRole do
end
it { is_expected.to allow_value nil }
it { is_expected.to allow_value Time.zone.now }
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 }
@ -26,4 +28,23 @@ RSpec.describe AccountRole do
it { is_expected.not_to allow_value 1.hour.from_now }
it { is_expected.not_to allow_value 1.day.from_now }
end
describe '#expires_at' do
def allow_value(*)
super.for :expires_at
end
it { is_expected.to allow_value nil }
it { is_expected.to allow_value Time.zone.now }
it { is_expected.to allow_value Faker::Time.backward.utc }
it { is_expected.to allow_value 1.second.ago }
it { is_expected.to allow_value 1.hour.ago }
it { is_expected.to allow_value 1.day.ago }
it { is_expected.to allow_value Faker::Time.forward.utc + 1.minute }
it { is_expected.to allow_value 1.minute.from_now }
it { is_expected.to allow_value 1.hour.from_now }
it { is_expected.to allow_value 1.day.from_now }
end
end

View file

@ -132,6 +132,46 @@ RSpec.describe Account do
expect(result).to eq false
end
end
context 'before role expires' do
before do
account_role.update! expires_at: 2.seconds.from_now
end
specify do
expect(result).to eq true
end
end
context 'before role expires' do
before do
account_role.update! expires_at: Faker::Time.forward
end
specify do
expect(result).to eq true
end
end
context 'after role expires' do
before do
account_role.update! expires_at: 1.second.ago
end
specify do
expect(result).to eq false
end
end
context 'after role expires' do
before do
account_role.update! expires_at: Faker::Time.backward
end
specify do
expect(result).to eq false
end
end
end
describe '#add_role' do