attrtastic 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,17 +2,27 @@
2
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
3
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
4
  <head>
5
- <meta name="Content-Type" content="text/html; charset=utf-8" />
6
- <title>Top Level Namespace</title>
7
- <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />
8
- <link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Documentation by YARD 0.7.2
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
9
16
 
10
17
  <script type="text/javascript" charset="utf-8">
11
18
  relpath = '';
12
19
  if (relpath != '') relpath += '/';
13
20
  </script>
14
- <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
15
- <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
21
+
22
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
23
+
24
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
25
+
16
26
 
17
27
  </head>
18
28
  <body>
@@ -33,11 +43,14 @@
33
43
  </div>
34
44
 
35
45
  <div id="search">
36
- <a id="class_list_link" href="#">Class List</a>
37
- <a id="method_list_link" href="#">Method List</a>
38
- <a id ="file_list_link" href="#">File List</a>
46
+
47
+ <a id="class_list_link" href="#">Class List</a>
48
+
49
+ <a id="method_list_link" href="#">Method List</a>
50
+
51
+ <a id="file_list_link" href="#">File List</a>
52
+
39
53
  </div>
40
-
41
54
  <div class="clear"></div>
42
55
  </div>
43
56
 
@@ -76,12 +89,14 @@
76
89
 
77
90
 
78
91
 
92
+
93
+
79
94
  </div>
80
95
 
81
96
  <div id="footer">
82
- Generated on Fri Dec 3 21:43:44 2010 by
97
+ Generated on Sat Jun 25 13:32:39 2011 by
83
98
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
84
- 0.6.3 (ruby-1.9.2).
99
+ 0.7.2 (ruby-1.9.2).
85
100
  </div>
86
101
 
87
102
  </body>
@@ -6,6 +6,9 @@ module Attrtastic
6
6
  # Only for testing purposes
7
7
  attr_reader :record, :template
8
8
 
9
+ # For compatibility with formtastic
10
+ alias :object :record
11
+
9
12
  def initialize(record, template)
10
13
  @record, @template = record, template
11
14
  end
@@ -64,6 +67,16 @@ module Attrtastic
64
67
  # <%= post.attribute :title %>
65
68
  # <% end %>
66
69
  #
70
+ # @example
71
+ # <%= attr.attributes :for => @posts do |post| %>
72
+ # <%= post.attribute :birthday, :format => false %>
73
+ # <% end %>
74
+ #
75
+ # @example
76
+ # <%= attr.attributes :for => @posts do |post| %>
77
+ # <%= post.attribute :birthday, :format => :my_fancy_birthday_formatter %>
78
+ # <% end %>
79
+ #
67
80
  # @overload attributes(header, options = {}, &block)
68
81
  # Creates attributes list with header and yields block to include each attribute
69
82
  #
@@ -208,8 +221,14 @@ module Attrtastic
208
221
  # @param [Hash] options Options
209
222
  # @option options [Hash] :html ({}) Hash with optional :class, :label_class and :value_class names of class for html
210
223
  # @option options [String] :label Label for attribute entry, overrides default label name from symbol
211
- # @option options [String] :value Value of attribute entry, overrides default value from record
224
+ # @option options [Symbol,Object] :value If it's Symbol, then it's used as either name of hash key to use on attribute
225
+ # (if it's hash) or method name to call on attribute. Otherwise it's used as value to use instead of
226
+ # actual attribute's.
212
227
  # @option options [Boolean] :display_empty (false) Indicates if print value of given attribute even if it is blank?
228
+ # @option options [Symbol,false,nil] :format (nil) Type of formatter to use to display attribute's value. If it's false,
229
+ # then don't format at all (just call #to_s). If it's nil, then use default formatting (#l for dates,
230
+ # #number_with_precision/#number_with_delimiter for floats/integers). If it's Symbol, then use it to select
231
+ # view helper method and pass aattribute's value to it to format.
213
232
  #
214
233
  # @example
215
234
  # <%= attr.attribute :name %>
@@ -220,6 +239,12 @@ module Attrtastic
220
239
  # @example
221
240
  # <%= attr.attribute :name, :value => @user.full_name %>
222
241
  #
