Files
fizzy/app/models/bubble/assignable.rb
T
2024-11-13 13:34:51 -06:00

22 lines
768 B
Ruby

module Bubble::Assignable
extend ActiveSupport::Concern
included do
has_many :assignments, dependent: :delete_all
has_many :assignees, through: :assignments
scope :unassigned, -> { where.missing :assignments }
scope :assigned_to, ->(users) { joins(:assignments).where(assignments: { assignee: users }).distinct }
scope :assigned_by, ->(users) { joins(:assignments).where(assignments: { assigner: users }).distinct }
end
def assign(users, assigner: Current.user)
assignee_rows = Array(users).collect { |user| { assignee_id: user.id, assigner_id: assigner.id, bubble_id: id } }
transaction do
Assignment.insert_all assignee_rows
track_event :assigned, assignee_ids: assignee_rows.pluck(:assignee_id)
end
end
end