Class: Consent::Permission
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Consent::Permission
- Defined in:
- app/models/consent/permission.rb
Class Method Summary collapse
-
.from_hash(permissions) ⇒ Array<Consent::Permission>
Transforms a hash of permissions and views to grant into a collection of Consent::Permission.
Instance Method Summary collapse
-
#action ⇒ Object
Symbol key of an action.
-
#replaces?(permission) ⇒ Boolean
It is true when it is a replacement for another permission.
-
#view ⇒ Object
Symbol key of a view or “1” for full access.
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>]
63 64 65 66 67 68 69 |
# File 'app/models/consent/permission.rb', line 63 def self.from_hash() .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
#action ⇒ Object
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
24 25 26 |
# File 'app/models/consent/permission.rb', line 24 def replaces?() subject == .subject && action == .action end |
#view ⇒ Object
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 |