diff --git a/app/models/mainstream_browse_page.rb b/app/models/mainstream_browse_page.rb index cb4ebb510..c98d2cef7 100644 --- a/app/models/mainstream_browse_page.rb +++ b/app/models/mainstream_browse_page.rb @@ -19,6 +19,7 @@ def self.find(base_path) def initialize(content_item) @content_item = content_item + @documents_service = PopularBrowseSearchDocuments end def top_level_browse_pages @@ -49,7 +50,24 @@ def lists @lists ||= ListSet.new("section", @content_item.content_id, details["groups"]) end - def slug - base_path.sub(%r{\A/browse/}, "") + def slug(path = base_path) + path.sub(%r{\A/browse/}, "") + end + + def display_popular_tasks? + %w[benefits business].include?(slug) + end + + def popular_tasks + return if second_level_browse_pages.blank? + + slugs = second_level_browse_pages.map { |page| slug(page.base_path) } + service_pages_tagged_to_browse_subtopics(slugs) + end + + def service_pages_tagged_to_browse_subtopics(subtopic_slugs) + @documents_service.new( + subtopic_slugs, "filter_any_mainstream_browse_pages" + ).fetch_related_documents_with_format({ filter_format: "transaction" }) end end diff --git a/app/models/topical_event.rb b/app/models/topical_event.rb index 7b735567c..df6359eb7 100644 --- a/app/models/topical_event.rb +++ b/app/models/topical_event.rb @@ -3,7 +3,7 @@ class TopicalEvent def initialize(content_item) @content_item = content_item - @documents_service = SearchDocuments.new(slug, "filter_topical_events") + @documents_service = TopicalEventSearchDocuments end def self.find!(base_path) @@ -92,24 +92,28 @@ def ordered_featured_documents end end + def documents_service + @documents_service.new([slug], "filter_topical_events") + end + def latest - @latest ||= @documents_service.fetch_related_documents_with_format + @latest ||= documents_service.fetch_related_documents_with_format end def publications - @publications ||= @documents_service.fetch_related_documents_with_format({ filter_format: "publication" }) + @publications ||= documents_service.fetch_related_documents_with_format({ filter_format: "publication" }) end def consultations - @consultations ||= @documents_service.fetch_related_documents_with_format({ filter_format: "consultation" }) + @consultations ||= documents_service.fetch_related_documents_with_format({ filter_format: "consultation" }) end def announcements - @announcements ||= @documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: "news_and_communications", reject_content_purpose_subgroup: %w[decisions updates_and_alerts] }) + @announcements ||= documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: "news_and_communications", reject_content_purpose_subgroup: %w[decisions updates_and_alerts] }) end def guidance_and_regulation - @guidance_and_regulation ||= @documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: "guidance_and_regulation" }) + @guidance_and_regulation ||= documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: "guidance_and_regulation" }) end def organisations diff --git a/app/models/world_location_news.rb b/app/models/world_location_news.rb index d9bd5de21..ed25fbc9c 100644 --- a/app/models/world_location_news.rb +++ b/app/models/world_location_news.rb @@ -3,7 +3,7 @@ class WorldLocationNews def initialize(content_item) @content_item = content_item - @documents_service = SearchDocuments.new(slug, "filter_world_locations") + @documents_service = TopicalEventSearchDocuments end def self.find!(base_path) @@ -70,19 +70,23 @@ def latest @latest ||= [*announcements, *publications, *statistics] .select { |entry| entry.dig(:metadata, :public_updated_at).present? } .sort { |entry1, entry2| - (entry1.dig(:metadata, :public_updated_at) <=> entry2.dig(:metadata, :public_updated_at)) } - .first(SearchDocuments::DEFAULT_COUNT) + .first(@documents_service::DEFAULT_COUNT) + end + + def documents_service + @documents_service.new([slug], "filter_world_locations") end def announcements - @announcements ||= @documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: "news_and_communications" }) + @announcements ||= documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: "news_and_communications" }) end def publications - @publications ||= @documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: %w[guidance_and_regulation policy_and_engagement transparency] }) + @publications ||= documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: %w[guidance_and_regulation policy_and_engagement transparency] }) end def statistics - @statistics ||= @documents_service.fetch_related_documents_with_format({ filter_content_purpose_subgroup: "statistics" }) + @statistics ||= documents_service.fetch_related_documents_with_format({ filter_content_purpose_subgroup: "statistics" }) end def type diff --git a/app/services/popular_browse_search_documents.rb b/app/services/popular_browse_search_documents.rb new file mode 100644 index 000000000..0230edb89 --- /dev/null +++ b/app/services/popular_browse_search_documents.rb @@ -0,0 +1,9 @@ +class PopularBrowseSearchDocuments < SearchDocuments + def fields + TOPICAL_EVENTS_SEARCH_FIELDS + end + + def order + "-popularity" + end +end diff --git a/app/services/search_documents.rb b/app/services/search_documents.rb index 64515ee92..72d3d08c6 100644 --- a/app/services/search_documents.rb +++ b/app/services/search_documents.rb @@ -2,23 +2,31 @@ class SearchDocuments include SearchApiFields DEFAULT_COUNT = 3 - def initialize(slug, filter_field) - @slug = slug + def initialize(slug_array, filter_field) + @slug_array = slug_array @filter_field = filter_field end def fetch_related_documents_with_format(filter_format = {}) - search_response = Services.search_api.search(default_search_options.merge(filter_format)) + search_response = Services.cached_search(default_search_options.merge(filter_format)) format_results(search_response) end + def fields + Raise NotImplementedError + end + + def order + Raise NotImplementedError + end + private def default_search_options - { @filter_field.to_sym => [@slug], + { @filter_field.to_sym => @slug_array, count: DEFAULT_COUNT, - order: "-public_timestamp", - fields: TOPICAL_EVENTS_SEARCH_FIELDS } + order:, + fields: } end def display_type(document) diff --git a/app/services/topical_event_search_documents.rb b/app/services/topical_event_search_documents.rb new file mode 100644 index 000000000..0d92c5f89 --- /dev/null +++ b/app/services/topical_event_search_documents.rb @@ -0,0 +1,9 @@ +class TopicalEventSearchDocuments < SearchDocuments + def fields + TOPICAL_EVENTS_SEARCH_FIELDS + end + + def order + "-public_timestamp" + end +end diff --git a/app/views/browse/show.html.erb b/app/views/browse/show.html.erb index 4031ffb37..d647312ca 100644 --- a/app/views/browse/show.html.erb +++ b/app/views/browse/show.html.erb @@ -31,7 +31,7 @@ } %> <% end %> -<% if display_action_links_for_slug?(page.slug) %> +<% if page.display_popular_tasks? %>