Set default "Untitled" title for published cards

This commit is contained in:
Jorge Manrubia
2025-05-01 13:08:15 +02:00
parent bbda3eb913
commit ca1e002806
2 changed files with 15 additions and 4 deletions
+7 -4
View File
@@ -11,6 +11,8 @@ class Card < ApplicationRecord
has_markdown :description
before_save :set_default_title
scope :reverse_chronologically, -> { order created_at: :desc, id: :desc }
scope :chronologically, -> { order created_at: :asc, id: :asc }
scope :latest, -> { order updated_at: :desc, id: :desc }
@@ -25,11 +27,12 @@ class Card < ApplicationRecord
end
end
def title=(new_title)
self[:title] = new_title.presence || "Untitled"
end
def cache_key
[ super, collection.name ].compact.join("/")
end
private
def set_default_title
self.title = "Untitled" if published? && title.blank?
end
end
+8
View File
@@ -120,4 +120,12 @@ class CardTest < ActiveSupport::TestCase
assert_includes card.cache_key, ApplicationRecord.current_tenant, "cache key must always include the tenant"
end
test "for published cards, it should set the default title 'Untitiled' when not provided" do
card = collections(:writebook).cards.create!
assert_nil card.title
card.publish
assert_equal "Untitled", card.reload.title
end
end