From 16ddcb100c96e91102842d16e2e4ee070d607cea Mon Sep 17 00:00:00 2001 From: Thibaut Courouble Date: Sun, 24 Jan 2016 12:24:05 -0500 Subject: [PATCH] Simplify version path separator Ref #25. --- assets/javascripts/models/doc.coffee | 2 +- lib/app.rb | 4 ++-- lib/docs/core/doc.rb | 2 +- test/app_test.rb | 26 +++++++++++++------------- test/files/docs.json | 2 +- test/lib/docs/core/doc_test.rb | 8 ++++---- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/assets/javascripts/models/doc.coffee b/assets/javascripts/models/doc.coffee index 10ba68cd..5a7961ad 100644 --- a/assets/javascripts/models/doc.coffee +++ b/assets/javascripts/models/doc.coffee @@ -4,7 +4,7 @@ class app.models.Doc extends app.Model constructor: -> super @reset @ - @slug_without_version = @slug.split('~v')[0] + @slug_without_version = @slug.split('~')[0] @fullName = "#{@name}" + if @version then " #{@version}" else '' @icon = @slug_without_version @text = @toEntry().text diff --git a/lib/app.rb b/lib/app.rb index 71ec6d09..c25ed086 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -123,7 +123,7 @@ class App < Sinatra::Application def find_doc(slug) settings.docs[slug] || begin - slug = "#{slug}~v" + slug = "#{slug}~" settings.docs.each do |_slug, _doc| return _doc if _slug.start_with?(slug) end @@ -133,7 +133,7 @@ class App < Sinatra::Application def user_has_docs?(slug) docs.include?(slug) || begin - slug = "#{slug}~v" + slug = "#{slug}~" docs.any? { |_slug| _slug.start_with?(slug) } end end diff --git a/lib/docs/core/doc.rb b/lib/docs/core/doc.rb index b906e1cb..cf62ba56 100644 --- a/lib/docs/core/doc.rb +++ b/lib/docs/core/doc.rb @@ -48,7 +48,7 @@ module Docs def slug slug = @slug || name.try(:downcase) - version? ? "#{slug}~v#{version}" : slug + version? ? "#{slug}~#{version}" : slug end def path diff --git a/test/app_test.rb b/test/app_test.rb index db7d7ba1..01660b47 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -80,11 +80,11 @@ class AppTest < MiniTest::Spec end it "works with cookie" do - set_cookie('docs=css/html~v5') + set_cookie('docs=css/html~5') get '/manifest.appcache' assert last_response.ok? assert_includes last_response.body, '/css/index.json?1420139788' - assert_includes last_response.body, '/html~v5/index.json?1420139791' + assert_includes last_response.body, '/html~5/index.json?1420139791' end it "ignores invalid docs in the cookie" do @@ -127,17 +127,17 @@ class AppTest < MiniTest::Spec describe "/[doc]" do it "renders when the doc exists and isn't enabled" do - set_cookie('docs=html~v5') - get '/html~v4/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER + set_cookie('docs=html~5') + get '/html~4/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER assert last_response.ok? end it "redirects via JS cookie when the doc exists and is enabled" do - set_cookie('docs=html~v5') - get '/html~v5/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER + set_cookie('docs=html~5') + get '/html~5/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER assert last_response.redirect? assert_equal 'http://example.org/', last_response['Location'] - assert last_response['Set-Cookie'].start_with?("initial_path=%2Fhtml%7Ev5%2F; path=/; expires=") + assert last_response['Set-Cookie'].start_with?("initial_path=%2Fhtml%7E5%2F; path=/; expires=") end it "renders when the doc exists, has no version in the path, and isn't enabled" do @@ -146,7 +146,7 @@ class AppTest < MiniTest::Spec end it "redirects via JS cookie when the doc exists, has no version in the path, and a version is enabled" do - set_cookie('docs=html~v5') + set_cookie('docs=html~5') get '/html/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER assert last_response.redirect? assert_equal 'http://example.org/', last_response['Location'] @@ -160,7 +160,7 @@ class AppTest < MiniTest::Spec end it "returns 404 when the doc doesn't exist" do - get '/html~v6/' + get '/html~6/' assert last_response.not_found? end @@ -177,15 +177,15 @@ class AppTest < MiniTest::Spec describe "/[doc]-[type]" do it "works when the doc exists" do - get '/html~v4-foo-bar_42/' + get '/html~4-foo-bar_42/' assert last_response.ok? - assert_includes last_response.body, 'app.DOC = {"name":"HTML","slug":"html~v4"' + assert_includes last_response.body, 'app.DOC = {"name":"HTML","slug":"html~4"' end it "works when the doc has no version in the path and a version exists" do get '/html-foo-bar_42/' assert last_response.ok? - assert_includes last_response.body, 'app.DOC = {"name":"HTML","slug":"html~v5"' + assert_includes last_response.body, 'app.DOC = {"name":"HTML","slug":"html~5"' end it "returns 404 when the type is blank" do @@ -199,7 +199,7 @@ class AppTest < MiniTest::Spec end it "returns 404 when the doc doesn't exist" do - get '/html~v6-bar/' + get '/html~6-bar/' assert last_response.not_found? end diff --git a/test/files/docs.json b/test/files/docs.json index c70c2056..bd9146b5 100644 --- a/test/files/docs.json +++ b/test/files/docs.json @@ -1 +1 @@ -[{"name":"CSS","slug":"css","type":"mdn","release":null,"mtime":1420139788,"db_size":3460507},{"name":"DOM","slug":"dom","type":"mdn","release":null,"mtime":1420139789,"db_size":11399128},{"name":"DOM Events","slug":"dom_events","type":"mdn","release":null,"mtime":1420139790,"db_size":889020},{"name":"HTML","slug":"html~v5","type":"mdn","version":"5","mtime":1420139791,"db_size":1835647},{"name":"HTML","slug":"html~v4","type":"mdn","version":"4","mtime":1420139790,"db_size":1835646},{"name":"HTTP","slug":"http","type":"rfc","release":null,"mtime":1420139790,"db_size":183083},{"name":"JavaScript","slug":"javascript","type":"mdn","release":null,"mtime":1420139791,"db_size":4125477}] +[{"name":"CSS","slug":"css","type":"mdn","release":null,"mtime":1420139788,"db_size":3460507},{"name":"DOM","slug":"dom","type":"mdn","release":null,"mtime":1420139789,"db_size":11399128},{"name":"DOM Events","slug":"dom_events","type":"mdn","release":null,"mtime":1420139790,"db_size":889020},{"name":"HTML","slug":"html~5","type":"mdn","version":"5","mtime":1420139791,"db_size":1835647},{"name":"HTML","slug":"html~4","type":"mdn","version":"4","mtime":1420139790,"db_size":1835646},{"name":"HTTP","slug":"http","type":"rfc","release":null,"mtime":1420139790,"db_size":183083},{"name":"JavaScript","slug":"javascript","type":"mdn","release":null,"mtime":1420139791,"db_size":4125477}] diff --git a/test/lib/docs/core/doc_test.rb b/test/lib/docs/core/doc_test.rb index a7091463..d8ba011c 100644 --- a/test/lib/docs/core/doc_test.rb +++ b/test/lib/docs/core/doc_test.rb @@ -45,15 +45,15 @@ class DocsDocTest < MiniTest::Spec assert_equal 'doc', Docs::Doc.slug end - it "returns 'doc~v42' when the class is Docs::Doc and its #version is '42'" do + it "returns 'doc~42' when the class is Docs::Doc and its #version is '42'" do stub(Docs::Doc).version { '42' } - assert_equal 'doc~v42', Docs::Doc.slug + assert_equal 'doc~42', Docs::Doc.slug end - it "returns 'foo~v42' when #slug has been set to 'foo' and #version to '42'" do + it "returns 'foo~42' when #slug has been set to 'foo' and #version to '42'" do doc.slug = 'foo' doc.version = '42' - assert_equal 'foo~v42', doc.slug + assert_equal 'foo~42', doc.slug end end