Module: Nesta::View::Helpers

Defined in:
lib/nesta/helpers.rb

Instance Method Summary collapse

Instance Method Details

#absolute_urls(text) ⇒ Object


27
28
29
30
# File 'lib/nesta/helpers.rb', line 27

def absolute_urls(text)
  text.gsub!(/(<a href=['"])\//, '\1' + path_to('/', uri: true))
  text
end

#article_summaries(articles) ⇒ Object


79
80
81
# File 'lib/nesta/helpers.rb', line 79

def article_summaries(articles)
  haml(:summaries, layout: false, locals: { pages: articles })
end

#articles_headingObject


83
84
85
# File 'lib/nesta/helpers.rb', line 83

def articles_heading
  @page.('articles heading') || "Articles on #{@page.heading}"
end

#atom_id(page = nil) ⇒ Object


37
38
39
40
41
42
43
# File 'lib/nesta/helpers.rb', line 37

def atom_id(page = nil)
  if page
    page.atom_id || nesta_atom_id_for_page(page)
  else
    "tag:#{request.host},2009:/"
  end
end

#format_date(date) ⇒ Object


59
60
61
# File 'lib/nesta/helpers.rb', line 59

def format_date(date)
  date.strftime("%d %B %Y")
end

#heading_or_site_titleObject

Returns the current page's heading or the site's title, if we don't have a current page, or the page doesn't have a heading defined.

Useful in templates that generate links to social sharing sites, which need a suitable description for the page being shared or bookmarked.


53
54
55
56
57
# File 'lib/nesta/helpers.rb', line 53

def heading_or_site_title
  @page && @page.heading
rescue Nesta::HeadingNotSet
  @title
end

#latest_articles(count = 8) ⇒ Object


75
76
77
# File 'lib/nesta/helpers.rb', line 75

def latest_articles(count = 8)
  Nesta::Page.find_articles[0..count - 1]
end

#local_stylesheet?Boolean


63
64
65
66
# File 'lib/nesta/helpers.rb', line 63

def local_stylesheet?
  Nesta.deprecated('local_stylesheet?', 'use local_stylesheet_link_tag')
  File.exist?(File.expand_path('views/local.sass', Nesta::App.root))
end

68
69
70
71
72
73
# File 'lib/nesta/helpers.rb', line 68

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: path_to("/css/#{name}.css"), rel: "stylesheet"
  end
end

#nesta_atom_id_for_page(page) ⇒ Object


32
33
34
35
# File 'lib/nesta/helpers.rb', line 32

def nesta_atom_id_for_page(page)
  published = page.date.strftime('%Y-%m-%d')
  "tag:#{request.host},#{published}:#{page.abspath}"
end

#no_widow(text) ⇒ Object


16
17
18
# File 'lib/nesta/helpers.rb', line 16

def no_widow(text)
  text.split[0...-1].join(" ") + "&nbsp;#{text.split[-1]}"
end

#path_to(page_path, options = {}) ⇒ Object

Generates the full path to a given page, taking Rack routers and reverse proxies into account.

Takes an options hash with a single option called `uri`. Set it to `true` if you'd like the publicly accessible URI for the path, rather than just the path relative to the site's root URI. The default is `false`.

path_to(page.abspath, uri: true)

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/nesta/helpers.rb', line 97

def path_to(page_path, options = {})
  host = ''
  if options[:uri]
    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

#set_common_variablesObject


20
21
22
23
24
25
# File 'lib/nesta/helpers.rb', line 20

def set_common_variables
  @menu_items = Nesta::Menu.for_path('/')
  @site_title = Nesta::Config.title
  set_from_config(:title, :subtitle, :google_analytics_code)
  @heading = @title
end

#set_from_config(*variables) ⇒ Object


4
5
6
7
8
# File 'lib/nesta/helpers.rb', line 4

def set_from_config(*variables)
  variables.each do |var|
    instance_variable_set("@#{var}", Nesta::Config.send(var))
  end
end

#set_from_page(*variables) ⇒ Object


10
11
12
13
14
# File 'lib/nesta/helpers.rb', line 10

def set_from_page(*variables)
  variables.each do |var|
    instance_variable_set("@#{var}", @page.send(var))
  end
end