Class: AssistantConversationShare

Inherits:
ApplicationRecord show all
Defined in:
app/models/assistant_conversation_share.rb

Overview

== Schema Information

Table name: assistant_conversation_shares
Database name: primary

id :bigint not null, primary key
access_level :string default("viewer"), not null
created_at :datetime not null
updated_at :datetime not null
assistant_conversation_id :bigint not null
party_id :bigint
role_id :bigint
shared_by_id :bigint not null

Indexes

idx_conv_shares_conversation (assistant_conversation_id)
idx_conv_shares_party (party_id) WHERE (party_id IS NOT NULL)
idx_conv_shares_role (role_id) WHERE (role_id IS NOT NULL)
idx_conv_shares_unique_role (assistant_conversation_id,role_id) UNIQUE WHERE (role_id IS NOT NULL)
idx_conv_shares_unique_user (assistant_conversation_id,party_id) UNIQUE WHERE (party_id IS NOT NULL)

Foreign Keys

fk_rails_... (assistant_conversation_id => assistant_conversations.id)
fk_rails_... (party_id => parties.id)
fk_rails_... (role_id => roles.id)
fk_rails_... (shared_by_id => parties.id)

Constant Summary collapse

ACCESS_LEVELS =
%w[viewer collaborator].freeze

Instance Attribute Summary collapse

Belongs to collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#access_levelObject (readonly)



39
# File 'app/models/assistant_conversation_share.rb', line 39

validates :access_level, inclusion: { in: ACCESS_LEVELS }

Class Method Details

.collaboratorsActiveRecord::Relation<AssistantConversationShare>

A relation of AssistantConversationShares that are collaborators. Active Record Scope

Returns:

See Also:



45
# File 'app/models/assistant_conversation_share.rb', line 45

scope :collaborators, -> { where(access_level: 'collaborator') }

.for_rolesActiveRecord::Relation<AssistantConversationShare>

A relation of AssistantConversationShares that are for roles. Active Record Scope

Returns:

See Also:



43
# File 'app/models/assistant_conversation_share.rb', line 43

scope :for_roles, -> { where.not(role_id: nil) }

.for_usersActiveRecord::Relation<AssistantConversationShare>

A relation of AssistantConversationShares that are for users. Active Record Scope

Returns:

See Also:



42
# File 'app/models/assistant_conversation_share.rb', line 42

scope :for_users, -> { where.not(party_id: nil) }

.granting_access_toActiveRecord::Relation<AssistantConversationShare>

A relation of AssistantConversationShares that are granting access to. Active Record Scope

Returns:

See Also:



48
49
50
51
# File 'app/models/assistant_conversation_share.rb', line 48

scope :granting_access_to, ->(party, ) {
  where(party_id: party.id)
    .or(where(role_id: .inherited_role_ids))
}

.viewersActiveRecord::Relation<AssistantConversationShare>

A relation of AssistantConversationShares that are viewers. Active Record Scope

Returns:

See Also:



44
# File 'app/models/assistant_conversation_share.rb', line 44

scope :viewers,      -> { where(access_level: 'viewer') }

Instance Method Details

#assistant_conversationAssistantConversation



34
# File 'app/models/assistant_conversation_share.rb', line 34

belongs_to :assistant_conversation

#collaborator?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/assistant_conversation_share.rb', line 57

def collaborator?
  access_level == 'collaborator'
end

#roleRole

Returns:

See Also:



36
# File 'app/models/assistant_conversation_share.rb', line 36

belongs_to :role, optional: true

#shared_byParty

Returns:

See Also:



37
# File 'app/models/assistant_conversation_share.rb', line 37

belongs_to :shared_by, class_name: 'Party'

#target_nameObject

Human-readable target name for display



62
63
64
65
66
67
68
# File 'app/models/assistant_conversation_share.rb', line 62

def target_name
  if party_id.present?
    user&.full_name || "User ##{party_id}"
  else
    role&.name&.titleize || "Role ##{role_id}"
  end
end

#userParty

Returns:

See Also:



35
# File 'app/models/assistant_conversation_share.rb', line 35

belongs_to :user, class_name: 'Party', foreign_key: :party_id, optional: true

#viewer?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/models/assistant_conversation_share.rb', line 53

def viewer?
  access_level == 'viewer'
end