Module: RocketPants::Linking

Defined in:
lib/rocket_pants/controller/linking.rb

Instance Method Summary collapse

Instance Method Details

#expose_metadata(metadata) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/rocket_pants/controller/linking.rb', line 29

def ()
  super.tap do |meta|
    if RocketPants.header_metadata? && (pagination = meta[:pagination])
      links :next  => (pagination[:next] && page_url(pagination[:next])),
            :prev  => (pagination[:previous] && page_url(pagination[:previous])),                
            :last  => (pagination[:pages] && page_url(pagination[:pages])),
            :first => page_url(1)
    end
  end
end

Generates a Link: header with the specified rel, uri and attributes.

Parameters:

  • rel (String, Symbol)

    the relation of the given link

  • uri (String)

    the full uri to the specified link resource

  • attributes (Hash) (defaults to: {})

    any other attributes for the link



8
9
10
11
12
13
14
# File 'lib/rocket_pants/controller/linking.rb', line 8

def link(rel, uri, attributes = {})
  headers['Link'] ||= []
  attributes = {:rel => rel}.merge(attributes)
  link = "<#{uri}>"
  attributes.each_pair { |k, v| link << "; #{k}=\"#{v}\"" }
  headers['Link'] << link
end

Lets you add a series of links for the current resource.

Parameters:

  • links (Hash{Symbol => String}) (defaults to: {})

    a hash of links. Those with nil as the value are skipped.



18
19
20
21
22
# File 'lib/rocket_pants/controller/linking.rb', line 18

def links(links = {})
  links.each_pair do |rel, uri|
    link rel, uri if uri
  end
end

#page_url(page) ⇒ Object

Hook method - Implement this to link to the current resource and we’ll automatically add header links.



25
26
27
# File 'lib/rocket_pants/controller/linking.rb', line 25

def page_url(page)
  nil
end