242
+ # @example
243
+ # <%= attr.attribute :address, :value => :street %>
244
+ #
245
+ # @example
246
+ # <%= attr.attribute :avatar, :value => :url, :format => :image_tag %>
247
+ #
223
248
  # @overload attribute(method, options = {}, &block)
224
249
  # Creates entry for attribute given with block
225
250
  #
@@ -265,7 +290,31 @@ module Attrtastic
265
290
  label = options.key?(:label) ? options[:label] : label_for_attribute(method)
266
291
 
267
292
  unless block_given?
268
- value = options.key?(:value) ? options[:value] : value_of_attribute(method)
293
+ value = if options.key?(:value)
294
+ case options[:value]
295
+ when Symbol
296
+ attribute_value = value_of_attribute(method)
297
+ case attribute_value
298
+ when Hash
299
+ attribute_value[options[:value]]
300
+ else
301
+ attribute_value.send(options[:value])
302
+ end
303
+ else
304
+ options[:value]
305
+ end
306
+ else
307
+ value_of_attribute(method)
308
+ end
309
+
310
+ value = case options[:format]
311
+ when false
312
+ value
313
+ when nil
314
+ format_attribute_value(value)
315
+ else
316
+ template.send(options[:format], value)
317
+ end
269
318
 
270
319
  if value.present? or options[:display_empty]
271
320
  output = template.tag(:li, {:class => html_class}, true)
@@ -296,7 +345,9 @@ module Attrtastic
296
345
  header = options[:name]
297
346
 
298
347
  if header.present?
299
- output << template.content_tag(:div, header, :class => html_header_class)
348
+ output << template.content_tag(:div, :class => html_header_class) do
349
+ template.content_tag(:span, header)
350
+ end
300
351
  end
301
352
 
302
353
  if block_given?
@@ -323,11 +374,20 @@ module Attrtastic
323
374
  end
324
375
 
325
376
  def value_of_attribute(method)
326
- value = record.send(method)
327
- value_methods = [ :to_label, :display_name, :full_name, :name, :title, :username, :login, :value ]
328
- value_method = value_methods.find { |m| value.respond_to?(m) } || :to_s
329
- value.send(value_method)
377
+ record.send(method)
330
378
  end
331
379
 
380
+ def format_attribute_value(value)
381
+ case value
382
+ when Date, Time, DateTime
383
+ template.send(:l, value)
384
+ when Integer
385
+ template.send(:number_with_delimiter, value)
386
+ when Float, BigDecimal
387
+ template.send(:number_with_precision, value)
388
+ else
389
+ value.to_s
390
+ end
391
+ end
332
392
  end
333
393
  end
@@ -1,3 +1,3 @@
1
1
  module Attrtastic
2
- VERSION = "0.3.3"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require 'test/unit'
2
2
  require 'shoulda'
3
3
  require 'action_view'
4
+ require 'bigdecimal'
4
5
 
5
6
  $LOAD_PATH.unshift(File.dirname(__FILE__))
6
7
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
@@ -14,12 +15,19 @@ class TestCase < Test::Unit::TestCase
14
15
  @user = User.new.tap do |u|
15
16
  u.first_name,u.last_name = "John","Doe"
16
17
  u.email = "john@doe.com"
18
+ u.created_at = DateTime.parse("2011-06-02 12:06:42")
19
+ u.time = Time.at(946702800)
20
+ u.birthday = Date.parse("1953-06-03")
21
+ u.float = 54424.22
22
+ u.decimal = BigDecimal.new('4454.3435')
23
+ u.integer = 45453
17
24
  end
18
25
  @blog = Blog.new.tap{|b| b.name,b.url,b.author = "IT Pro Blog","http://www.it.pro.blog",@user}
19
26
  @blog.posts = [
20
27
  Post.new.tap{|p| p.title,p.content = "Hello World!","Hello World!\nInitial post"},
21
28
  Post.new.tap{|p| p.title,p.content = "Sorry","Sorry for long delay. Had much stuff on my head..."},
22
29
  ]
30
+ @user.blog = @blog
23
31
 
24
32
  ActionView::Base.send :include, Attrtastic::SemanticAttributesHelper
25
33
  @template = ActionView::Base.new.tap{ |av| av.output_buffer = ActiveSupport::SafeBuffer.new }
@@ -29,7 +37,14 @@ class TestCase < Test::Unit::TestCase
29
37
  end
