From 3b6d94ce6247b5a4c5546bce2831cf5eefe1ea71 Mon Sep 17 00:00:00 2001 From: David Roetzel Date: Tue, 10 Mar 2026 15:54:38 +0100 Subject: [PATCH] Add column for html descriptions of collections (#38124) --- app/models/collection.rb | 8 ++++++-- app/serializers/rest/collection_serializer.rb | 4 ++++ ...310095021_add_description_html_to_collections.rb | 13 +++++++++++++ db/schema.rb | 5 +++-- spec/fabricators/collection_fabricator.rb | 2 ++ spec/models/collection_spec.rb | 4 ++++ spec/serializers/rest/collection_serializer_spec.rb | 9 +++++++++ 7 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20260310095021_add_description_html_to_collections.rb diff --git a/app/models/collection.rb b/app/models/collection.rb index 3b8ee82a3c..0061e7ff5c 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -5,7 +5,8 @@ # Table name: collections # # id :bigint(8) not null, primary key -# description :text not null +# description :text +# description_html :text # discoverable :boolean not null # item_count :integer default(0), not null # language :string @@ -30,7 +31,10 @@ class Collection < ApplicationRecord has_many :collection_reports, dependent: :delete_all validates :name, presence: true - validates :description, presence: true + validates :description, presence: true, + if: :local? + validates :description_html, presence: true, + if: :remote? validates :local, inclusion: [true, false] validates :sensitive, inclusion: [true, false] validates :discoverable, inclusion: [true, false] diff --git a/app/serializers/rest/collection_serializer.rb b/app/serializers/rest/collection_serializer.rb index 9296a5cf4a..370384c220 100644 --- a/app/serializers/rest/collection_serializer.rb +++ b/app/serializers/rest/collection_serializer.rb @@ -13,6 +13,10 @@ class REST::CollectionSerializer < ActiveModel::Serializer object.id.to_s end + def description + object.local? ? object.description : object.description_html + end + def items object.items_for(current_user&.account) end diff --git a/db/migrate/20260310095021_add_description_html_to_collections.rb b/db/migrate/20260310095021_add_description_html_to_collections.rb new file mode 100644 index 0000000000..ef6a9aaecf --- /dev/null +++ b/db/migrate/20260310095021_add_description_html_to_collections.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class AddDescriptionHtmlToCollections < ActiveRecord::Migration[8.1] + def change + add_column :collections, :description_html, :text + + reversible do |direction| + direction.up { change_column :collections, :description, :text, null: true } + + direction.down { change_column :collections, :description, :text, null: false } + end + end +end diff --git a/db/schema.rb b/db/schema.rb index f1dddcbe26..d01d1af500 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.1].define(version: 2026_03_03_144409) do +ActiveRecord::Schema[8.1].define(version: 2026_03_10_095021) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -387,7 +387,8 @@ ActiveRecord::Schema[8.1].define(version: 2026_03_03_144409) do create_table "collections", id: :bigint, default: -> { "timestamp_id('collections'::text)" }, force: :cascade do |t| t.bigint "account_id", null: false t.datetime "created_at", null: false - t.text "description", null: false + t.text "description" + t.text "description_html" t.boolean "discoverable", null: false t.integer "item_count", default: 0, null: false t.string "language" diff --git a/spec/fabricators/collection_fabricator.rb b/spec/fabricators/collection_fabricator.rb index 7e0e14a765..cbce4bd478 100644 --- a/spec/fabricators/collection_fabricator.rb +++ b/spec/fabricators/collection_fabricator.rb @@ -12,6 +12,8 @@ end Fabricator(:remote_collection, from: :collection) do account { Fabricate.build(:remote_account) } local false + description nil + description_html '

People to follow

' uri { sequence(:uri) { |i| "https://example.com/collections/#{i}" } } original_number_of_items 0 end diff --git a/spec/models/collection_spec.rb b/spec/models/collection_spec.rb index ba1819fa6c..fc833d354b 100644 --- a/spec/models/collection_spec.rb +++ b/spec/models/collection_spec.rb @@ -23,6 +23,10 @@ RSpec.describe Collection do context 'when collection is remote' do subject { Fabricate.build :collection, local: false } + it { is_expected.to_not validate_presence_of(:description) } + + it { is_expected.to validate_presence_of(:description_html) } + it { is_expected.to validate_presence_of(:uri) } it { is_expected.to validate_presence_of(:original_number_of_items) } diff --git a/spec/serializers/rest/collection_serializer_spec.rb b/spec/serializers/rest/collection_serializer_spec.rb index 0fbe955b2e..816b1873f6 100644 --- a/spec/serializers/rest/collection_serializer_spec.rb +++ b/spec/serializers/rest/collection_serializer_spec.rb @@ -43,4 +43,13 @@ RSpec.describe REST::CollectionSerializer do 'items' => [] ) end + + context 'when the collection is remote' do + let(:collection) { Fabricate(:remote_collection, description_html: '

remote

') } + + it 'includes the html description' do + expect(subject) + .to include('description' => '

remote

') + end + end end