Class: Attrtastic::SemanticAttributesBuilder
- Inherits:
-
Object
- Object
- Attrtastic::SemanticAttributesBuilder
- Defined in:
- lib/attrtastic/semantic_attributes_builder.rb
Instance Attribute Summary (collapse)
-
- (Object) record
(also: #object)
readonly
Only for testing purposes.
-
- (Object) template
readonly
Only for testing purposes.
Instance Method Summary (collapse)
-
- (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
12 13 14 |
# File 'lib/attrtastic/semantic_attributes_builder.rb', line 12 def initialize(record, template) @record, @template = record, template end |
Instance Attribute Details
- (Object) record (readonly) Also known as: object
Only for testing purposes
7 8 9 |
# File 'lib/attrtastic/semantic_attributes_builder.rb', line 7 def record @record end |
- (Object) template (readonly)
Only for testing purposes
7 8 9 |
# File 'lib/attrtastic/semantic_attributes_builder.rb', line 7 def template @template end |
Instance Method Details
- (Object) attribute(method, options = {}) - (Object) attribute(method, options = {}) { ... } - (Object) attribute(options = {}) { ... }
Creates list entry for single record attribute
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/attrtastic/semantic_attributes_builder.rb', line 280 def attribute(*args, &block) = args. [: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) unless block_given? value = if .key?(:value) case [:value] when Symbol attribute_value = value_of_attribute(method) case attribute_value when Hash attribute_value[[:value]] else attribute_value.send([:value]) end else [:value] end else value_of_attribute(method) end value = case [:format] when false value when nil format_attribute_value(value) else template.send([:format], value) end if value.present? or [:display_empty] output = template.tag(:li, {:class => html_class}, true) output << template.content_tag(:span, label, :class => html_label_class) output << template.content_tag(:span, value, :class => html_value_class) output.safe_concat("</li>") end else output = template.tag(:li, {:class => html_class}, true) output << template.content_tag(: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 |
- (Object) attributes(options = {}) { ... } - (Object) attributes(header, options = {}) {|builder| ... } - (Object) attributes(*symbols, options = {}) - (Object) attributes(header, *symbols, options = {})
Creates block of attributes with optional header. Attributes are surrounded with ordered list.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/attrtastic/semantic_attributes_builder.rb', line 187 def attributes(*args, &block) = args. [:html] ||= {} if args.first and args.first.is_a? String [:name] = args.shift end if [:for].blank? attributes_for(record, args, , &block) else for_value = if [:for].is_a? Symbol record.send([:for]) else [:for] end [*for_value].map do |value| = .clone [:html][:class] = [ [:html][:class], value.class.to_s.underscore ].compact.join(" ") attributes_for(value, args, , &block) end.join.html_safe end end |