Skip to content

Commit 7c8e9f6

Browse files
Merge pull request #271 from jonathanhefner/consolidate-project_version-badge_version
Consolidate `project_version` and `badge_version`
2 parents ab67c57 + c76d02e commit 7c8e9f6

File tree

10 files changed

+55
-56
lines changed

10 files changed

+55
-56
lines changed

Rakefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class RailsTask < Rails::API::EdgeTask
5454

5555
def setup_horo_variables
5656
super
57+
58+
ENV["HORO_BADGE_VERSION"] ||= "edge" if ENV["HORO_PROJECT_VERSION"]&.include?("@")
59+
5760
if ENV['NETLIFY']
5861
ENV['HORO_CANONICAL_URL'] = ENV.fetch('DEPLOY_PRIME_URL', 'https://edgeapi.rubyonrails.org')
5962
end

lib/rdoc/generator/template/rails/class.rhtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<% if project_name %>
2323
<div>
2424
<%= project_name %>
25-
<span title="<%= project_git_head %>"><%= badge_version || project_version %></span>
25+
<span title="<%= project_git_head %>"><%= project_version %></span>
2626
</div>
2727
<% end %>
2828

@@ -40,8 +40,8 @@
4040
<% end %>
4141
</h2>
4242

43-
<% if badge_version %>
44-
<div id="version-badge"><%= badge_version %></div>
43+
<% if project_version %>
44+
<div id="version-badge"><%= project_version %></div>
4545
<% end %>
4646
</div>
4747

lib/rdoc/generator/template/rails/file.rhtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<% if project_name %>
2020
<div>
2121
<%= project_name %>
22-
<span title="<%= project_git_head %>"><%= badge_version || project_version %></span>
22+
<span title="<%= project_git_head %>"><%= project_version %></span>
2323
</div>
2424
<% end %>
2525

@@ -36,8 +36,8 @@
3636
<li>Last modified: <%= file.file_stat.mtime %></li>
3737
</ul>
3838

39-
<% if badge_version %>
40-
<div id="version-badge"><%= badge_version %></div>
39+
<% if project_version %>
40+
<div id="version-badge"><%= project_version %></div>
4141
<% end %>
4242
</div>
4343

lib/rdoc/generator/template/rails/index.rhtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<% if project_name %>
1818
<div>
1919
<%= project_name %>
20-
<span title="<%= project_git_head %>"><%= badge_version || project_version %></span>
20+
<span title="<%= project_git_head %>"><%= project_version %></span>
2121
</div>
2222
<% end %>
2323

@@ -28,8 +28,8 @@
2828
<li><%= h index.relative_name %></li>
2929
<li>Last modified: <%= index.last_modified %></li>
3030
</ul>
31-
<% if badge_version %>
32-
<div id="version-badge"><%= badge_version %></div>
31+
<% if project_version %>
32+
<div id="version-badge"><%= project_version %></div>
3333
<% end %>
3434
</div>
3535

lib/sdoc/generator.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ def self.setup_options(options)
7272
exit
7373
end
7474

75-
options.title = [ENV["HORO_PROJECT_NAME"], ENV["HORO_BADGE_VERSION"], "API documentation"].compact.join(" ")
75+
options.title = [
76+
ENV["HORO_PROJECT_NAME"],
77+
ENV["HORO_BADGE_VERSION"] || ENV["HORO_PROJECT_VERSION"],
78+
"API documentation"
79+
].compact.join(" ")
7680
end
7781

7882
def initialize(store, options)

lib/sdoc/helpers.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ def project_name
3434
end
3535

3636
def project_version
37-
h(ENV["HORO_PROJECT_VERSION"]) if ENV["HORO_PROJECT_VERSION"]
38-
end
39-
40-
def badge_version
41-
h(ENV["HORO_BADGE_VERSION"]) if ENV["HORO_BADGE_VERSION"]
37+
version = ENV["HORO_BADGE_VERSION"] || ENV["HORO_PROJECT_VERSION"]
38+
h version if version
4239
end
4340

4441
def project_git_head
@@ -50,7 +47,7 @@ def page_title(title = nil)
5047
end
5148

