Skip to content

Commit 3586958

Browse files
committed
XML: group by published_at with year->month nesting; dedupe providers in AllProviders; use rails_blob_path in TopicsHelper
1 parent a3cbdd9 commit 3586958

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

app/helpers/topics_helper.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@ def card_preview_media(file)
1212
private
1313

1414
def render_image(file)
15-
image_tag(url_for(file), class: "img-fluid w-100")
15+
image_tag(rails_blob_path(file, disposition: "inline"), class: "img-fluid w-100")
1616
end
1717

1818
def render_pdf(file)
1919
content_tag(:div, class: "embed-responsive embed-responsive-item embed-responsive-16by9 w-100") do
20-
content_tag(:object, data: url_for(file), type: "application/pdf", width: "100%", height: "400px") do
21-
content_tag(:iframe, "", src: url_for(file), width: "100%", height: "100%", style: "border: none;") do
22-
content_tag(:p, "Your browser does not support PDF viewing. #{link_to('Download the PDF', url_for(file))}")
20+
content_tag(:object, data: rails_blob_path(file, disposition: "inline"), type: "application/pdf", width: "100%", height: "400px") do
21+
content_tag(:iframe, "", src: rails_blob_path(file, disposition: "inline"), width: "100%", height: "100%", style: "border: none;") do
22+
content_tag(:p, "Your browser does not support PDF viewing. #{link_to('Download the PDF', rails_blob_path(file))}")
2323
end
2424
end
2525
end
2626
end
2727

2828
def render_video(file)
29-
video_tag(url_for(file), style: "width: 100%")
29+
video_tag(rails_blob_path(file, disposition: "inline"), style: "width: 100%")
3030
end
3131

3232
def render_audio(file)
33-
audio_tag(url_for(file), controls: true, style: "width: 100%")
33+
audio_tag(rails_blob_path(file, disposition: "inline"), controls: true, style: "width: 100%")
3434
end
3535

3636
def render_download_link(file)
37-
link_to file.filename, url_for(file)
37+
link_to file.filename, rails_blob_path(file)
3838
end
3939
end

app/services/xml_generator/single_provider.rb

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,30 @@ def provider_xml(provider)
1616
Ox::Element.new("content_provider").tap do |xml|
1717
xml[:name] = provider.name
1818

19-
grouped_topics(provider).each do |(year, month), topics|
19+
grouped_topics(provider).sort_by { |year, _| -year.to_i }.each do |(year, months)|
2020
xml << Ox::Element.new("topic_year").tap do |year_element|
2121
year_element[:year] = year.to_s
22-
year_element << Ox::Element.new("topic_month").tap do |month_element|
23-
month_element[:month] = month
24-
topics.each do |topic|
25-
month_element << Ox::Element.new("title").tap do |title_element|
26-
title_element[:name] = topic.title
27-
title_element << Ox::Element.new("topic_id").tap { |id| id << topic.id.to_s }
28-
title_element << Ox::Element.new("topic_files").tap do |files|
29-
files[:files] = "Files"
30-
topic.documents.each_with_index do |document, index|
31-
next if document.content_type == "video/mp4"
32-
files << Ox::Element.new("file_name_#{index + 1}").tap do |file_name|
33-
file_name << document.filename.to_s
34-
file_name[:file_size] = document.byte_size
22+
months.sort_by { |month_label, _| month_label }.each do |(month, topics)|
23+
year_element << Ox::Element.new("topic_month").tap do |month_element|
24+
month_element[:month] = month
25+
topics.each do |topic|
26+
month_element << Ox::Element.new("title").tap do |title_element|
27+
title_element[:name] = topic.title
28+
title_element << Ox::Element.new("topic_id").tap { |id| id << topic.id.to_s }
29+
title_element << Ox::Element.new("topic_files").tap do |files|
30+
files[:files] = "Files"
31+
topic.documents.each_with_index do |document, index|
32+
next if document.content_type == "video/mp4"
33+
files << Ox::Element.new("file_name_#{index + 1}").tap do |file_name|
34+
file_name << document.filename.to_s
35+
file_name[:file_size] = document.byte_size
36+
end
3537
end
3638
end
37-
end
38-
title_element << Ox::Element.new("topic_tags").tap do |tags|
39-
names = topic.taggings.map { |tg| tg.tag&.name }.compact.uniq
40-
tags << names.join(", ")
39+
title_element << Ox::Element.new("topic_tags").tap do |tags|
40+
names = topic.taggings.map { |tg| tg.tag&.name }.compact.uniq
41+
tags << names.join(", ")
42+
end
4143
end
4244
end
4345
end
@@ -48,16 +50,19 @@ def provider_xml(provider)
4850
end
4951

5052
def grouped_topics(prov)
51-
topic_scope(prov).group_by { |topic| [ topic.published_at.year, topic.published_at.strftime("%m_%B") ] }
53+
# { year => { "MM_Month" => [topics] } }
54+
topic_scope(prov)
55+
.group_by { |topic| topic.published_at.year }
56+
.transform_values { |topics_in_year| topics_in_year.group_by { |t| t.published_at.strftime("%m_%B") } }
5257
end
5358

5459
def topic_scope(prov)
5560
scope = prov.topics
5661
scope = scope.where("published_at > ?", 1.month.ago) if args.fetch(:recent, false)
5762
scope
5863
.select(:id, :title, :published_at, :language_id, :provider_id)
59-
.includes(:language, taggings: :tag) # eager-load language and taggings->tag
60-
.with_attached_documents # eager-load Active Storage attachments + blobs
64+
.includes(:language, taggings: :tag)
65+
.with_attached_documents
6166
.order(published_at: :desc)
6267
end
6368
end

0 commit comments

Comments
 (0)