aboutsummaryrefslogtreecommitdiff
path: root/app/models/agency.rb
blob: 03e6e5a51334f29207bab0cdfbc26bb53e120cd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Agency < ApplicationRecord
  include Bitfields

  belongs_to :agent
  belongs_to :user

  bitfield :roles, :observer, :editor, :maintainer, :leader

  class << self
    # Grant role in agent to user
    def grant(agent, user, role)
      r = find_or_create_by(agent: agent, user: user)
      r&.public_send("#{role}=", true) && r&.save
    end

    # Revoke role in agent from user
    def revoke(agent, user, role)
      r = find_by(agent: agent, user: user)
      r&.public_send("#{role}=", false) && r&.save
    end
  end

  # Grant role to current user in current agent
  def grant(role)
    self.class.grant(agent, user, role)
  end

  # Revoke role from current user in current agent
  def revoke(role)
    self.class.revoke(agent, user, role)
  end
end