Skip to content

Commit c002c83

Browse files
committed
Refactor atom article rendering with new Page#body_html method
1 parent f1980f0 commit c002c83

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

app/views/components/atom/entry_content.rb

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,51 @@
88
class Atom::EntryContent
99
include ActionView::Helpers::UrlHelper
1010

11-
# @param article [Sitepress::Model] the article to render for atom feed
12-
def initialize(article, request: nil)
13-
@article = article
11+
# @param page [Page] the Page instance to render for atom feed
12+
def initialize(page, request: nil)
13+
@page = page
1414
@request = request
1515
end
1616

1717
def render
18-
html = render_template_html
18+
html = page.body_html(format: :atom)
1919

2020
doc = Nokogiri::HTML.fragment(html)
2121

2222
doc.css("turbo-frame").each do |frame|
23-
frame.add_next_sibling(link_to("Click here to see content", article_url))
23+
frame_url = base_uri.merge(path: page.request_path, fragment: (frame["id"] if frame["id"])).to_s
24+
frame.add_next_sibling("(" + link_to("Go to the article to see dynamic content", frame_url) + ")")
2425
frame.remove
2526
end
2627

2728
doc.css("img").each do |img|
28-
img["src"] = base_url + img["src"] if img["src"].start_with?("/")
29+
img["src"] = url(img["src"]) if img["src"].start_with?("/")
2930
end
3031

3132
doc.css("a").each do |a|
32-
a["href"] = base_url + a["href"] if a["href"].start_with?("/")
33+
a["href"] = url(a["href"]) if a["href"].start_with?("/")
3334
end
3435

3536
doc.to_html
3637
end
3738

3839
private
3940

40-
attr_reader :article, :request
41+
attr_reader :page, :request
42+
43+
def base_uri
44+
@base_uri ||= Addressable::URI.parse(base_url)
45+
end
46+
47+
def url(path)
48+
base_uri.join(path).to_s
49+
end
4150

4251
def base_url
4352
request&.base_url || ""
4453
end
4554

4655
def article_url
47-
base_url + article.request_path
48-
end
49-
50-
def render_template_html
51-
ApplicationController.render(
52-
inline: article.body,
53-
type: :"mdrb-atom",
54-
layout: false,
55-
content_type: "application/atom+xml",
56-
assigns: {
57-
current_page: article,
58-
format: :atom
59-
}
60-
)
56+
url page.request_path
6157
end
6258
end

spec/views/components/atom/entry_content_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require "rails_helper"
22

33
RSpec.describe Atom::EntryContent do
4-
let(:article) { SitepressArticle.published.find { |a| a.title == "Introducing Joy of Rails" } } # introducing_joy_of_rails.mdrb
4+
let(:article) { FactoryBot.create(:page, request_path: "/articles/introducing-joy-of-rails") }
55

66
describe "#render" do
77
it "renders the article body as html without enhanced markdown" do
@@ -13,7 +13,7 @@
1313
it "rewrites turbo frame content" do
1414
html = described_class.new(article).render
1515
expect(html).not_to include("<turbo-frame")
16-
expect(html).to include("<a href=\"/articles/introducing-joy-of-rails\">Click here to see content</a>")
16+
expect(html).to include("<a href=\"/articles/introducing-joy-of-rails#new_examples_counter\">Go to the article to see dynamic content</a>")
1717
end
1818

1919
it "replaces relative urls with absolute urls" do

0 commit comments

Comments
 (0)