Class: AssociationRenderer

Inherits:
ResourceRenderer::AttributeRenderer::Base show all
Defined in:
app/attribute_renderers/association_renderer.rb

Instance Attribute Summary

Attributes inherited from ResourceRenderer::AttributeRenderer::Base

#helper, #model

Instance Method Summary collapse

Methods inherited from ResourceRenderer::AttributeRenderer::Base

#initialize

Constructor Details

This class inherits a constructor from ResourceRenderer::AttributeRenderer::Base

Instance Method Details

#display(attribute_name, label, options = {}, &block) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/attribute_renderers/association_renderer.rb', line 2

def display(attribute_name, label, options = {}, &block)
  options.reverse_merge!(label_method: :to_s)
  label_method = options.delete(:label_method)

  associated = value_for_attribute(attribute_name)

  return if associated.nil?

  url = options.delete(:url)
  label = associated.send(label_method)

  target = url.call(model) if url.is_a?(Proc)
  target = url if url.is_a?(String)
  target = associated if url.is_a?(TrueClass)

  if target.nil?
    label
  else
    h.link_to(label, target)
  end
end