Skip to content

Commit

Permalink
Merge pull request #264 from jonathanhefner/index-context-as-nil
Browse files Browse the repository at this point in the history
Use `nil` for index context
  • Loading branch information
jonathanhefner authored Jul 31, 2023
2 parents 56ab26c + 2663a1b commit b6bf464
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/rdoc/generator/template/rails/index.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="<%= @options.charset %>">
<title><%= page_title %></title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= include_template '_head.rhtml', {:context => :index, tree_keys: []} %>
<%= include_template '_head.rhtml', {:context => nil, tree_keys: []} %>
</head>

<body>
Expand Down
16 changes: 3 additions & 13 deletions lib/sdoc/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,12 @@ def link_to_external(text, url, html_attributes = {})
end

def base_tag_for_context(context)
if context == :index
%(<base href="./" data-current-path=".">)
else
relative_root = "../" * context.path.count("/")
%(<base href="#{relative_root}" data-current-path="#{context.path}">)
end
relative_root = "../" * context.path.count("/") if context
%(<base href="./#{relative_root}" data-current-path="#{context&.path}">)
end

def canonical_url(context)
if ENV["HORO_CANONICAL_URL"]
if context == :index
"#{ENV["HORO_CANONICAL_URL"]}/"
else
"#{ENV["HORO_CANONICAL_URL"]}/#{context.as_href("")}"
end
end
"#{ENV["HORO_CANONICAL_URL"]}/#{context&.path}" if ENV["HORO_CANONICAL_URL"]
end

def project_name
Expand Down
18 changes: 9 additions & 9 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ module Foo; class Bar; def qux; end; end; end
end

describe "#base_tag_for_context" do
it "returns an idempotent <base> tag for the :index context" do
_(@helpers.base_tag_for_context(:index)).
must_equal %(<base href="./" data-current-path=".">)
it "returns an idempotent <base> tag for nil context" do
_(@helpers.base_tag_for_context(nil)).
must_equal %(<base href="./" data-current-path="">)
end

it "returns a <base> tag with an appropriate path for the given RDoc::Context" do
Expand All @@ -146,13 +146,13 @@ module Foo; module Bar; module Qux; end; end; end
RUBY

_(@helpers.base_tag_for_context(top_level.find_module_named("Foo"))).
must_equal %(<base href="../" data-current-path="classes/Foo.html">)
must_equal %(<base href="./../" data-current-path="classes/Foo.html">)

_(@helpers.base_tag_for_context(top_level.find_module_named("Foo::Bar"))).
must_equal %(<base href="../../" data-current-path="classes/Foo/Bar.html">)
must_equal %(<base href="./../../" data-current-path="classes/Foo/Bar.html">)

_(@helpers.base_tag_for_context(top_level.find_module_named("Foo::Bar::Qux"))).
must_equal %(<base href="../../../" data-current-path="classes/Foo/Bar/Qux.html">)
must_equal %(<base href="./../../../" data-current-path="classes/Foo/Bar/Qux.html">)
end
end

Expand All @@ -167,15 +167,15 @@ module Foo; module Bar; module Qux; end; end; end
end
end

it "returns a URL based on ENV['HORO_CANONICAL_URL'] for the :index context" do
it "returns a URL based on ENV['HORO_CANONICAL_URL'] for nil context" do
with_env("HORO_CANONICAL_URL" => "https://canonical") do
_(@helpers.canonical_url(:index)).must_equal "https://canonical/"
_(@helpers.canonical_url(nil)).must_equal "https://canonical/"
end
end

it "returns nil when ENV['HORO_CANONICAL_URL'] is not set" do
with_env("HORO_CANONICAL_URL" => nil) do
_(@helpers.canonical_url(:index)).must_be_nil
_(@helpers.canonical_url(nil)).must_be_nil
end
end
end
Expand Down

0 comments on commit b6bf464

Please sign in to comment.