Permalink
Browse files

Replace use of Sinatra's "url" helper method with our own "path_to"

  • Loading branch information...
1 parent a913035 commit d19ec5e1597ef596f450728481fdef477bfe6288 @MicahChalmer MicahChalmer committed with May 10, 2012
Showing with 45 additions and 20 deletions.
  1. +27 −2 lib/nesta/helpers.rb
  2. +2 −2 lib/nesta/navigation.rb
  3. +3 −3 spec/page_spec.rb
  4. +3 −3 views/atom.haml
  5. +1 −1 views/error.haml
  6. +1 −1 views/feed.haml
  7. +2 −2 views/layout.haml
  8. +2 −2 views/page_meta.haml
  9. +2 −2 views/sitemap.haml
  10. +2 −2 views/summaries.haml
View
29 lib/nesta/helpers.rb
@@ -25,7 +25,7 @@ def set_common_variables
end
def absolute_urls(text)
- text.gsub!(/(<a href=['"])\//, '\1' + url('/'))
+ text.gsub!(/(<a href=['"])\//, '\1' + path_to('/', true))
text
end
@@ -54,7 +54,7 @@ def local_stylesheet?
def local_stylesheet_link_tag(name)
pattern = File.expand_path("views/#{name}.s{a,c}ss", Nesta::App.root)
if Dir.glob(pattern).size > 0
- haml_tag :link, :href => url("/css/#{name}.css"), :rel => "stylesheet"
+ haml_tag :link, :href => path_to("/css/#{name}.css"), :rel => "stylesheet"
end
end
@@ -69,6 +69,31 @@ def article_summaries(articles)
def articles_heading
@page.metadata('articles heading') || "Articles on #{@page.heading}"
end
+
+ # Generates the full path to a given page in the app.
+ # Takes Rack routers and reverse proxies into account.
+ # With Sinatra::Helpers included you could get the same
+ # effect with uri(page_path, false) but this is here to avoid
+ # depending on Sinatra::Helpers.
+ #
+ # If absolute is true, we'll return a full URI. Note that unlike
+ # Sinatra's uri method, this deaults to false instead of true.
+ def path_to(page_path, absolute = false)
+ host = ''
+ if absolute
+ host << "http#{'s' if request.ssl?}://"
+ if (request.env.include?("HTTP_X_FORWARDED_HOST") or
+ request.port != (request.ssl? ? 443 : 80))
+ host << request.host_with_port
+ else
+ host << request.host
+ end
+ end
+ uri_parts = [host]
+ uri_parts << request.script_name.to_s if request.script_name
+ uri_parts << page_path
+ File.join(uri_parts)
+ end
end
end
end
View
4 lib/nesta/navigation.rb
@@ -23,7 +23,7 @@ def display_menu_item(item, options = {})
else
html_class = current_item?(item) ? "current" : nil
haml_tag :li, :class => html_class do
- haml_tag :a, :<, :href => url(item.abspath) do
+ haml_tag :a, :<, :href => path_to(item.abspath) do
haml_concat link_text(item)
end
end
@@ -44,7 +44,7 @@ def display_breadcrumbs(options = {})
haml_tag :ul, :class => options[:class] do
breadcrumb_ancestors[0...-1].each do |page|
haml_tag :li do
- haml_tag :a, :<, :href => url(page.abspath), :itemprop => 'url' do
+ haml_tag :a, :<, :href => path_to(page.abspath), :itemprop => 'url' do
haml_tag :span, :<, :itemprop => 'title' do
haml_concat link_text(page)
end
View
6 spec/page_spec.rb
@@ -74,7 +74,7 @@
:ext => :haml,
:content => @default_homepage_content)
do_get
- body.should have_tag("ul.menu a[@href=http://example.org/]", /Home/)
+ body.should have_tag("ul.menu a[@href='/']", /Home/)
end
it "should use the heading if it exists" do
@@ -84,7 +84,7 @@
:heading => 'My heading',
:content => @default_homepage_content)
do_get
- body.should have_tag("ul.menu a[@href=http://example.org/", /My heading/)
+ body.should have_tag("ul.menu a[@href='/'", /My heading/)
end
it "should use the link text if specified" do
@@ -95,7 +95,7 @@
:content => @default_homepage_content,
:metadata => {'link text'=>'My link text'})
do_get
- body.should have_tag("ul.menu a[@href=http://example.org/", /My link text/)
+ body.should have_tag("ul.menu a[@href='/'", /My link text/)
end
end
end
View
6 views/atom.haml
@@ -3,8 +3,8 @@
%title(type='text')= @title
%generator(uri='http://nestacms.com') Nesta
%id= atom_id
- %link(href="#{url('/articles.xml')}" rel='self')
- %link(href="#{url('/')}" rel='alternate')
+ %link(href="#{path_to('/articles.xml', true)}" rel='self')
+ %link(href="#{path_to('/', true)}" rel='alternate')
%subtitle(type='text')= @subtitle
- if @articles[0]
%updated= @articles[0].date(:xmlschema)
@@ -19,7 +19,7 @@
- @articles.each do |article|
%entry
%title= article.heading
- %link{ :href => url(article.path), :type => 'text/html', :rel => 'alternate' }
+ %link{ :href => path_to(article.path, true), :type => 'text/html', :rel => 'alternate' }
%id= atom_id(article)
%content(type='html')&= find_and_preserve(absolute_urls(article.body(self)))
%published= article.date(:xmlschema)
View
2 views/error.haml
@@ -1,5 +1,5 @@
%nav.breadcrumb
- %a(href="#{url('/')}") Home
+ %a(href="#{path_to('/')}") Home
#content
%section(role="main")
View
2 views/feed.haml
@@ -1,3 +1,3 @@
- title ||= "Atom feed"
.feed
- %a(href="#{url('/articles.xml')}" title=title)= title
+ %a(href="#{path_to('/articles.xml')}" title=title)= title
View
4 views/layout.haml
@@ -8,14 +8,14 @@
%meta(name="keywords" content=@keywords)
%title= @title
<!--[if ! lte IE 6]><!-->
- %link(href="#{url('/css/master.css')}" media="screen" rel="stylesheet")
+ %link(href="#{path_to('/css/master.css')}" media="screen" rel="stylesheet")
<!--<![endif]-->
/[if lte IE 6]
%link(rel="stylesheet" href="http://universal-ie6-css.googlecode.com/files/ie6.1.1.css" media="screen, projection")
/[if lt IE 9]
%script(src="//html5shim.googlecode.com/svn/trunk/html5.js")
- local_stylesheet_link_tag('local')
- %link(href="#{url('/articles.xml')}" rel="alternate" type="application/atom+xml")
+ %link(href="#{path_to('/articles.xml')}" rel="alternate" type="application/atom+xml")
= haml :analytics, :layout => false
%body
#container
View
4 views/page_meta.haml
@@ -10,7 +10,7 @@
- page.categories.each do |category|
- if category != page.categories[-1]
= succeed ',' do
- %a(href="#{url(category.abspath)}")= category.link_text
+ %a(href="#{path_to(category.abspath)}")= category.link_text
- else
- %a(href="#{url(category.abspath)}")= category.link_text
+ %a(href="#{path_to(category.abspath)}")= category.link_text
View
4 views/sitemap.haml
@@ -1,11 +1,11 @@
!!! XML
%urlset(xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
%url
- %loc= url('/')
+ %loc= path_to('/',true)
%changefreq daily
%priority 1.0
%lastmod= @last.xmlschema
- @pages.each do |page|
%url
- %loc= url(page.path)
+ %loc= path_to(page.path,true)
%lastmod= page.last_modified.xmlschema
View
4 views/summaries.haml
@@ -5,11 +5,11 @@
%article
%header
%h1
- %a(href="#{url(page.abspath)}")= page.link_text
+ %a(href="#{path_to(page.abspath)}")= page.link_text
- if page.summary.nil? || page.summary.empty?
~ page.body(self)
- else
~ page.summary
%p.read_more
- %a(href="#{url(page.abspath)}")= page.read_more
+ %a(href="#{path_to(page.abspath)}")= page.read_more
= haml :page_meta, :layout => false, :locals => { :page => page }

0 comments on commit d19ec5e

Please sign in to comment.