Gab-Social/app/models/group_account.rb
2020-11-15 12:48:32 -06:00

45 lines
1.1 KiB
Ruby

# == 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
#
class GroupAccount < ApplicationRecord
self.ignored_columns = ["unread_count"]
# : todo : enum: 1,2,3,4,5...
enum role: {
admin: "admin",
moderator: "moderator"
}
belongs_to :group
belongs_to :account
validates :account_id, uniqueness: { scope: :group_id }
after_commit :remove_relationship_cache
after_create :increment_member_count
after_destroy :decrement_member_count
private
def remove_relationship_cache
Rails.cache.delete("relationship:#{account_id}:group#{group_id}")
end
def increment_member_count
group&.increment!(:member_count)
end
def decrement_member_count
group&.decrement!(:member_count)
end
end