Class: DisplayFor::Element::Base

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

Direct Known Subclasses

Action, Attribute, Html

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder, name, options = {}, &block) ⇒ Base

Returns a new instance of Base.



6
7
8
# File 'lib/display_for/element/base.rb', line 6

def initialize(builder, name, options = {}, &block)
  @builder, @name, @options, @block = builder, name, options, block
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/display_for/element/base.rb', line 4

def name
  @name
end

Instance Method Details

#content(resource) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/display_for/element/base.rb', line 28

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

#default_label(resource_classs = nil) ⇒ Object



24
25
26
# File 'lib/display_for/element/base.rb', line 24

def default_label(resource_classs = nil)
  @name.to_s.humanize
end

#html_optionsObject



10
11
12
# File 'lib/display_for/element/base.rb', line 10

def html_options
  @options[:html] || {}
end

#label(resource_class = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/display_for/element/base.rb', line 14

def label(resource_class = nil)
  return nil if @options[:label] === false
  
  name = @options[:label] || @name.to_sym
  
  return name unless name.is_a?(Symbol)
  
  I18n.t(name, :scope => 'display_for.labels', :default => default_label(resource_class) )
end


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/display_for/element/base.rb', line 36

def link_to(string, resource, html_options = {})
  url = if @options[:link_to].respond_to?(:call)
    @options[:link_to].call(resource)
  elsif @options[:link_to].is_a?(Symbol)
    @builder.template.send(@options[:link_to], resource)
  else
    @builder.namespace + [resource]
  end

  return nil unless url

  html_options[:method]  ||= @options[:method]
  html_options[:confirm] ||= @options[:confirm]
  html_options[:remote]  ||= @options[:remote]
  html_options[:class]   ||= @options[:class]

  @builder.template.link_to(string, url, html_options)
end