From 2663a1b685dbfa8181403ecc3328ebf33bb41c64 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Sun, 30 Jul 2023 21:44:43 -0500 Subject: [PATCH] Use nil for index context This simplifies the code and also paves the way for generalizing `canonical_url`'s parameter. --- lib/rdoc/generator/template/rails/index.rhtml | 2 +- lib/sdoc/helpers.rb | 16 +++------------- spec/helpers_spec.rb | 18 +++++++++--------- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/lib/rdoc/generator/template/rails/index.rhtml b/lib/rdoc/generator/template/rails/index.rhtml index 27ae175f..6be3a7de 100644 --- a/lib/rdoc/generator/template/rails/index.rhtml +++ b/lib/rdoc/generator/template/rails/index.rhtml @@ -4,7 +4,7 @@ <%= page_title %> - <%= include_template '_head.rhtml', {:context => :index, tree_keys: []} %> + <%= include_template '_head.rhtml', {:context => nil, tree_keys: []} %> diff --git a/lib/sdoc/helpers.rb b/lib/sdoc/helpers.rb index 25d2b7ae..211ddaaa 100644 --- a/lib/sdoc/helpers.rb +++ b/lib/sdoc/helpers.rb @@ -20,22 +20,12 @@ def link_to_external(text, url, html_attributes = {}) end def base_tag_for_context(context) - if context == :index - %() - else - relative_root = "../" * context.path.count("/") - %() - end + relative_root = "../" * context.path.count("/") if context + %() 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 diff --git a/spec/helpers_spec.rb b/spec/helpers_spec.rb index 2813213f..8f5c13f6 100644 --- a/spec/helpers_spec.rb +++ b/spec/helpers_spec.rb @@ -135,9 +135,9 @@ module Foo; class Bar; def qux; end; end; end end describe "#base_tag_for_context" do - it "returns an idempotent tag for the :index context" do - _(@helpers.base_tag_for_context(:index)). - must_equal %() + it "returns an idempotent tag for nil context" do + _(@helpers.base_tag_for_context(nil)). + must_equal %() end it "returns a tag with an appropriate path for the given RDoc::Context" do @@ -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 %() + must_equal %() _(@helpers.base_tag_for_context(top_level.find_module_named("Foo::Bar"))). - must_equal %() + must_equal %() _(@helpers.base_tag_for_context(top_level.find_module_named("Foo::Bar::Qux"))). - must_equal %() + must_equal %() end end @@ -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