30
38
 
31
39
  class User
32
- attr_accessor :first_name, :last_name, :email, :title
40
+ attr_accessor :first_name, :last_name, :email, :title, :created_at, :time, :birthday, :float, :decimal, :integer, :blog
41
+
42
+ def address
43
+ {
44
+ :street => "Hellway 13",
45
+ :city => "New York",
46
+ }
47
+ end
33
48
 
34
49
  def full_name
35
50
  [last_name,first_name].join(", ")
@@ -37,19 +37,178 @@ class TestAttribute < TestCase
37
37
  assert_equal expected, actual
38
38
  end
39
39
 
40
- should "convert nested object to value" do
41
- expected = html <<-EOHTML
42
- <li class="attribute">
43
- <span class="label">Author</span>
44
- <span class="value">Doe, John</span>
45
- </li>
46
- EOHTML
40
+ context "with default formating" do
41
+ should "properly format a String" do
42
+ expected = html <<-EOHTML
43
+ <li class="attribute">
44
+ <span class="label">Author</span>
45
+ <span class="value">Doe, John</span>
46
+ </li>
47
+ EOHTML
48
+ actual = @blog_builder.attribute(:author_full_name, :label => "Author")
49
+ assert_equal expected, actual
50
+ end
47
51
 
48
- actual = @blog_builder.attribute(:author_full_name, :label => "Author")
49
- assert_equal expected, actual
52
+ should "properly format a Date" do
53
+ expected = html <<-EOHTML
54
+ <li class="attribute">
55
+ <span class="label">Birthday</span>
56
+ <span class="value">1953-06-03</span>
57
+ </li>
58
+ EOHTML
59
+ actual = @user_builder.attribute(:birthday)
60
+ assert_equal expected, actual
61
+ end
50
62
 
