aboutsummaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/agency.rb22
-rw-r--r--app/models/agent.rb4
2 files changed, 26 insertions, 0 deletions
diff --git a/app/models/agency.rb b/app/models/agency.rb
new file mode 100644
index 0000000..fe81687
--- /dev/null
+++ b/app/models/agency.rb
@@ -0,0 +1,22 @@
+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
+end
diff --git a/app/models/agent.rb b/app/models/agent.rb
new file mode 100644
index 0000000..bb33c1b
--- /dev/null
+++ b/app/models/agent.rb
@@ -0,0 +1,4 @@
+class Agent < ApplicationRecord
+ has_many :agencies
+ has_many :members, through: :agencies, source: :user
+end