add role validation from a set list

This commit is contained in:
James Smith committed 2024-03-21 14:39:16 +00:00
1 parent e5d38f1267
commit a73fbb0ebf
2 files changed
+15

No files matched your search

+10
View File
@@ -1,4 +1,11 @@
class Role < ApplicationRecord
ROLES = [
:administrator, # Can do everything
:editor, # Can edit any models
:contributor, # Can upload models and edit their own
:viewer # Can view models; read only access
]
has_many :users, through: :users_roles
belongs_to :resource,
@@ -9,5 +16,8 @@ class Role < ApplicationRecord
inclusion: {in: Rolify.resource_types},
allow_nil: true
validates :name,
inclusion: {in: ROLES.map(&:to_s)}
scopify
end
+5
View File
@@ -7,4 +7,9 @@ RSpec.describe Role do
user.add_role :administrator
expect(user.is_administrator?).to be true
end
it "must be from the allowed list" do # rubocop:disable RSpec/MultipleExpectations
expect { user.add_role :batman }.to raise_error(ActiveRecord::RecordInvalid)
expect(user.is_batman?).to be false
end
end