list.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: lists
  5. #
  6. # id :bigint(8) not null, primary key
  7. # account_id :bigint(8) not null
  8. # title :string default(""), not null
  9. # created_at :datetime not null
  10. # updated_at :datetime not null
  11. # replies_policy :integer default("list"), not null
  12. # exclusive :boolean default(FALSE), not null
  13. #
  14. class List < ApplicationRecord
  15. include Paginable
  16. PER_ACCOUNT_LIMIT = 50
  17. enum replies_policy: { list: 0, followed: 1, none: 2 }, _prefix: :show
  18. belongs_to :account, optional: true
  19. has_many :list_accounts, inverse_of: :list, dependent: :destroy
  20. has_many :accounts, through: :list_accounts
  21. validates :title, presence: true
  22. validates_each :account_id, on: :create do |record, _attr, value|
  23. record.errors.add(:base, I18n.t('lists.errors.limit')) if List.where(account_id: value).count >= PER_ACCOUNT_LIMIT
  24. end
  25. before_destroy :clean_feed_manager
  26. private
  27. def clean_feed_manager
  28. FeedManager.instance.clean_feeds!(:list, [id])
  29. end
  30. end