Module: RuoteKit::Helpers::LinkHelpers

Defined in:
lib/ruote-kit/helpers/link_helpers.rb

Overview

Helpers about links

Instance Method Summary collapse

Instance Method Details

Returns an <a href=“x” rel=“y”>z</a>, uses #hlink.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ruote-kit/helpers/link_helpers.rb', line 70

def alink(*args)

  args << {} unless args.last.is_a?(Hash)
  args.last[:title] = true

  opts = args.last

  hl = hlink(*args)

  attributes = %w[ href rel title ].inject([]) { |a, att|
    if val = hl[att]
      a << "#{att}=\"#{val}\""
    end
    a
  }.join(' ')

  "<a #{attributes}>#{opts[:text] || hl['href']}</a>"
end

#as_jsonObject

Computes the path to the JSON alternate version of the current request uri, used by the the “as_json link at the bottom of each page.

Returns the String path.



16
17
18
19
20
21
22
23
24
25
# File 'lib/ruote-kit/helpers/link_helpers.rb', line 16

def as_json

  href = request.path + '.json'

  if request.query_string.length > 0
    href = "#{href}?#{request.query_string}"
  end

  href
end

Determining { ‘href’ => ‘x’, ‘rel’ => ‘y’, … } and co



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ruote-kit/helpers/link_helpers.rb', line 29

def hlink(*args)

  query = args.last.is_a?(Hash) ? args.pop : {}
  query = query.reject { |k, v| k == :text }

  args = args.collect { |arg| arg.to_s }

  if args.last == 'head'
    args.pop
    query[:skip] = 0
    query[:limit] = settings.limit
  end

  rel   = query.delete(:rel) || compute_rel(args)
  title = query.delete(:title)

  query = Rack::Utils.build_query(query.sort)
  query = "?#{query}" if !query.empty?

  if args.empty? or ( ! args.first.match(/^\/\_ruote/))
    args.unshift('/_ruote')
  end
  href = args.join('/')

  href = url("#{href}#{query}", false)

  result = {
    'href' => href,
    'rel'  => rel.match(/^#/) ?
      "http://ruote.rubyforge.org/rels.html#{rel}" : rel,
  }

  if title
    result['title'] = title == true ? href : title
  end

  result
end