mastodon/app/models/collection.rb
David Roetzel 5a7a4fff11
Some checks are pending
Check i18n / check-i18n (push) Waiting to run
Chromatic / Check for relevant changes (push) Waiting to run
Chromatic / Run Chromatic (push) Blocked by required conditions
CodeQL / Analyze (actions) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
CodeQL / Analyze (ruby) (push) Waiting to run
Check formatting / lint (push) Waiting to run
CSS Linting / lint (push) Waiting to run
JavaScript Linting / lint (push) Waiting to run
Ruby Linting / lint (push) Waiting to run
JavaScript Testing / test (push) Waiting to run
Historical data migration test / test (14-alpine) (push) Waiting to run
Historical data migration test / test (15-alpine) (push) Waiting to run
Historical data migration test / test (16-alpine) (push) Waiting to run
Historical data migration test / test (17-alpine) (push) Waiting to run
Ruby Testing / build (production) (push) Waiting to run
Ruby Testing / build (test) (push) Waiting to run
Ruby Testing / test (.ruby-version) (push) Blocked by required conditions
Ruby Testing / test (3.2) (push) Blocked by required conditions
Ruby Testing / test (3.3) (push) Blocked by required conditions
Ruby Testing / ImageMagick tests (.ruby-version) (push) Blocked by required conditions
Ruby Testing / ImageMagick tests (3.2) (push) Blocked by required conditions
Ruby Testing / ImageMagick tests (3.3) (push) Blocked by required conditions
Ruby Testing / End to End testing (.ruby-version) (push) Blocked by required conditions
Ruby Testing / End to End testing (3.2) (push) Blocked by required conditions
Ruby Testing / End to End testing (3.3) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:8.10.2) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, opensearchproject/opensearch:2) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (3.2, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (3.3, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Blocked by required conditions
First draft of Collection update API (#37110)
2025-12-04 10:00:21 +00:00

73 lines
2.0 KiB
Ruby

# 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 :local, inclusion: [true, false]
validates :sensitive, inclusion: [true, false]
validates :discoverable, inclusion: [true, false]
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
scope :with_items, -> { includes(:collection_items).merge(CollectionItem.with_accounts) }
def remote?
!local?
end
def items_for(account = nil)
result = collection_items.with_accounts
result = result.not_blocked_by(account) unless account.nil?
result
end
def tag_name
tag&.formatted_name
end
def tag_name=(new_name)
self.tag = Tag.find_or_create_by_names(new_name).first
end
private
def tag_is_usable
return if tag.blank?
errors.add(:tag_name, :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