5249
def og_title(title)
53-
project = [project_name, badge_version].join(" ").strip
50+
project = [project_name, project_version].join(" ").strip
5451
"#{h title}#{" (#{project})" unless project.empty?}"
5552
end
5653

lib/sdoc/postprocessor.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def version_url(url, version)
6161
uri = URI(url)
6262

6363
unless uri.path.match?(%r"\A/v\d")
64-
if version.match?(/\A[.0-9]+\z/)
65-
uri.path = "/v#{version}#{uri.path}"
64+
if version.match?(/\Av?[.0-9]+\z/)
65+
uri.path = "/#{version.sub(/\Av?/, "v")}#{uri.path}"
6666
else
6767
uri.host = "edge#{uri.host}"
6868
end

spec/helpers_spec.rb

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -208,23 +208,15 @@ module Foo; module Bar; module Qux; end; end; end
208208
end
209209
end
210210

211-
it "returns nil when ENV['HORO_PROJECT_VERSION'] is not set" do
212-
with_env("HORO_PROJECT_VERSION" => nil) do
213-
_(@helpers.project_version).must_be_nil
214-
end
215-
end
216-
end
217-
218-
describe "#badge_version" do
219-
it "returns escaped version from ENV['HORO_BADGE_VERSION']" do
220-
with_env("HORO_BADGE_VERSION" => "~> 1.0.0") do
221-
_(@helpers.badge_version).must_equal "~&gt; 1.0.0"
211+
it "prioritizes ENV['HORO_BADGE_VERSION'] over ENV['HORO_PROJECT_VERSION']" do
212+
with_env("HORO_BADGE_VERSION" => "badge", "HORO_PROJECT_VERSION" => "project") do
213+
_(@helpers.project_version).must_equal "badge"
222214
end
223215
end
224216

225-
it "returns nil when ENV['HORO_BADGE_VERSION'] is not set" do
226-
with_env("HORO_BADGE_VERSION" => nil) do
227-
_(@helpers.badge_version).must_be_nil
217+
it "returns nil when neither ENV['HORO_BADGE_VERSION'] nor ENV['HORO_PROJECT_VERSION'] are set" do
218+
with_env("HORO_BADGE_VERSION" => nil, "HORO_PROJECT_VERSION" => nil) do
219+
_(@helpers.project_version).must_be_nil
228220
end
229221
end
230222
end
@@ -265,26 +257,26 @@ module Foo; module Bar; module Qux; end; end; end
265257
end
266258

267259
describe "#og_title" do
268-
it "includes ENV['HORO_PROJECT_NAME'] and ENV['HORO_BADGE_VERSION']" do
269-
with_env("HORO_PROJECT_NAME" => "My Gem", "HORO_BADGE_VERSION" => "v2.0") do
260+
it "includes ENV['HORO_PROJECT_NAME'] and ENV['HORO_PROJECT_VERSION']" do
261+
with_env("HORO_PROJECT_NAME" => "My Gem", "HORO_PROJECT_VERSION" => "v2.0") do
270262
_(@helpers.og_title("Foo")).must_equal "Foo (My Gem v2.0)"
271263
end
272264

273-
with_env("HORO_PROJECT_NAME" => "My Gem", "HORO_BADGE_VERSION" => nil) do
265+
with_env("HORO_PROJECT_NAME" => "My Gem", "HORO_PROJECT_VERSION" => nil) do
274266
_(@helpers.og_title("Foo")).must_equal "Foo (My Gem)"
275267
end
276268

277-
with_env("HORO_PROJECT_NAME" => nil, "HORO_BADGE_VERSION" => "v2.0") do
269+
with_env("HORO_PROJECT_NAME" => nil, "HORO_PROJECT_VERSION" => "v2.0") do
278270
_(@helpers.og_title("Foo")).must_equal "Foo (v2.0)"
279271
end
280272

281-
with_env("HORO_PROJECT_NAME" => nil, "HORO_BADGE_VERSION" => nil) do
273+
with_env("HORO_PROJECT_NAME" => nil, "HORO_PROJECT_VERSION" => nil) do
282274
_(@helpers.og_title("Foo")).must_equal "Foo"
283275
end
284276
end
285277

286278
it "escapes the title" do
287-
with_env("HORO_PROJECT_NAME" => "Ruby & Rails", "HORO_BADGE_VERSION" => "~> 1.0.0") do
279+
with_env("HORO_PROJECT_NAME" => "Ruby & Rails", "HORO_PROJECT_VERSION" => "~> 1.0.0") do
288280
_(@helpers.og_title("Foo<Bar>")).must_equal "Foo&lt;Bar&gt; (Ruby &amp; Rails ~&gt; 1.0.0)"
289281
end
290282
end

spec/postprocessor_spec.rb

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,16 @@
3434
<a href="https://guides.rubyonrails.org/testing.html">Testing</a>
3535
HTML
3636

37-
with_env("HORO_PROJECT_VERSION" => "3.2.1", "HORO_PROJECT_NAME" => "Ruby on Rails") do
38-
_(SDoc::Postprocessor.process(rendered)).
39-
must_include %(<a href="https://guides.rubyonrails.org/v3.2.1/testing.html">Testing</a>)
40-
end
41-
42-
with_env("HORO_PROJECT_VERSION" => "main@1337c0d3", "HORO_PROJECT_NAME" => "Ruby on Rails") do
43-
_(SDoc::Postprocessor.process(rendered)).
44-
must_include %(<a href="https://edgeguides.rubyonrails.org/testing.html">Testing</a>)
45-
end
46-
47-
with_env("HORO_PROJECT_VERSION" => nil, "HORO_PROJECT_NAME" => "Ruby on Rails") do
48-
_(SDoc::Postprocessor.process(rendered)).
49-
must_include %(<a href="https://guides.rubyonrails.org/testing.html">Testing</a>)
37+
{
38+
"3.2.1" => %(<a href="https://guides.rubyonrails.org/v3.2.1/testing.html">Testing</a>),
39+
"v3.2.1" => %(<a href="https://guides.rubyonrails.org/v3.2.1/testing.html">Testing</a>),
40+
"main@1337c0d3" => %(<a href="https://edgeguides.rubyonrails.org/testing.html">Testing</a>),
41+
"edge" => %(<a href="https://edgeguides.rubyonrails.org/testing.html">Testing</a>),
42+
nil => %(<a href="https://guides.rubyonrails.org/testing.html">Testing</a>),
43+
}.each do |version, expected|
44+
with_env("HORO_PROJECT_VERSION" => version, "HORO_PROJECT_NAME" => "Ruby on Rails") do
45+
_(SDoc::Postprocessor.process(rendered)).must_include expected
46+
end
5047
end
5148
end
5249

spec/rdoc_generator_spec.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,30 @@ def parse_options(*options)
4646
end
4747

4848
describe "options.title" do
49-
it "includes ENV['HORO_PROJECT_NAME'] and ENV['HORO_BADGE_VERSION'] by default" do
50-
with_env("HORO_PROJECT_NAME" => "My Gem", "HORO_BADGE_VERSION" => "v2.0") do
49+
it "includes ENV['HORO_PROJECT_NAME'] and ENV['HORO_PROJECT_VERSION'] by default" do
50+
with_env("HORO_PROJECT_NAME" => "My Gem", "HORO_PROJECT_VERSION" => "v2.0") do
5151
_(parse_options().title).must_equal "My Gem v2.0 API documentation"
5252
end
5353

54-
with_env("HORO_PROJECT_NAME" => "My Gem", "HORO_BADGE_VERSION" => nil) do
54+
with_env("HORO_PROJECT_NAME" => "My Gem", "HORO_PROJECT_VERSION" => nil) do
5555
_(parse_options().title).must_equal "My Gem API documentation"
5656
end
5757

58-
with_env("HORO_PROJECT_NAME" => nil, "HORO_BADGE_VERSION" => "v2.0") do
58+
with_env("HORO_PROJECT_NAME" => nil, "HORO_PROJECT_VERSION" => "v2.0") do
5959
_(parse_options().title).must_equal "v2.0 API documentation"
6060
end
6161

62-
with_env("HORO_PROJECT_NAME" => nil, "HORO_BADGE_VERSION" => nil) do
62+
with_env("HORO_PROJECT_NAME" => nil, "HORO_PROJECT_VERSION" => nil) do
6363
_(parse_options().title).must_equal "API documentation"
6464
end
6565
end
6666

67+
it "prioritizes ENV['HORO_BADGE_VERSION'] over ENV['HORO_PROJECT_VERSION']" do
68+
with_env("HORO_BADGE_VERSION" => "badge", "HORO_PROJECT_VERSION" => "project") do
69+
_(parse_options().title).must_equal "badge API documentation"
70+
end
71+
end
72+
6773
it "can be overridden" do
6874
_(parse_options("--title", "Docs Docs Docs!").title).must_equal "Docs Docs Docs!"
6975
end

0 commit comments

Comments
 (0)