Class: Consent::Permission

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_hash(permissions) ⇒ Array<Consent::Permission>

Transforms a hash of permissions and views to grant into a collection of Consent::Permission

I.e.:

Permission.from_hash User => { write: :territory }
=> [#<Consent::Permission view: :territory, action: :write, subject: User>]

Permission.from_hash User => { write: :territory, read: :all }
=> [#<Consent::Permission view: :territory, action: :write, subject: User>,]
    #<Consent::Permission view: :all, action: :read, subject: User>]

It also eliminates any invalid permission from the resulting set

I.e.:

Permission.from_hash User => { write: :territory, read: :no_access }, Department: { write: :all }
=> [#<Consent::Permission view: :territory, action: :write, subject: User>,]
    #<Consent::Permission view: :all, action: :write, subject: Department>]

Parameters:

  • permissions (Hash)

    a set of permissions in the hash format

Returns:



63
64
65
66
67
68
69
# File 'app/models/consent/permission.rb', line 63

def self.from_hash(permissions)
  permissions.flat_map do |subject, actions|
    actions.flat_map do |action, view|
      new(subject: subject, action: action, view: view)
    end
  end.select(&:valid?)
end

Instance Method Details

#actionObject

Symbol key of an action



30
31
32
# File 'app/models/consent/permission.rb', line 30

def action
  super&.to_sym
end

#replaces?(permission) ⇒ Boolean

It is true when it is a replacement for another permission

Returns:

  • (Boolean)


24
25
26
# File 'app/models/consent/permission.rb', line 24

def replaces?(permission)
  subject == permission.subject && action == permission.action
end

#viewObject

Symbol key of a view or “1” for full access



36
37
38
39
40
# File 'app/models/consent/permission.rb', line 36

def view
  return "1" if Consent::FULL_ACCESS.include?(super)

  super&.to_sym
end