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



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

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

#articles_headingObject



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

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



45
46
47
# File 'lib/nesta/helpers.rb', line 45

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

#latest_articles(count = 8) ⇒ Object



61
62
63
# File 'lib/nesta/helpers.rb', line 61

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

#local_stylesheet?Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/nesta/helpers.rb', line 49

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


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

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)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/nesta/helpers.rb', line 83

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