Class: Arkaan::Campaigns::Files::Document

Inherits:
Object
  • Object
show all
Includes:
Concerns::Nameable, Arkaan::Concerns::MimeTypable, Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/arkaan/campaigns/files/document.rb

Overview

A document is any piece of data added by a user to a campaign. It can either be a direct text note, or an uploaded document for example.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#permissionArkaan::Campaigns::Permission

Returns the permissions granted to the different users of the campaign concerning this document.

Returns:

  • (Arkaan::Campaigns::Permission)

    the permissions granted to the different users of the campaign concerning this document.



20
# File 'lib/arkaan/campaigns/files/document.rb', line 20

has_many :permissions, class_name: 'Arkaan::Campaigns::Files::Permission', inverse_of: :document

#sizeInteger

Returns the size of the document in bytes.

Returns:

  • (Integer)

    the size of the document in bytes.



14
# File 'lib/arkaan/campaigns/files/document.rb', line 14

field :size, type: Integer, default: 0

Instance Method Details

#campaignObject



58
59
60
# File 'lib/arkaan/campaigns/files/document.rb', line 58

def campaign
  permissions.first.invitation.campaign rescue nil
end

#creatorArkaan::Account

Custom getter for the creator of the campaign.

Returns:

  • (Arkaan::Account)

    the account of the player that created this document.



32
33
34
# File 'lib/arkaan/campaigns/files/document.rb', line 32

def creator
  return permissions.where(enum_level: :creator).first.invitation.
end

#creator=(invitation) ⇒ Object

Custom setter for the creator of the document so that it can be used as a normal field.

Parameters:



24
25
26
27
28
# File 'lib/arkaan/campaigns/files/document.rb', line 24

def creator=(invitation)
  if !permissions.where(invitation: invitation).exists?
    Arkaan::Campaigns::Files::Permission.create(enum_level: :creator, document: self, invitation: invitation)
  end
end

#has_permission?(account) ⇒ Boolean

Checks if the account has the permission to access the designated document.

Parameters:

  • account (Arkaan::Account)

    the account to check the existence of a permission for.

Returns:

  • (Boolean)

    TRUE if the account has a permission to access this document, FALSE otherwise.



53
54
55
56
# File 'lib/arkaan/campaigns/files/document.rb', line 53

def has_permission?()
  invitation = campaign.invitations.where(account: ).first
  return !invitation.nil? && permissions.where(invitation: invitation).exists?
end

#is_allowed?(account) ⇒ Boolean

Checks if an account is allowed to access this document, accounts must have one of the following characteristics to read a document :

  • Be the creator of the document

  • Be the game master of the campaign in which the document is declared

  • Have been granted the permission to access the document

Parameters:

Returns:

  • (Boolean)

    TRUE if this account has the right to access the document, FALSe otherwise.



43
44
45
46
47
48
# File 'lib/arkaan/campaigns/files/document.rb', line 43

def is_allowed?()
  return true if campaign.creator.id == .id
  return true if creator.id == .id
  return true if has_permission?()
  return false
end