Class: Adeia::Permission

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/adeia/permission.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add!(**args) ⇒ Object



36
37
38
# File 'app/models/adeia/permission.rb', line 36

def self.add!(**args)
  self.create!(**conditions(args))
end

.conditions(args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/adeia/permission.rb', line 55

def self.conditions(args)
  args[:read] = args[:create] = args[:update] = args[:destroy] = true if args[:full]
  element, actions = fetch_element_and_actions!(args.fetch(:element), args.fetch(:actions, []))
  return {
    owner: args.fetch(:owner),
    element: element,
    permission_type: self.permission_types[args.fetch(:type, "all_entries")],
    read_right: args[:read],
    create_right: args[:create],
    update_right: args[:update],
    destroy_right: args[:destroy],
    resource_id: args[:resource_id],
    actions: actions
  }.reject { |k, v| v.blank? }
end

.fetch_element_and_actions!(element_name, action_names) ⇒ Object



49
50
51
52
53
# File 'app/models/adeia/permission.rb', line 49

def self.fetch_element_and_actions!(element_name, action_names)
  actions = action_names.map { |action| Adeia::Action.find_or_create_by!(name: action) }.compact
  element = Adeia::Element.find_or_create_by!(name: element_name)
  return element, actions
end

.find_or_add_by!(**args) ⇒ Object



40
41
42
43
44
45
46
47
# File 'app/models/adeia/permission.rb', line 40

def self.find_or_add_by!(**args)
  conditions = conditions(args)
  if permission = self.where(**conditions).first
    permission
  else
    self.create!(**conditions(args))
  end
end

Instance Method Details

#autosave_associated_records_for_actionsObject



26
27
28
29
30
# File 'app/models/adeia/permission.rb', line 26

def autosave_associated_records_for_actions
  self.actions = actions.reject{ |a| a._destroy == true }.map do |action|
    Adeia::Action.find_or_create_by(name: action.name)
  end
end

#full_nameObject



32
33
34
# File 'app/models/adeia/permission.rb', line 32

def full_name
  "#{id} - #{element.name} - R:#{read_right} - C:#{create_right}- U:#{update_right} - D:#{destroy_right} - #{resource_id} - #{actions.to_a}"
end

#global_ownerObject



18
19
20
# File 'app/models/adeia/permission.rb', line 18

def global_owner
  self.owner.to_global_id if self.owner.present?
end

#global_owner=(owner) ⇒ Object



22
23
24
# File 'app/models/adeia/permission.rb', line 22

def global_owner=(owner)
  self.owner = GlobalID::Locator.locate owner
end