list.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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
  19. has_many :list_accounts, inverse_of: :list, dependent: :destroy
  20. has_many :accounts, through: :list_accounts
  21. validates :title, presence: true
  22. validate :validate_account_lists_limit, on: :create
  23. before_destroy :clean_feed_manager
  24. private
  25. def validate_account_lists_limit
  26. errors.add(:base, I18n.t('lists.errors.limit')) if account.owned_lists.count >= PER_ACCOUNT_LIMIT
  27. end
  28. def clean_feed_manager
  29. FeedManager.instance.clean_feeds!(:list, [id])
  30. end
  31. end