File tree Expand file tree Collapse file tree 2 files changed +17
-6
lines changed
Expand file tree Collapse file tree 2 files changed +17
-6
lines changed Original file line number Diff line number Diff 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
7888end
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments