Skip to content

Commit

Permalink
Refactoring: Reduce usages of sti_name to minimum
Browse files Browse the repository at this point in the history
sti_names methods don't seem to be used.
within Edition::Identifiable:
- When edition is saved, the edition type should be propogated to the document type. Currently it is using sti_name, but Edition also has a type method. We could be using this instead.
- The published as class method is used to fetch document by slug based on the type of the subclass. So unfortunately we still need to use sti_name for this. The test however has 1 duplicate unecessary test, as we not only support News and Speeches sharing same slug, but all other document types too, which already has another test above covering that.
  • Loading branch information
minhngocd committed Dec 17, 2024
1 parent b4a347f commit 488da0f
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 31 deletions.
4 changes: 0 additions & 4 deletions app/models/announcement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ class Announcement < Edition
include Edition::WorldLocations
include Edition::TopicalEvents

def self.sti_names
([self] + descendants).map(&:sti_name)
end

def self.published_with_eager_loading(ids)
published.with_translations.includes([:document, { organisations: :translations }]).where(id: ids)
end
Expand Down
20 changes: 7 additions & 13 deletions app/models/concerns/edition/identifiable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,16 @@ def update_document_slug
end

def propagate_type_to_document
document.document_type = self.class.document_type if document
document.document_type = type if document
end

module ClassMethods
def document_type
sti_name
end

def published_as(slug, locale = I18n.default_locale)
document = Document.at_slug(document_type, slug)
if document.present?
live_edition = document.live_edition
return live_edition if live_edition.present? && live_edition.available_in_locale?(locale)
end
nil
def self.published_as(slug, locale = I18n.default_locale)
document = Document.at_slug(sti_name, slug)
if document.present?
live_edition = document.live_edition
return live_edition if live_edition.present? && live_edition.available_in_locale?(locale)
end
nil
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/edition/publishing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def by_major_change_published_at

def unpublished_as(slug)
document = Document.at_slug(document_type, slug)
document && document.latest_edition && document.latest_edition.unpublishing
document&.latest_edition&.unpublishing
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/edition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def self.concrete_descendant_search_format_types

def self.scheduled_for_publication_as(slug)
document = Document.at_slug(document_type, slug)
document && document.scheduled_edition
document&.scheduled_edition
end

def skip_main_validation?
Expand Down
4 changes: 0 additions & 4 deletions app/models/publicationesque.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class Publicationesque < Edition

include ::Attachable

def self.sti_names
([self] + descendants).map(&:sti_name)
end

def self.published_with_eager_loading(ids)
published.with_translations.includes([:document, { organisations: :translations }]).where(id: ids)
end
Expand Down
8 changes: 0 additions & 8 deletions test/unit/app/models/edition/identifiable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ class Edition::IdentifiableTest < ActiveSupport::TestCase
assert_equal publication.document.slug, news_article.document.slug
end

test "should allow the same slug to be used for a news article and a speech" do
same_title = "same-title"
news_article = create(:news_article, title: same_title)
speech = create(:speech, title: same_title)

assert_equal news_article.document.slug, speech.document.slug
end

test "should return the edition of the correct type when matching slugs for other types exist" do
same_title = "same-title"
news_article = create(:published_news_article, title: same_title)
Expand Down

0 comments on commit 488da0f

Please sign in to comment.