diff --git a/app/models/document.rb b/app/models/document.rb index ed5d81df980..9f3e1b55ff6 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -124,6 +124,10 @@ def title_with_state "#{title} (#{state})" end + def sluggable_title + title + end + class << self def authored_by(user) joins(:document_authors).where(document_authors: {user_id: user}).group(:document_id) diff --git a/app/models/document/identifiable.rb b/app/models/document/identifiable.rb index 44705bf0251..f2090659148 100644 --- a/app/models/document/identifiable.rb +++ b/app/models/document/identifiable.rb @@ -10,11 +10,11 @@ module Document::Identifiable end def set_document_identity - self.document_identity ||= DocumentIdentity.new(sluggable_string: self.title) + self.document_identity ||= DocumentIdentity.new(sluggable_string: self.sluggable_title) end def update_document_identity_slug - self.document_identity.update_slug_if_possible(self.title) + self.document_identity.update_slug_if_possible(self.sluggable_title) end def set_document_type_on_document_identity diff --git a/app/models/policy.rb b/app/models/policy.rb index 8c8d1e8ede1..8070fdbd005 100644 --- a/app/models/policy.rb +++ b/app/models/policy.rb @@ -20,8 +20,16 @@ def process_associations_after_save(document) scope :stub, where(stub: true) - def title - original_title = super + define_attribute_methods + + def title_with_stub + original_title = title_without_stub stub? ? "[Sample] #{original_title}" : original_title end + alias_method_chain :title, :stub + + def sluggable_title + title_without_stub + end + end \ No newline at end of file diff --git a/test/unit/policy_test.rb b/test/unit/policy_test.rb index ae75df23efb..66ff68892ac 100644 --- a/test/unit/policy_test.rb +++ b/test/unit/policy_test.rb @@ -33,7 +33,11 @@ class PolicyTest < ActiveSupport::TestCase assert_equal [policy_area_1, policy_area_2], policy.policy_areas.reload end - test "should prepend stub signifier to stub policy titles" do + test "prepends stub signifier to stub policy titles" do assert_equal "[Sample] stub title", build(:policy, title: "stub title", stub: true).title end + + test "stub status doesn't affect slug generation" do + assert_equal "stub-title", create(:policy, title: "stub title", stub: true).document_identity.slug + end end \ No newline at end of file