Skip to content

Commit 6a1fe17

Browse files
authored
Merge pull request #2281 from broadinstitute/jb-feature-announcement-slug
Use latest published date for feature announcement slug (SCP-5759)
2 parents cf6c193 + 49c6f84 commit 6a1fe17

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

app/models/feature_announcement.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,19 @@ def record_latest_event
7070
private
7171

7272
def set_slug
73-
if new_record? || title_changed? && !published
74-
today = created_at.nil? ? Time.zone.today : created_at
75-
self.slug = "#{today.strftime('%F')}-#{title.downcase.gsub(/[^a-zA-Z0-9]+/, '-').chomp('-')}"
73+
if published
74+
published_date = history[:published] || Date.today
75+
# determine if we have an existing title slug we need to preserve
76+
# this prevents published links from breaking when the title changes
77+
# NOTE: unpublishing a feature will always break any published links
78+
if new_record? || slug&.starts_with?('unpublished-')
79+
title_entry = title.downcase.gsub(/[^a-zA-Z0-9]+/, '-')
80+
else
81+
title_entry = slug.split('-')[3..].join('-')
82+
end
83+
self.slug = "#{published_date.strftime('%F')}-#{title_entry.chomp('-')}"
84+
else
85+
self.slug = "unpublished-#{id}"
7686
end
7787
end
7888
end

test/models/feature_announcement_test.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@ class FeatureAnnouncementTest< ActiveSupport::TestCase
2121
FeatureAnnouncement.update_all(published: true, archived: false)
2222
end
2323

24-
test 'should set slug on save until published' do
24+
test 'should set slug on save when published' do
2525
feature = FeatureAnnouncement.create(
2626
title: 'Unpublished Feature',
2727
content: '<p>This is the content.</p>',
2828
doc_link: 'https://singlecell.zendesk.com/hc/en-us',
29+
created_at: Date.today.in_time_zone - 1.day,
2930
published: false,
3031
archived: false
3132
)
32-
assert_equal "#{@today}-unpublished-feature", feature.slug
33+
assert_equal "unpublished-#{feature.id}", feature.slug
3334
feature.update(title: 'Announcing a New Feature!!')
3435
feature.reload
35-
assert_equal "#{@today}-announcing-a-new-feature", feature.slug
36+
assert_equal "unpublished-#{feature.id}", feature.slug
3637
feature.update(published: true)
3738
feature.reload
3839
assert_equal "#{@today}-announcing-a-new-feature", feature.slug

0 commit comments

Comments
 (0)