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
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/supplejack/controllers/helpers.rb', line 43
def attribute(record, attributes, options={})
options.reverse_merge!(:label => true, :label_inline => true, :limit => nil, :delimiter => ", ",
:link_path => false, :tag => Supplejack.attribute_tag, :label_tag => Supplejack.label_tag,
:label_class => Supplejack.label_class, :trans_key => nil, :link => false,
:extra_html => nil, :tag_class => nil)
value = []
attributes = [attributes] unless attributes.is_a?(Array)
attributes.each do |attribute|
if attribute.is_a?(String) && attribute.match(/\./)
object, attr = attribute.split(".")
v = record.try(object.to_sym).try(attr.to_sym) if object && attr
else
v = record.send(attribute)
end
if v.is_a?(Array)
value += v.compact
else
value << v if v.present?
end
end
value = value.first if value.size == 1
attribute = attributes.first
if value.is_a?(Array)
if options[:limit] && options[:limit].to_i > 0
value = value[0..(options[:limit].to_i-1)]
end
if options[:link_path]
value = value.map {|v| link_to(v, send(options[:link_path], {:i => {attribute => v}})) }
end
if options[:link]
value = value.map do |v|
attribute_link_replacement(v, options[:link])
end
end
value = value.join(options[:delimiter]).html_safe
value = truncate(value, :length => options[:limit]) if options[:limit].to_i > 20
value
else
if options[:limit] && options[:limit].to_i > 0
value = truncate(value, :length => options[:limit].to_i)
end
if options[:link]
value = attribute_link_replacement(value, options[:link])
end
end
content = ""
if options[:label]
if options[:trans_key].present?
translation = I18n.t(options[:trans_key], :default => attribute.to_s.capitalize) + ": "
else
i18n_class_name = record.class.to_s.tableize.downcase.gsub(/\//, "_")
translation = "#{I18n.t("#{i18n_class_name}.#{attribute}", :default => attribute.to_s.capitalize)}: "
end
content = content_tag(options[:label_tag], translation, :class => options[:label_class]).html_safe
content << "<br/>".html_safe unless options[:label_inline]
end
content << value.to_s
content << options[:extra_html] if options[:extra_html]
if value.present? and value != "Not specified"
options[:tag] ? content_tag(options[:tag], content.html_safe, :class => options[:tag_class]) : content.html_safe
end
end
|