view 1.0.0.alpha.3 → 1.0.0.alpha.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +269 -51
- data/lib/view/configuration.rb +55 -0
- data/lib/view/formatter.rb +8 -12
- data/lib/view/formatters/array.rb +1 -1
- data/lib/view/formatters/auto.rb +6 -2
- data/lib/view/formatters/definition_list.rb +110 -0
- data/lib/view/formatters/file_link.rb +1 -1
- data/lib/view/formatters/guess.rb +2 -2
- data/lib/view/formatters/human.rb +1 -0
- data/lib/view/formatters/image.rb +49 -8
- data/lib/view/formatters/link.rb +29 -4
- data/lib/view/formatters/percentage.rb +3 -1
- data/lib/view/formatters/phone.rb +1 -0
- data/lib/view/formatters/precision.rb +3 -1
- data/lib/view/formatters/size.rb +7 -0
- data/lib/view/formatters/table.rb +208 -0
- data/lib/view/helper.rb +27 -0
- data/lib/view/railtie.rb +1 -1
- data/lib/view/version.rb +1 -1
- data/lib/view.rb +28 -56
- data/spec/spec_helper.rb +52 -4
- data/spec/view/configuration_spec.rb +11 -0
- data/spec/view/formatters/currency_spec.rb +2 -6
- data/spec/view/formatters/definition_list_spec.rb +59 -0
- data/spec/view/formatters/delimited_spec.rb +2 -6
- data/spec/view/formatters/html_safe_spec.rb +13 -0
- data/spec/view/formatters/human_spec.rb +9 -0
- data/spec/view/formatters/image_spec.rb +36 -0
- data/spec/view/formatters/link_spec.rb +20 -0
- data/spec/view/formatters/percentage_spec.rb +15 -0
- data/spec/view/formatters/phone_spec.rb +15 -0
- data/spec/view/formatters/precision_spec.rb +15 -0
- data/spec/view/formatters/size_spec.rb +15 -0
- data/spec/view/formatters/table_spec.rb +131 -0
- data/spec/view/helper_spec.rb +26 -4
- metadata +29 -4
data/README.rdoc
CHANGED
@@ -2,79 +2,304 @@
|
|
2
2
|
|
3
3
|
{Source}[http://github.com/iain/view] | {Documentation}[http://rubydoc.info/github/iain/view/master/frames]
|
4
4
|
|
5
|
-
|
5
|
+
View is a DSL to help you make default representations of objects. It's goal is not to add complex
|
6
|
+
logic, but to help you make it really easy to display default elements.
|
6
7
|
|
7
|
-
|
8
|
-
Some of those DSLs are coming soon to a rubygem near you!
|
8
|
+
Some features:
|
9
9
|
|
10
|
-
|
10
|
+
* View will automatically display your objects as you expect them to be.
|
11
|
+
* View is smart about blank objects, so no more checking it in your views.
|
12
|
+
* View contains many formatters, some really complex, and is very extensible.
|
13
|
+
* View allows for exceptions by using the power of blocks.
|
14
|
+
* View is designed to be short and looks at home in HAML.
|
15
|
+
* Optional support for InheritedResources.
|
11
16
|
|
12
|
-
|
17
|
+
This gem was inspired by {formtastic}[http://github.com/justinfrench/formtastic] and
|
18
|
+
{simple_form}[http://github.com/plataformatec/simple_form]. It has been extracted from the helper
|
19
|
+
methods I use regularly myself, but as a gem, View has still a long way to go.
|
13
20
|
|
14
|
-
|
21
|
+
== Installation
|
22
|
+
|
23
|
+
Just add it to your Gemfile:
|
24
|
+
|
25
|
+
gem 'view'
|
26
|
+
|
27
|
+
Run `bundle install` and you're ready to go!
|
28
|
+
|
29
|
+
== Basic Usage
|
15
30
|
|
16
|
-
|
31
|
+
It' usually done through the `view` helper method.
|
32
|
+
|
33
|
+
= view @post
|
17
34
|
|
18
35
|
View will try to figure out what the most logical way is to present the object you passed in.
|
19
36
|
If the @post object has a title for instance, it will use that.
|
20
37
|
|
21
|
-
|
38
|
+
View will automatically try to choose the proper formatter. The following will use I18n for
|
39
|
+
formatting the time correctly:
|
22
40
|
|
23
|
-
|
41
|
+
= view @post.created_at
|
24
42
|
|
25
|
-
|
43
|
+
Normally I18n.localize will throw an exception if the value were to be nil, so you'd have to
|
44
|
+
check for that yourself. View does this for you.
|
26
45
|
|
27
|
-
|
28
|
-
It is a helper for the view part of your application after all.
|
46
|
+
You can specify a formatter yourself:
|
29
47
|
|
30
|
-
|
48
|
+
= view @post.created_at, :as => :date
|
31
49
|
|
32
|
-
|
50
|
+
You can add a block to specify your own behavior. The formatter is passed in as a block-variable.
|
51
|
+
On the formatter the following methods are always available:
|
33
52
|
|
34
|
-
|
53
|
+
* `to_s` gets the formatted result
|
54
|
+
* `value` gets the original value
|
55
|
+
* `options` will return a (filtered) options hash, complete with defaults
|
35
56
|
|
36
|
-
|
57
|
+
Example:
|
37
58
|
|
38
|
-
|
39
|
-
|
40
|
-
<% end %>
|
59
|
+
= view @post.author do |formatter|
|
60
|
+
= link_to formatter.to_s, backend_user_path(formatter.value)
|
41
61
|
|
42
|
-
Most formatters pass their options down to the method they use, such as the
|
62
|
+
Most formatters pass their options down to the method they use, such as the date formatter:
|
43
63
|
|
44
|
-
|
64
|
+
= view @post.created_at, :format => :short
|
45
65
|
|
46
66
|
== Examples
|
47
67
|
|
48
|
-
|
68
|
+
Now that we've got the basics down, let's continue with some more advanced examples.
|
69
|
+
There are a lot of formatters that do very trivial things. This is because they are meant to be
|
70
|
+
used inside other formatters. Let's start with some bigger formatters to give you an idea.
|
71
|
+
|
72
|
+
=== Table
|
73
|
+
|
74
|
+
You can render a table quite easily. If you're displaying a collection of ActiveRecord objects,
|
75
|
+
it's even trivial:
|
76
|
+
|
77
|
+
= view @posts, :as => :table
|
78
|
+
|
79
|
+
or even:
|
80
|
+
|
81
|
+
= table_for @posts
|
82
|
+
|
83
|
+
This will display all columns, properly formatted, as you would expect. All columns might be a bit
|
84
|
+
too much, so let's slim the table down:
|
85
|
+
|
86
|
+
= table_for @posts, :fields => [ :title, :author, :published_at ]
|
87
|
+
|
88
|
+
Turn the titles into links:
|
89
|
+
|
90
|
+
= table_for @posts, :fields => [ :title, :author, :published_at ], :link => :title
|
91
|
+
|
92
|
+
Ok, nice, but this is rather ugly. This list might get very long. Also, what if you want to
|
93
|
+
control how certain columns are formatted? The solution is simple: use a block:
|
94
|
+
|
95
|
+
= table_for @posts do |table|
|
96
|
+
|
97
|
+
= table.view :title, :as => :link
|
98
|
+
|
99
|
+
= table.view :author do |formatter|
|
100
|
+
= link_to formatter.to_s, formatter.value
|
101
|
+
|
102
|
+
= table.view :published_at, :as => :date
|
103
|
+
|
104
|
+
Now the power of the simple formatters can be fully appreciated.
|
105
|
+
|
106
|
+
The table formatter will display a simple table. If you want to control what the table looks like,
|
107
|
+
you can add a partial to your application in `app/views/shared/_table.html.haml` (or erb). Since
|
108
|
+
this gem is a Rails 3 engine, it will automatically choose your own version over the one provided
|
109
|
+
by this gem.
|
110
|
+
|
111
|
+
One final note on tables: If you're using the gem
|
112
|
+
{InheritedResources}[http://github.com/josevalim/inherited_resources], or something similar, you
|
113
|
+
can just call `table` from the index view, and it will automatically use the collection.
|
114
|
+
|
115
|
+
= table
|
116
|
+
|
117
|
+
See the {documentation}[http://rubydoc.info/github/iain/view/master/View/Table] for more
|
118
|
+
information on tables.
|
119
|
+
|
120
|
+
=== Definition lists
|
121
|
+
|
122
|
+
You can generate definition lists in rather the same fashion as tables.
|
123
|
+
Definition Lists are great for the show pages in a backend.
|
124
|
+
|
125
|
+
On it's own:
|
126
|
+
|
127
|
+
= view @post, :as => :definition_list
|
128
|
+
|
129
|
+
Or even:
|
130
|
+
|
131
|
+
= definition_list_for @post
|
132
|
+
|
133
|
+
Or if you're using InheritedResources:
|
134
|
+
|
135
|
+
= definition_list
|
136
|
+
|
137
|
+
You can also specify fields:
|
138
|
+
|
139
|
+
= definition_list_for @post, :fields => [ :title, :author ]
|
140
|
+
|
141
|
+
And you can also use a block to go specific:
|
142
|
+
|
143
|
+
= definition_list_for @post do |dl|
|
144
|
+
|
145
|
+
= dl.view :title, :as => :link
|
146
|
+
= dl.view :author
|
147
|
+
= dl.view :published_on, :as => :date
|
148
|
+
|
149
|
+
See the {documentation}[http://rubydoc.info/github/iain/view/master/View/DefinitionList] for more
|
150
|
+
information on definition lists.
|
151
|
+
|
152
|
+
=== Other formatters
|
153
|
+
|
154
|
+
No we've seen the biggest formatters, let's look at some smaller ones.
|
155
|
+
|
156
|
+
Some of these are longer than their regular Rails counterparts. In that case, remember that
|
157
|
+
they can be easily used inside the table or definition list. Most of the common Rails helpers
|
158
|
+
have been converted to formatters for that reason.
|
159
|
+
|
160
|
+
==== Links
|
161
|
+
|
162
|
+
Links can be quite easy:
|
163
|
+
|
164
|
+
= view @post, :as => :link
|
165
|
+
|
166
|
+
You can provide options too:
|
167
|
+
|
168
|
+
= view @post, :as => :link, :method => :delete, :confirm => "are you sure?"
|
169
|
+
|
170
|
+
And you can link to the specific versions of the page, using the awesome power of Rails'
|
171
|
+
`polymorphic_path`.
|
172
|
+
|
173
|
+
= view @post, :as => :link, :path => [ :edit, :backend ]
|
49
174
|
|
50
|
-
|
51
|
-
view @post, :as => :link, :method => :delete, :confirm => "are you sure?"
|
175
|
+
{Documentation}[http://rubydoc.info/github/iain/view/master/View/Link]
|
52
176
|
|
53
|
-
|
54
|
-
view @post, :as => :link, :path => [ :edit ]
|
177
|
+
==== Sentences
|
55
178
|
|
56
|
-
|
57
|
-
view Post.all, :each => { :as => :link }
|
179
|
+
Create a simple sentence of an array, propery formatting each element:
|
58
180
|
|
59
|
-
|
60
|
-
|
181
|
+
= view @post.tags, :as => :sentence
|
182
|
+
|
183
|
+
Or even create a sentence of links:
|
184
|
+
|
185
|
+
= view @post.tags, :as => :sentence, :each => { :as => :link }
|
186
|
+
|
187
|
+
{Documentation}[http://rubydoc.info/github/iain/view/master/View/Sentence]
|
188
|
+
|
189
|
+
==== Images and file links
|
190
|
+
|
191
|
+
There is also some support for uploaded files with paperclip.
|
192
|
+
|
193
|
+
The following displays the user's (resized) avatar:
|
194
|
+
|
195
|
+
= view @user.avatar, :as => :image, :style => :thumbnail
|
196
|
+
|
197
|
+
Or, if it isn't an image:
|
198
|
+
|
199
|
+
= view @project.document, :as => :file_link
|
200
|
+
|
201
|
+
{Image Documentation}[http://rubydoc.info/github/iain/view/master/View/Image]
|
202
|
+
{FileLink Documentation}[http://rubydoc.info/github/iain/view/master/View/FileLink]
|
203
|
+
|
204
|
+
==== Booleans
|
205
|
+
|
206
|
+
Booleans are automatically determined and converted into "Yes" and "No".
|
61
207
|
|
62
|
-
# Renders "yes" or "no" (with I18n support!)
|
63
208
|
view @user.admin?
|
64
209
|
|
65
|
-
|
210
|
+
You can change the text via I18n:
|
211
|
+
|
212
|
+
en:
|
213
|
+
view:
|
214
|
+
booleans:
|
215
|
+
'true': Yup
|
216
|
+
'false': Nope
|
217
|
+
|
218
|
+
{Documentation}[http://rubydoc.info/github/iain/view/master/View/Boolean]
|
219
|
+
|
220
|
+
==== Blank values
|
221
|
+
|
222
|
+
Blank values are usually caught before any other formatter. So the Blank formatter can be used
|
223
|
+
even if you chose another.
|
224
|
+
|
225
|
+
So if the post hasn't been published, it will use the blank formatter.
|
226
|
+
|
227
|
+
= view @post.published_at
|
228
|
+
|
229
|
+
Normally, the blank formatter doesn't output anything. You can change that by using I18n:
|
230
|
+
|
231
|
+
en:
|
232
|
+
view:
|
233
|
+
blank: "<em>~</em>"
|
234
|
+
|
235
|
+
|
236
|
+
{Documentation}[http://rubydoc.info/github/iain/view/master/View/Blank]
|
237
|
+
|
238
|
+
== Configuration
|
239
|
+
|
240
|
+
There a lots of ways to configure the gem itself and individual formatters too! Let's go over
|
241
|
+
the most important ones:
|
242
|
+
|
243
|
+
The `guess` formatter looks for methods it considers to be safe to display. The default list is
|
244
|
+
|
245
|
+
* `to_label`
|
246
|
+
* `display_name`
|
247
|
+
* `full_name`
|
248
|
+
* `title`
|
249
|
+
* `username`
|
250
|
+
* `login`
|
251
|
+
* `value`
|
252
|
+
* `to_s`
|
253
|
+
|
254
|
+
It will use the first one it can find. If you want to add a method to that list, you can add
|
255
|
+
a file in your initializers and write:
|
256
|
+
|
257
|
+
View.configure do |config|
|
258
|
+
config.guessing_methods = [ :my, :own, :methods ]
|
259
|
+
end
|
260
|
+
|
261
|
+
If you wrote your own formatter that you would rather use, instead of the `auto` formatter:
|
262
|
+
|
263
|
+
View.configure do |config|
|
264
|
+
config.default_formatter = :my_own_formatter
|
265
|
+
end
|
266
|
+
|
267
|
+
There is also a default formatter for arrays, which you can change like this:
|
268
|
+
|
269
|
+
View.configure do |config|
|
270
|
+
config.default_list_formatter = :sentence
|
271
|
+
end
|
272
|
+
|
273
|
+
{Configuration}[http://rubydoc.info/github/iain/view/master/View/Configuration]
|
274
|
+
|
275
|
+
You might have a special object, that you want to be rendered with your favorite formatter.
|
276
|
+
You can let the auto formatter know about it. Say you want to display all numbers as delimited
|
277
|
+
values (using the `number_with_delimiter` helper from Rails):
|
278
|
+
|
279
|
+
View::Auto.add :delimited do
|
280
|
+
value.is_a?(Fixnum)
|
281
|
+
end
|
282
|
+
|
283
|
+
{Auto formatter}[http://rubydoc.info/github/iain/view/master/View/Auto]
|
284
|
+
|
285
|
+
You can also customize formatters default options. To change every unit in currency,
|
286
|
+
you can use this:
|
287
|
+
|
288
|
+
View::Currency.default_options = { :unit => "Cubits", :format => "%n %s" }
|
289
|
+
|
290
|
+
Remember that the Rails number formatters can also be configured using I18n.
|
66
291
|
|
67
292
|
== Adding formatters
|
68
293
|
|
69
|
-
You can add a formatter by inheriting from
|
70
|
-
The only method you need to implement is the
|
294
|
+
You can add a formatter by inheriting from `View::Formatter`.
|
295
|
+
The only method you need to implement is the `format` method.
|
71
296
|
|
72
297
|
In the class you have access to the following methods:
|
73
298
|
|
74
|
-
*
|
75
|
-
*
|
76
|
-
*
|
77
|
-
*
|
299
|
+
* `value`: the object passed in
|
300
|
+
* `options`: a filtered hash of options
|
301
|
+
* `all_options`: the unfiltered hash
|
302
|
+
* `template`: call methods like link_to on this
|
78
303
|
|
79
304
|
If you wanted a uppercase formatter for example, you could do this:
|
80
305
|
|
@@ -88,7 +313,7 @@ If you wanted a uppercase formatter for example, you could do this:
|
|
88
313
|
|
89
314
|
The name of the formatter is automatically infered from the name of the class.
|
90
315
|
|
91
|
-
You can use the
|
316
|
+
You can use the `.as` method to specify a different name.
|
92
317
|
|
93
318
|
class Foo < View::Formatter
|
94
319
|
as :bar
|
@@ -102,7 +327,7 @@ You can control which options are allowed, by adding reserved options:
|
|
102
327
|
end
|
103
328
|
|
104
329
|
Now, the options method will return the options passed by the user, minus foo and bar.
|
105
|
-
To use them, in your code, use the
|
330
|
+
To use them, in your code, use the `all_options` method.
|
106
331
|
This is done to easily pass the options to another method, without cluttering:
|
107
332
|
|
108
333
|
class Paragraph < View::Formatter
|
@@ -122,22 +347,15 @@ To more tightly control which options are allowed, specify the allowed options:
|
|
122
347
|
|
123
348
|
You can use the existing formatters as examples.
|
124
349
|
|
125
|
-
|
126
|
-
|
127
|
-
See +lib/view.rb+ for information on configuration.
|
128
|
-
|
129
|
-
== Installation
|
130
|
-
|
131
|
-
Just add it to your Gemfile:
|
132
|
-
|
133
|
-
gem 'view'
|
134
|
-
|
135
|
-
Run +bundle install+ and you're ready to go!
|
350
|
+
{Documentation}[http://rubydoc.info/github/iain/view/master/View/Formatter]
|
136
351
|
|
137
352
|
== Contributing
|
138
353
|
|
139
354
|
Yes please! You know the drill: fork, commit, pull request, profit!
|
140
355
|
|
356
|
+
I'm especially interested in ideas for formatters and new use cases.
|
357
|
+
Use the {issue tracker}[http://github.com/iain/view/issues] to let me know!
|
358
|
+
|
141
359
|
== License
|
142
360
|
|
143
361
|
Copyright 2010 Iain Hecker (iain@iain.nl), released under the MIT License.
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module View
|
2
|
+
|
3
|
+
# The central place for all configuration.
|
4
|
+
class Configuration
|
5
|
+
|
6
|
+
# Used by the guess filter, these are the methods used to format an object.
|
7
|
+
# It will use the first method that the object responds to.
|
8
|
+
attr_accessor :guessing_methods
|
9
|
+
|
10
|
+
# To determine if something is an uploaded image, it checks
|
11
|
+
# to see whether it responds to one of these methods.
|
12
|
+
attr_accessor :file_methods
|
13
|
+
|
14
|
+
# To show an image, it will use one of these methods to get the src
|
15
|
+
# attribute.
|
16
|
+
attr_accessor :path_methods
|
17
|
+
|
18
|
+
# You can set a default argument for rendering images, like the style or size
|
19
|
+
# of the image. It will be passed to one of the path_methods.
|
20
|
+
attr_accessor :path_arguments
|
21
|
+
|
22
|
+
# Which formatter will be used by default. The default is auto, which does
|
23
|
+
# all sorts of interesting stuff to see what to do.
|
24
|
+
attr_accessor :default_formatter
|
25
|
+
|
26
|
+
# The auto formatter will choose this formatter by default when your value is
|
27
|
+
# an array (or really, something that responds to +each+).
|
28
|
+
attr_accessor :default_list_formatter
|
29
|
+
|
30
|
+
# Shorthand for configuring this gem.
|
31
|
+
#
|
32
|
+
# @example In +config/initializers/view.rb+
|
33
|
+
#
|
34
|
+
# View.configure do |config|
|
35
|
+
# config.default_formatter = :self
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# @yield [config] The View module
|
39
|
+
def configure
|
40
|
+
yield self
|
41
|
+
end
|
42
|
+
|
43
|
+
def initialize
|
44
|
+
self.guessing_methods = %w|to_label display_name full_name name title
|
45
|
+
username login value to_s|
|
46
|
+
self.file_methods = %w|file? mounted_as public_filename|
|
47
|
+
self.path_methods = %w|url mounted_as public_filename|
|
48
|
+
self.path_arguments = []
|
49
|
+
self.default_formatter = :auto
|
50
|
+
self.default_list_formatter = :sentence
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
data/lib/view/formatter.rb
CHANGED
@@ -9,7 +9,7 @@ module View
|
|
9
9
|
self.default_options = {}
|
10
10
|
|
11
11
|
class_inheritable_array :reserved_options
|
12
|
-
self.reserved_options = [ :as
|
12
|
+
self.reserved_options = [ :as ]
|
13
13
|
|
14
14
|
class_inheritable_array :allowed_options
|
15
15
|
self.allowed_options = []
|
@@ -47,7 +47,7 @@ module View
|
|
47
47
|
|
48
48
|
# By default, blank values (nil, empty strings, etc), will override any
|
49
49
|
# formatter you specified. This way empty values are handled globally.
|
50
|
-
#
|
50
|
+
#
|
51
51
|
# If you don't want this, you can either turn it off per formatter.
|
52
52
|
# Call this method to turn it off.
|
53
53
|
#
|
@@ -81,7 +81,7 @@ module View
|
|
81
81
|
# It's generally a good idea to black list options that you use inside your
|
82
82
|
# formatter.
|
83
83
|
#
|
84
|
-
# The
|
84
|
+
# The option +:as+ is black listed by default.
|
85
85
|
#
|
86
86
|
# @example White listing:
|
87
87
|
#
|
@@ -148,7 +148,7 @@ module View
|
|
148
148
|
end
|
149
149
|
|
150
150
|
def self.formatters
|
151
|
-
View.formatters
|
151
|
+
View.formatters
|
152
152
|
end
|
153
153
|
|
154
154
|
def initialize(value, options = {}, template = nil, &block)
|
@@ -160,7 +160,7 @@ module View
|
|
160
160
|
|
161
161
|
def formatted_value
|
162
162
|
formatter_not_found unless formatter
|
163
|
-
formatter.new(value, all_options, template, &block).format!
|
163
|
+
formatter.new(value, all_options, template, &block).send(:format!)
|
164
164
|
end
|
165
165
|
|
166
166
|
def template_can_capture?
|
@@ -176,11 +176,11 @@ module View
|
|
176
176
|
end
|
177
177
|
|
178
178
|
def captured_value_by_template
|
179
|
-
template.capture(self,
|
179
|
+
template.capture(self, &block)
|
180
180
|
end
|
181
181
|
|
182
182
|
def captured_return_value
|
183
|
-
block.call(self
|
183
|
+
block.call(self)
|
184
184
|
end
|
185
185
|
|
186
186
|
def formatter
|
@@ -204,12 +204,8 @@ module View
|
|
204
204
|
end
|
205
205
|
end
|
206
206
|
|
207
|
-
def block_arguments
|
208
|
-
all_options[:block_arguments] || []
|
209
|
-
end
|
210
|
-
|
211
207
|
def as
|
212
|
-
all_options[:as] || View.default_formatter
|
208
|
+
all_options[:as] || View.configuration.default_formatter
|
213
209
|
end
|
214
210
|
|
215
211
|
def blank_formatter
|
data/lib/view/formatters/auto.rb
CHANGED
@@ -43,7 +43,7 @@ module View
|
|
43
43
|
as ? as[:formatter] : :guess
|
44
44
|
end
|
45
45
|
|
46
|
-
add View.default_list_formatter do
|
46
|
+
add View.configuration.default_list_formatter do
|
47
47
|
value.respond_to?(:each)
|
48
48
|
end
|
49
49
|
|
@@ -52,13 +52,17 @@ module View
|
|
52
52
|
end
|
53
53
|
|
54
54
|
add :file_link do
|
55
|
-
View.file_methods.any? { |method| value.respond_to?(method) }
|
55
|
+
View.configuration.file_methods.any? { |method| value.respond_to?(method) }
|
56
56
|
end
|
57
57
|
|
58
58
|
add :link do
|
59
59
|
all_options.has_key?(:to)
|
60
60
|
end
|
61
61
|
|
62
|
+
add :self do
|
63
|
+
value.is_a?(String)
|
64
|
+
end
|
65
|
+
|
62
66
|
add :blank do
|
63
67
|
value.blank?
|
64
68
|
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
module View
|
2
|
+
|
3
|
+
# Creates a simple definition list for an object.
|
4
|
+
# This works well with ActiveRecord objects.
|
5
|
+
# Without a block it will render all fields. You can specify the fields
|
6
|
+
# using the `fields` option.
|
7
|
+
#
|
8
|
+
# You can use a block, to further format the way the definition items are
|
9
|
+
# formatted.
|
10
|
+
#
|
11
|
+
# @example Without a block
|
12
|
+
#
|
13
|
+
# = view @post, :as => :definition_list, :fields => [ :title, :author ]
|
14
|
+
#
|
15
|
+
# @example With a block
|
16
|
+
#
|
17
|
+
# = view @post, :as => :definition_list do |dl|
|
18
|
+
# = dl.view :title
|
19
|
+
# = dl.view :author
|
20
|
+
# = dl.view :published_at, :as => :date
|
21
|
+
#
|
22
|
+
class DefinitionList < Formatter
|
23
|
+
|
24
|
+
self.reserved_options = [ :fields ]
|
25
|
+
|
26
|
+
# This will add the dt and dd tags.
|
27
|
+
#
|
28
|
+
# @param [Symbol] attribute The name of the attribute to be called on +value+
|
29
|
+
# @param [Hash] options These will be delegated to the formatting of the
|
30
|
+
# attribute's value.
|
31
|
+
# @yield [formatter] The block will be used for formatting the attribute's
|
32
|
+
# value
|
33
|
+
def view(attribute, options = {}, &block)
|
34
|
+
tag = Attribute.new(value, attribute, block, template, options)
|
35
|
+
tag.dt + tag.dd
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
|
40
|
+
def format!
|
41
|
+
template.content_tag(:dl, super, options)
|
42
|
+
end
|
43
|
+
|
44
|
+
def format
|
45
|
+
unless block
|
46
|
+
fields.inject("".html_safe) do |html, field|
|
47
|
+
html << view(field)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def fields
|
53
|
+
all_options[:fields] || all_fields
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def all_fields
|
59
|
+
value_class.respond_to?(:column_names) ? value_class.column_names : []
|
60
|
+
end
|
61
|
+
|
62
|
+
def value_class
|
63
|
+
value.class
|
64
|
+
end
|
65
|
+
|
66
|
+
# Helper class for definition lists
|
67
|
+
class Attribute < Struct.new(:resource, :attribute, :block, :template, :options)
|
68
|
+
|
69
|
+
def dt
|
70
|
+
tag :dt, name
|
71
|
+
end
|
72
|
+
|
73
|
+
def dd
|
74
|
+
tag :dd, value
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def tag(type, value)
|
80
|
+
template.content_tag(type, value, html_options)
|
81
|
+
end
|
82
|
+
|
83
|
+
def name
|
84
|
+
if klass.respond_to?(:human_attribute_name)
|
85
|
+
klass.human_attribute_name(attribute)
|
86
|
+
else
|
87
|
+
attribute.to_s.titleize
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def klass
|
92
|
+
resource.class
|
93
|
+
end
|
94
|
+
|
95
|
+
def value
|
96
|
+
View.format(original_value, options, template, &block)
|
97
|
+
end
|
98
|
+
|
99
|
+
def original_value
|
100
|
+
resource.send(attribute)
|
101
|
+
end
|
102
|
+
|
103
|
+
def html_options
|
104
|
+
{ :class => attribute }
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
end
|
@@ -3,11 +3,11 @@ module View
|
|
3
3
|
# This formatter is the fallback formatter for auto. It figures out which
|
4
4
|
# method will be called to get a proper version to render.
|
5
5
|
#
|
6
|
-
# @see View
|
6
|
+
# @see View::Configuration#guessing_methods
|
7
7
|
class Guess < Formatter
|
8
8
|
|
9
9
|
def format
|
10
|
-
View.guessing_methods.each do |method|
|
10
|
+
View.configuration.guessing_methods.each do |method|
|
11
11
|
return value.send(method) if value.respond_to?(method)
|
12
12
|
end
|
13
13
|
value
|
@@ -2,6 +2,7 @@ module View
|
|
2
2
|
|
3
3
|
# The number_to_human helper was introduced in Rails 3.
|
4
4
|
# It will give results like "10 thousand"
|
5
|
+
# @see http://rubydoc.info/docs/rails/3.0.1/ActionView/Helpers/NumberHelper:number_to_human
|
5
6
|
class Human < Formatter
|
6
7
|
|
7
8
|
self.allowed_options = [ :locale, :precision, :significant, :separator, :delimiter,
|