Module: Hammock::HamlinkTo::InstanceMethods

Defined in:
lib/hammock/hamlink_to.rb

Instance Method Summary collapse

Instance Method Details

Generate a restful link to verb the provided resources if the request would be allowed under the current scope. If the scope would not allow the specified request, the link is ommitted entirely from the page.

Use this method to render edit and delete links, and anything else that is only available to certain users or under certain conditions.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hammock/hamlink_to.rb', line 19

def hamlink_to *args
  opts = args.extract_options!
  verb = args.first if args.first.is_a?(Symbol)
  entity = args.last

  if can_verb_entity?(verb, entity)
    route = route_for *args.push(opts.dragnet(:nest, :format))

    opts[:class] = ['current', opts[:class]].squash.join(' ') if opts[:indicate_current] && route.matches?(request)
    opts[:class] = [link_class_for(route.verb, entity), opts[:class]].squash.join(' ')

    text = opts.delete(:text) || opts.delete(:text_or_else)

    if text.is_a?(Symbol)
      text = entity.send(text)
    end

    link_to(text || route.verb,
      route.path(opts.delete(:params)),
      opts.merge(:method => (route.http_method unless route.get?))
    )
  else
    opts[:else] || opts[:text_or_else]
  end
end