2019-07-02 08:10:25 +01:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: group_accounts
|
|
|
|
#
|
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# group_id :bigint(8) not null
|
|
|
|
# account_id :bigint(8) not null
|
|
|
|
# write_permissions :boolean default(FALSE), not null
|
|
|
|
# role :string
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2019-07-19 23:06:32 +01:00
|
|
|
# unread_count :integer default(0)
|
2019-07-02 08:10:25 +01:00
|
|
|
#
|
|
|
|
|
|
|
|
class GroupAccount < ApplicationRecord
|
|
|
|
enum role: { admin: "admin" }
|
|
|
|
|
|
|
|
belongs_to :group
|
|
|
|
belongs_to :account
|
|
|
|
|
|
|
|
validates :account_id, uniqueness: { scope: :group_id }
|
2019-07-15 15:29:54 +01:00
|
|
|
|
|
|
|
after_commit :remove_relationship_cache
|
2019-07-22 01:46:00 +01:00
|
|
|
after_create :increment_member_count
|
|
|
|
after_destroy :decrement_member_count
|
2019-07-15 15:29:54 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def remove_relationship_cache
|
|
|
|
Rails.cache.delete("relationship:#{account_id}:group#{group_id}")
|
|
|
|
end
|
2019-07-22 01:46:00 +01:00
|
|
|
|
|
|
|
def increment_member_count
|
|
|
|
group&.increment!(:member_count)
|
|
|
|
end
|
|
|
|
|
|
|
|
def decrement_member_count
|
|
|
|
group&.decrement!(:member_count)
|
|
|
|
end
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|