Class: Manageable::Helpers::AttributesBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/manageable/helpers/attributes_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, template) ⇒ AttributesBuilder

Returns a new instance of AttributesBuilder.



12
13
14
# File 'lib/manageable/helpers/attributes_builder.rb', line 12

def initialize(record, template)
  @record, @template = record, template
end

Instance Attribute Details

#recordObject (readonly) Also known as: object

Only for testing purposes



7
8
9
# File 'lib/manageable/helpers/attributes_builder.rb', line 7

def record
  @record
end

#templateObject (readonly)

Only for testing purposes



7
8
9
# File 'lib/manageable/helpers/attributes_builder.rb', line 7

def template
  @template
end

Instance Method Details

#attribute(*args, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/manageable/helpers/attributes_builder.rb', line 42

def attribute(*args, &block)
  options = args.extract_options!
  options[:html] ||= {}

  method = args.shift

  html_label_class = [ "label", options[:html][:label_class] ].compact.join(" ")
  html_value_class = [ "value", options[:html][:value_class] ].compact.join(" ")

  html_class = [ "attribute", options[:html][:class] ]

  position = options.delete(:position)
  html_class << "column_left"  if position == :left
  html_class << "column_right" if position == :right
  html_class = html_class.compact.join(" ")

  label = options.key?(:label) ? options[:label] : label_for_attribute(method)

  unless block_given?
    value = if options.key?(:value)
      case options[:value]
        when Symbol
          attribute_value = value_of_attribute(method)
          case attribute_value
            when Hash
              attribute_value[options[:value]]
            else
              attribute_value.send(options[:value])
          end
        else
          options[:value]
      end
    else
      value_of_attribute(method)
    end

    value = case options[:format]
      when false
        value
      when nil
        format_attribute_value(value)
      else
        template.send(options[:format], value)
    end

    if value.present? or options[:display_empty]
      output = template.tag(:li, {:class => html_class}, true)
      output << template.(:span, label, :class => html_label_class)
      output << template.(:span, value, :class => html_value_class)
      output.safe_concat("</li>")
    end
  else
    output = template.tag(:li, {:class => html_class}, true)
    output << template.(:span, label, :class => html_label_class)
    output << template.tag(:span, {:class => html_value_class}, true)
    output << template.capture(&block)
    output.safe_concat("</span>")
    output.safe_concat("</li>")
  end
end

#attributes(*args, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/manageable/helpers/attributes_builder.rb', line 16

def attributes(*args, &block)
  options = args.extract_options!
  options[:html] ||= {}

  if args.first and args.first.is_a? String
    options[:name] = args.shift
  end

  if options[:for].blank?
    attributes_for(record, args, options, &block)
  else
    for_value = if options[:for].is_a? Symbol
      record.send(options[:for])
    else
      options[:for]
    end

    [*for_value].map do |value|
      value_options = options.clone
      value_options[:html][:class] = [ options[:html][:class], value.class.to_s.underscore ].compact.join(" ")

      attributes_for(value, args, options, &block)
    end.join.html_safe
  end
end