Class: Attrtastic::SemanticAttributesBuilder
- Inherits:
-
Object
- Object
- Attrtastic::SemanticAttributesBuilder
- Defined in:
- lib/attrtastic.rb
Instance Attribute Summary
- - (Object) record readonly Only for testing purposes.
- - (Object) template readonly Only for testing purposes.
Instance Method Summary
- - (Object) attribute(*args, &block) Creates list entry for single record attribute.
- - (Object) attributes(*args, &block) Creates block of attributes with optional header.
- - (SemanticAttributesBuilder) initialize(record, template) constructor A new instance of SemanticAttributesBuilder.
Constructor Details
- (SemanticAttributesBuilder) initialize(record, template)
A new instance of SemanticAttributesBuilder
14 15 16 |
# File 'lib/attrtastic.rb', line 14 def initialize(record, template) @record, @template = record, template end |
Instance Attribute Details
- (Object) record (readonly)
Only for testing purposes
12 13 14 |
# File 'lib/attrtastic.rb', line 12 def record @record end |
- (Object) template (readonly)
Only for testing purposes
12 13 14 |
# File 'lib/attrtastic.rb', line 12 def template @template end |
Instance Method Details
- (Object) attribute(method, options = {}) - (Object) attribute(method, options = {}, &block) { ... }
Creates list entry for single record attribute
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/attrtastic.rb', line 162 def attribute(*args, &block) = {} if args.last and args.last.kind_of? Hash = args.last args = args[0 .. -2] end [:html] ||= {} method = args.shift html_label_class = [ "label", [:html][:label_class] ].compact.join(" ") html_value_class = [ "value", [:html][:value_class] ].compact.join(" ") html_class = [ "attribute", [:html][:class] ].compact.join(" ") label = .key?(:label) ? [:label] : label_for_attribute(method) label_content = template.content_tag(:span, label, :class => html_label_class) unless block_given? value = .key?(:value) ? [:value] : value_of_attribute(method) value_content = template.content_tag(:span, value, :class => html_value_class) if value.present? or [:display_empty] content = [ label_content, value_content ].join template.content_tag(:li, content, :class => html_class) end else template.concat(template.tag(:li, {:class => html_class}, true)) template.concat(label_content) template.concat(template.tag(:span, {:class => html_value_class}, true)) yield template.concat("</span>") template.concat("</li>") end end |
- (Object) attributes(options = {}, &block) { ... } - (Object) attributes(header, options = {}, &block) { ... } - (Object) attributes(*symbols, options = {}) - (Object) attributes(header, *symbols, options = {})
Creates block of attributes with optional header. Attributes are surrounded with ordered list.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/attrtastic.rb', line 83 def attributes(*args, &block) = {} if args.last and args.last.kind_of? Hash = args.last args = args[0 .. -2] end [:html] ||= {} html_class = [ "attributes", [:html].delete(:class) ].compact.join(" ") html_header_class = [ "legend", [:html].delete(:header_class) ].compact.join(" ") template.concat(template.tag(:div, {:class => html_class}, true)) if args.first and args.first.is_a? String header = args.shift template.concat(template.content_tag(:div, header, :class => html_header_class)) end if block_given? template.concat(template.tag(:ol, {}, true)) yield template.concat("</ol>") elsif args.present? template.concat(template.tag(:ol, {}, true)) attrs = args.map {|method| attribute(method, )}.compact.join template.concat(attrs) template.concat("</ol>") end template.concat("</div>") end |