Class: DisplayFor::Element::Attribute

Inherits:
Base
  • Object
show all
Defined in:
lib/display_for/element/attribute.rb

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#html_options, #initialize, #label, #link_to

Constructor Details

This class inherits a constructor from DisplayFor::Element::Base

Instance Method Details

#content(resource) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/display_for/element/attribute.rb', line 10

def content(resource)
  if @block
    @block.call(resource)
  else
    value(resource)
  end
end

#default_label(resource_class) ⇒ Object



4
5
6
7
8
# File 'lib/display_for/element/attribute.rb', line 4

def default_label(resource_class)
  raise "#{resource_class} does not respond to human_attribute_name" unless resource_class.respond_to?(:human_attribute_name)

  resource_class.human_attribute_name(@name.to_s)
end

#templateObject



39
40
41
# File 'lib/display_for/element/attribute.rb', line 39

def template
  @builder.template
end

#value(resource) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/display_for/element/attribute.rb', line 18

def value(resource)
  raise "#{resource.class} does not respond to #{@name}" unless resource.respond_to?(@name)
  
  result = resource.send(@name) 
  
  if @options[:type]
    if @options[:type] == :html
      result = result.html_safe
    else
      result = template.send(:"number_to_#{@options[:type]}", result)
    end
  end
  
  if [Date, DateTime, Time].include?(result.class) &&  @options[:format]
    result = template.l(result, :format => @options[:format])
  end
  
  result = link_to(result, resource) if @options[:link_to]
  result.to_s
end