51
- actual = @blog_builder.attribute(:author)
52
- assert_equal expected, actual
63
+ should "properly format a DateTime" do
64
+ expected = html <<-EOHTML
65
+ <li class="attribute">
66
+ <span class="label">Created at</span>
67
+ <span class="value">Thu, 02 Jun 2011 12:06:42 +0000</span>
68
+ </li>
69
+ EOHTML
70
+ actual = @user_builder.attribute(:created_at)
71
+ assert_equal expected, actual
72
+ end
73
+
74
+ should "properly format a Time" do
75
+ expected = html <<-EOHTML
76
+ <li class="attribute">
77
+ <span class="label">Time</span>
78
+ <span class="value">Sat, 01 Jan 2000 06:00:00 +0100</span>
79
+ </li>
80
+ EOHTML
81
+ actual = @user_builder.attribute(:time)
82
+ assert_equal expected, actual
83
+ end
84
+
85
+ should "properly format a Float" do
86
+ expected = html <<-EOHTML
87
+ <li class="attribute">
88
+ <span class="label">Float</span>
89
+ <span class="value">54424.220</span>
90
+ </li>
91
+ EOHTML
92
+ actual = @user_builder.attribute(:float)
93
+ assert_equal expected, actual
94
+ end
95
+
96
+ should "properly format a Decimal" do
97
+ expected = html <<-EOHTML
98
+ <li class="attribute">
99
+ <span class="label">Decimal</span>
100
+ <span class="value">4454.344</span>
101
+ </li>
102
+ EOHTML
103
+ actual = @user_builder.attribute(:decimal)
104
+ assert_equal expected, actual
105
+ end
106
+
107
+ should "properly format a Integer" do
108
+ expected = html <<-EOHTML
109
+ <li class="attribute">
110
+ <span class="label">Integer</span>
111
+ <span class="value">45,453</span>
112
+ </li>
113
+ EOHTML
114
+ actual = @user_builder.attribute(:integer)
115
+ assert_equal expected, actual
116
+ end
117
+ end
118
+
119
+ context "with default formating disabled" do
120
+ should "properly format a String" do
121
+ expected = html <<-EOHTML
122
+ <li class="attribute">
123
+ <span class="label">Author</span>
124
+ <span class="value">Doe, John</span>
125
+ </li>
126
+ EOHTML
127
+ actual = @blog_builder.attribute(:author_full_name, :label => "Author", :format => false)
128
+ assert_equal expected, actual
129
+ end
130
+
131
+ should "properly format a Date" do
132
+ expected = html <<-EOHTML
133
+ <li class="attribute">
134
+ <span class="label">Birthday</span>
135
+ <span class="value">1953-06-03</span>
136
+ </li>
137
+ EOHTML
138
+ actual = @user_builder.attribute(:birthday, :format => false)
139
+ assert_equal expected, actual
140
+ end
141
+
142
+ should "properly format a DateTime" do
143
+ expected = html <<-EOHTML
144
+ <li class="attribute">
145
+ <span class="label">Created at</span>
146
+ <span class="value">2011-06-02T12:06:42+00:00</span>
147
+ </li>
148
+ EOHTML
149
+ actual = @user_builder.attribute(:created_at, :format => false)
150
+ assert_equal expected, actual
151
+ end
152
+
153
+ should "properly format a Time" do
154
+ expected = html <<-EOHTML
155
+ <li class="attribute">
156
+ <span class="label">Time</span>
157
+ <span class="value">2000-01-01 06:00:00 +0100</span>
158
+ </li>
159
+ EOHTML
160
+ actual = @user_builder.attribute(:time, :format => false)
161
+ assert_equal expected, actual
162
+ end
163
+
164
+ should "properly format a Float" do
165
+ expected = html <<-EOHTML
166
+ <li class="attribute">
167
+ <span class="label">Float</span>
168
+ <span class="value">54424.22</span>
169
+ </li>
170
+ EOHTML
171
+ actual = @user_builder.attribute(:float, :format => false)
172
+ assert_equal expected, actual
173
+ end
174
+
175
+ should "properly format a Decimal" do
176
+ expected = html <<-EOHTML
177
+ <li class="attribute">
178
+ <span class="label">Decimal</span>
179
+ <span class="value">4454.3435</span>
180
+ </li>
181
+ EOHTML
182
+ actual = @user_builder.attribute(:decimal, :format => false)
183
+ assert_equal expected, actual
184
+ end
185
+
186
+ should "properly format a Integer" do
187
+ expected = html <<-EOHTML
188
+ <li class="attribute">
189
+ <span class="label">Integer</span>
190
+ <span class="value">45453</span>
191
+ </li>
192
+ EOHTML
193
+ actual = @user_builder.attribute(:integer, :format => false)
194
+ assert_equal expected, actual
195
+ end
196
+ end
197
+
198
+ context "with custom formating" do
199
+ should "output the return value of called template's method" do
200
+ expected = html <<-EOHTML
201
+ <li class="attribute">
202
+ <span class="label">Author</span>
203
+ <span class="value">Hello, my name is Doe, John</span>
204
+ </li>
205
+ EOHTML
206
+ def @template.hello(name)
207
+ "Hello, my name is #{name}"
208
+ end
209
+ actual = @blog_builder.attribute(:author_full_name, :label => "Author", :format => :hello)
210
+ assert_equal expected, actual
211
+ end
53
212
  end
54
213
 
55
214
  should "show custom label" do
@@ -76,6 +235,30 @@ class TestAttribute < TestCase
76
235
  assert_equal expected, actual
77
236
  end
78
237
 
238
+ should "use th custome value as hash key if it's a symbol and the attribute is a hash" do
239
+ expected = html <<-EOHTML
240
+ <li class="attribute">
241
+ <span class="label">Address</span>
242
+ <span class="value">Hellway 13</span>
243
+ </li>
244
+ EOHTML
245
+
246
+ actual = @user_builder.attribute(:address, :value => :street)
247
+ assert_equal expected, actual
248
+ end
249
+
250
+ should "use th custome value as a method it's a symbol and the attribute is not a hash" do
251
+ expected = html <<-EOHTML
252
+ <li class="attribute">
253
+ <span class="label">Blog</span>
254
+ <span class="value">IT Pro Blog</span>
255
+ </li>
256
+ EOHTML
257
+
258
+ actual = @user_builder.attribute(:blog, :value => :name)
259
+ assert_equal expected, actual
260
+ end
261
+
79
262
  should "work with custom value blank" do
80
263
  assert_nil @user_builder.attribute(:full_name, :value => nil)
81
264
  assert_nil @user_builder.attribute(:full_name, :value => "")