2018-05-07 16:47:29 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2021-02-20 09:57:27 -08:00
|
|
|
# == Schema Information
|
|
|
|
|
#
|
|
|
|
|
# Table name: roles
|
|
|
|
|
#
|
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
|
# description :string
|
|
|
|
|
# name :string
|
|
|
|
|
# resource_type :string
|
|
|
|
|
# created_at :datetime
|
|
|
|
|
# updated_at :datetime
|
|
|
|
|
# resource_id :integer
|
|
|
|
|
#
|
|
|
|
|
# Indexes
|
|
|
|
|
#
|
|
|
|
|
# index_roles_on_name (name)
|
|
|
|
|
# index_roles_on_name_and_resource_type_and_resource_id (name,resource_type,resource_id)
|
|
|
|
|
#
|
2017-09-05 22:23:34 +03:00
|
|
|
class Role < ApplicationRecord
|
2014-08-12 11:51:59 +03:00
|
|
|
belongs_to :resource, polymorphic: true
|
2016-05-24 23:54:46 +05:30
|
|
|
has_many :users_roles
|
|
|
|
|
has_many :users, through: :users_roles
|
|
|
|
|
|
2020-04-03 09:51:35 -07:00
|
|
|
has_paper_trail on: [:create, :update],
|
|
|
|
|
only: [:name, :description],
|
|
|
|
|
meta: { conference_id: :conference_id, organization_id: :organization_id }
|
2016-05-24 23:54:46 +05:30
|
|
|
|
2016-03-11 20:44:09 +05:30
|
|
|
before_destroy :cancel
|
2014-08-12 11:51:59 +03:00
|
|
|
scopify
|
|
|
|
|
|
2015-01-12 23:13:10 +02:00
|
|
|
validates :name, presence: true
|
|
|
|
|
|
|
|
|
|
validates :name, uniqueness: { scope: :resource }
|
2016-03-11 20:44:09 +05:30
|
|
|
|
2020-04-03 09:51:35 -07:00
|
|
|
def conference_id
|
|
|
|
|
resource_type == 'Conference' ? resource_id : nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def organization_id
|
|
|
|
|
resource_type == 'Organization' ? resource_id : nil
|
|
|
|
|
end
|
|
|
|
|
|
2016-03-11 20:44:09 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
|
|
# Needed to ensure that removing all user from role doesn't remove role.
|
|
|
|
|
def cancel
|
2019-05-03 17:33:14 +02:00
|
|
|
throw(:abort)
|
2016-03-11 20:44:09 +05:30
|
|
|
end
|
2014-06-30 17:07:53 +02:00
|
|
|
end
|