Module: Webby::Helpers::UrlHelper

Defined in:
lib/webby/helpers/url_helper.rb

Instance Method Summary collapse

Instance Method Details

call-seq:

link_to( name, url, :attrs => {} )

Create an HTTP anchor tag with

url can be a url string, a page, :back, or nothing

:attrs are used to generate HTML anchor tag attributes

Examples

<%= link_to('Google', 'http://www.google.com/', :attrs => {:name => 'google'}) %>
# => <a href="http://www.google.com/" name="google">Google</a>

<%= link_to('A Page', @page, :anchor => 'blah') %>
# => <a href="/a/page.html#blah">A Page</a>


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/webby/helpers/url_helper.rb', line 91

def link_to( name, *args )
  opts = Hash === args.last ? args.pop : {}
  obj = args.first
  attrs = opts.delete(:attrs)

  url = case obj
    when String, Webby::Resources::Resource
      self.url_for(obj, opts)
    when ::Webby::Paginator::Page
      self.url_for(obj.url, opts)
    when :back
      'javascript:history.back()'
    else
      self.url_for(name, opts)
    end

  if attrs
    html_opts = attrs.stringify_keys
    href = html_opts.has_key? 'href'
    attrs = tag_options(html_opts)
  else
    href = false
    attrs = nil
  end

  href_attr = href ? nil : %Q(href="#{url}")
  "<a #{href_attr}#{attrs}>#{name || h(url)}</a>"
end

call-seq:

link_to_page( name )
link_to_page( :key => value )
link_to_page( name, :key => value )
link_to_page( page )

Creates a link tag of the given name using a URL created by finding the associated page from the key/value pairs. If the key/value pairs are omitted, the name is used in conjunction with the default site find_by attribute. Unless changed by the user, the default find_by attribute is the page title.

Pages are found using key/value pairs. The key is any of the page attributes, and the value is what that attribute should be. Any number of key/value pairs can be included, but all values must equal the corresponding page attributes for a match to be found -- i.e. the comparisons are joined by AND operations to determine a match.

In the absence of any key/value pairs -- just a name was given -- then the default site find_by attribute is used, and the name is compared against this attribute from the page. The default find_by attribue is set in the Rakefile or in the Webby.site.find_by parameter.

Several options can be passed to the method to determin how URLs are created and to specify any HTML attributes on the returned link tag. The URL options are given as a hash to the :url key. The HTML attributes are given as a hash to the :attrs key.

See the url_for method for a desciption of the :url options. See the link_to method for a description of the :attrs options.

Examples

<%= link_to_page('Funny Story', :url => {:anchor => 'punchline'}) %>
# => <a href="/humor/funny_story.html#punchline">Funny Story</a>

<%= link_to_page('Hilarious', :title => 'Funny Story') %>
# => <a href="/humor/funn_story.html">Hilarious</a>


160
161
162
163
164
165
# File 'lib/webby/helpers/url_helper.rb', line 160

def link_to_page( *args )
  self.link_to(*_find_page(args))
rescue ::Webby::Error => err
  return yield if block_given?
  raise err
end

call-seq:

link_to_page_unless_current( name )
link_to_page_unless_current( :key => value )
link_to_page_unless_current( name, :key => value )
link_to_page_unless_current( page )

This function operates in the same fashion as the link_to_page fuction with the exception that if the page to be linked to is the current page, then only the name is rendered without an HTML anchor tag.

Examples

<%= link_to_page_unless_current('Funny Story') %>
# => <a href="/humor/funny_story.html">Funny Story</a>

<%= link_to_page_unless_current(@page) %>
# => This Page


185
186
187
188
189
190
191
192
193
# File 'lib/webby/helpers/url_helper.rb', line 185

def link_to_page_unless_current( *args )
  name, page, link_opts = _find_page(args)
  return name if @page == page

  self.link_to(name, page, link_opts)
rescue ::Webby::Error => err
  return yield if block_given?
  raise err
end

#url_for(*args) ⇒ Object

call-seq:

url_for( name, opts = {} )

Creates a URL for the given name and opts. If name is a string then it is used as the URL base. If name is a Resource then it is converted to a URL by calling its url method.

Options

  • :escape ??? determines whether the returned URL will be HTML escaped or not (true by default)

  • :anchor ??? specifies the anchor name to be appended to the path

Examples

<%= url_for('/some/page.html') %>
# => /some/page

<%= url_for('/some/page.html', :anchor => 'tidbit') %>
# => /some/page#tidbit

<%= url_for(@page) %>
# => /current/page.html

<%= url_for(@page, :anchor => 'this&that') %>
# => /current/page.html#this&amp;that


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/webby/helpers/url_helper.rb', line 33

def url_for( *args )
  opts = Hash === args.last ? args.pop : {}
  obj = args.first

  anchor = opts.delete(:anchor)
  escape = opts.has_key?(:escape) ? opts.delete(:escape) : true

  url = Webby::Resources::Resource === obj ? obj.url : obj.to_s
  url = escape_once(url) if escape
  url << "#" << anchor if anchor

  return url
end

#url_for_page(opts = {}) ⇒ Object

call-seq:

url_for_page( :key => value, :url => {} )

Creates a URL for the page identified by the set of :key / value pairs. The :url options are passed to the url_for method for final URL creation; see the url_for method for documentation on those options.

The Resources::DB#find method is used to locate the page; see the find method for the available options.

Examples

<%= url_for_page(:title => 'Funny Story', :anchor => 'punchline') %>
# => /humor/funny_story.html#punchline

Raises:



63
64
65
66
67
68
69
70
71
72
# File 'lib/webby/helpers/url_helper.rb', line 63

def url_for_page( opts = {} )
  opts = opts.symbolize_keys
  url_opts = opts.delete(:url)

  p = @pages.find(opts)
  raise ::Webby::Error,
        "could not find requested page: #{opts.inspect}" if p.nil?

  self.url_for(p, url_opts)
end