scinote-web/app/models/concerns/assignable.rb

41 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module Assignable
extend ActiveSupport::Concern
included do
include Canaid::Helpers::PermissionsHelper
has_many :user_assignments, as: :assignable, dependent: :destroy
default_scope { includes(user_assignments: :user_role).distinct }
scope :readable_by_user, lambda { |user|
joins(user_assignments: :user_role)
.where(user_assignments: { user: user })
.where('? = ANY(user_roles.permissions)', "::#{self.class.to_s.split('::').first}Permissions".constantize::READ)
}
scope :managable_by_user, lambda { |user|
joins(user_assignments: :user_role)
.where(user_assignments: { user: user })
.where('? = ANY(user_roles.permissions)', "::#{self.class.to_s.split('::').first}Permissions".constantize::MANAGE)
}
after_create do
UserAssignment.create!(
user: created_by,
assignable: self,
assigned: :manually, # we set this to manually since was the user action to create the item
user_role: UserRole.find_by(name: I18n.t('user_roles.predefined.owner'))
)
UserAssignments::GenerateUserAssignmentsJob.perform_later(self, created_by)
end
def role_for_user(user)
user_assignments.find_by(user: user)&.user_role
end
end
end