fedibird-fe/app/models/account_subscribe.rb

40 lines
1 KiB
Ruby
Raw Normal View History

2019-08-30 08:02:41 +00:00
# == Schema Information
#
# Table name: account_subscribes
#
# id :bigint(8) not null, primary key
# account_id :bigint(8)
# target_account_id :bigint(8)
# created_at :datetime not null
# updated_at :datetime not null
# list_id :bigint(8)
#
class AccountSubscribe < ApplicationRecord
2019-12-12 11:02:35 +00:00
include Paginable
include RelationshipCacheable
2019-08-30 08:02:41 +00:00
belongs_to :account
belongs_to :target_account, class_name: 'Account'
belongs_to :list, optional: true
validates :account_id, uniqueness: { scope: [:target_account_id, :list_id] }
scope :recent, -> { reorder(id: :desc) }
scope :subscribed_lists, ->(account) { AccountSubscribe.where(target_account_id: account.id).where.not(list_id: nil).select(:list_id).uniq }
2019-12-12 11:02:35 +00:00
after_create :increment_cache_counters
after_destroy :decrement_cache_counters
private
def increment_cache_counters
account&.increment_count!(:subscribing_count)
end
def decrement_cache_counters
account&.decrement_count!(:subscribing_count)
end
2019-08-30 08:02:41 +00:00
end