1
0
Fork 0

Add model AccountRole

This commit is contained in:
Alex Kotov 2019-02-02 08:48:23 +05:00
parent 7f507c9a1e
commit 43b6e08e3f
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
6 changed files with 53 additions and 2 deletions

View file

@ -1,9 +1,20 @@
# frozen_string_literal: true # frozen_string_literal: true
class Account < ApplicationRecord class Account < ApplicationRecord
include Rolify::Role
extend Rolify::Dynamic if Rolify.dynamic_shortcuts
USERNAME_RE = /\A[a-z][_a-z0-9]*[a-z0-9]\z/.freeze USERNAME_RE = /\A[a-z][_a-z0-9]*[a-z0-9]\z/.freeze
rolify role_join_table_name: :account_roles self.role_cname = 'Role'
self.role_table_name = 'roles'
self.strict_rolify = false
self.adapter = Rolify::Adapter::Base.create 'role_adapter', role_cname, name
has_many :account_roles, dependent: :restrict_with_exception
has_many :roles, through: :account_roles
belongs_to :person, optional: true belongs_to :person, optional: true

View file

@ -0,0 +1,6 @@
# frozen_string_literal: true
class AccountRole < ApplicationRecord
belongs_to :account
belongs_to :role
end

View file

@ -5,7 +5,9 @@ class Role < ApplicationRecord
superuser superuser
].map(&:freeze).freeze ].map(&:freeze).freeze
has_and_belongs_to_many :accounts, join_table: :account_roles has_many :account_roles, dependent: :restrict_with_exception
has_many :accounts, through: :account_roles
belongs_to :resource, polymorphic: true, optional: true belongs_to :resource, polymorphic: true, optional: true

View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe AccountRole do
it { is_expected.to belong_to :account }
it { is_expected.to belong_to :role }
end

View file

@ -13,6 +13,18 @@ RSpec.describe Account do
.dependent(:restrict_with_exception) .dependent(:restrict_with_exception)
end end
it do
is_expected.to \
have_many(:account_roles)
.dependent(:restrict_with_exception)
end
it do
is_expected.to \
have_many(:roles)
.through(:account_roles)
end
it do it do
is_expected.to \ is_expected.to \
have_many(:account_telegram_contacts) have_many(:account_telegram_contacts)

View file

@ -5,6 +5,18 @@ require 'rails_helper'
RSpec.describe Role do RSpec.describe Role do
subject { create :role } subject { create :role }
it do
is_expected.to \
have_many(:account_roles)
.dependent(:restrict_with_exception)
end
it do
is_expected.to \
have_many(:accounts)
.through(:account_roles)
end
describe '#name' do describe '#name' do
def allow_value(*) def allow_value(*)
super.for :name super.for :name