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.



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

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

Computing the href for the “as_json” link at the bottom of each page



13
14
15
16
17
18
19
20
21
22
# File 'lib/ruote-kit/helpers/link_helpers.rb', line 13

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



26
27
28
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
# File 'lib/ruote-kit/helpers/link_helpers.rb', line 26

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 = query.inject({}) { |h, (k, v)| h[k.to_s] = v; h }
  query = query.keys.sort.collect { |k| "#{k}=#{query[k]}" }.join('&')
  query = "?#{query}" if query.length > 0

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

  href = "#{href}#{query}"

  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