Add models to represent "Collections" (#36977)
This commit is contained in:
1 parent
cfa4f402ef
commit
7ffa5fa0c4
12 files changed
+291
-1
No files matched your search
@@ -0,0 +1,53 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: collections
|
||||
#
|
||||
# id :bigint(8) not null, primary key
|
||||
# description :text not null
|
||||
# discoverable :boolean not null
|
||||
# local :boolean not null
|
||||
# name :string not null
|
||||
# original_number_of_items :integer
|
||||
# sensitive :boolean not null
|
||||
# uri :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# account_id :bigint(8) not null
|
||||
# tag_id :bigint(8)
|
||||
#
|
||||
class Collection < ApplicationRecord
|
||||
MAX_ITEMS = 25
|
||||
|
||||
belongs_to :account
|
||||
belongs_to :tag, optional: true
|
||||
|
||||
has_many :collection_items, dependent: :delete_all
|
||||
|
||||
validates :name, presence: true
|
||||
validates :description, presence: true
|
||||
validates :uri, presence: true, if: :remote?
|
||||
validates :original_number_of_items,
|
||||
presence: true,
|
||||
numericality: { greater_than_or_equal: 0 },
|
||||
if: :remote?
|
||||
validate :tag_is_usable
|
||||
validate :items_do_not_exceed_limit
|
||||
|
||||
def remote?
|
||||
!local?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def tag_is_usable
|
||||
return if tag.blank?
|
||||
|
||||
errors.add(:tag, :unusable) unless tag.usable?
|
||||
end
|
||||
|
||||
def items_do_not_exceed_limit
|
||||
errors.add(:collection_items, :too_many, count: MAX_ITEMS) if collection_items.size > MAX_ITEMS
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,40 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: collection_items
|
||||
#
|
||||
# id :bigint(8) not null, primary key
|
||||
# activity_uri :string
|
||||
# approval_last_verified_at :datetime
|
||||
# approval_uri :string
|
||||
# object_uri :string
|
||||
# position :integer default(1), not null
|
||||
# state :integer default("pending"), not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# account_id :bigint(8)
|
||||
# collection_id :bigint(8) not null
|
||||
#
|
||||
class CollectionItem < ApplicationRecord
|
||||
belongs_to :collection
|
||||
belongs_to :account, optional: true
|
||||
|
||||
enum :state,
|
||||
{ pending: 0, accepted: 1, rejected: 2, revoked: 3 },
|
||||
validate: true
|
||||
|
||||
delegate :local?, :remote?, to: :collection
|
||||
|
||||
validates :position, numericality: { only_integer: true, greater_than: 0 }
|
||||
validates :activity_uri, presence: true, if: :local_item_with_remote_account?
|
||||
validates :approval_uri, absence: true, unless: :local?
|
||||
validates :account, presence: true, if: :accepted?
|
||||
validates :object_uri, presence: true, if: -> { account.nil? }
|
||||
|
||||
scope :ordered, -> { order(position: :asc) }
|
||||
|
||||
def local_item_with_remote_account?
|
||||
local? && account&.remote?
|
||||
end
|
||||
end
|
||||
@@ -13,6 +13,8 @@ module Account::Associations
|
||||
has_many :account_warnings
|
||||
has_many :aliases, class_name: 'AccountAlias'
|
||||
has_many :bookmarks
|
||||
has_many :collections
|
||||
has_many :collection_items
|
||||
has_many :conversations, class_name: 'AccountConversation'
|
||||
has_many :custom_filters
|
||||
has_many :favourites
|
||||
|
||||
Reference in new issue
Block a user