Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 151 lines (107 sloc) 4.653 kB
b522cc9 @josevalim Initial commit.
josevalim authored
1 == ShowFor
2
818ee1e @josevalim Massive refactoring.
josevalim authored
3 ShowFor allows you to quickly show a model information with I18n features.
4
5 <% show_for @user do |u| %>
6 <%= u.attribute :name %>
7 <%= u.attribute :confirmed? %>
8 <%= u.attribute :created_at, :format => :short %>
9 <%= u.attribute :last_sign_in_at, :if_blank => "User did not access yet"
10 :wrapper_html => { :id => "sign_in_timestamp" } %>
11
12 <% u.attribute :photo do
13 <%= image_url(@user.photo_url) %>
14 end %>
15
16 <%= u.association :company %>
17 <%= u.association :tags, :to_sentence => true %>
18 <% end %>
b522cc9 @josevalim Initial commit.
josevalim authored
19
20 == Installation
21
22 Install the gem:
23
24 sudo gem install show_for
25
26 Configure simple_form gem inside your app:
27
28 config.gem 'show_for'
29
30 Run the generator:
31
32 ruby script/generate show_for
33
34 And you're ready to go.
35
818ee1e @josevalim Massive refactoring.
josevalim authored
36 == Usage
37
38 ShowFor allows you to quickly show a model information with I18n features.
39
40 <% show_for @admin do |a| %>
41 <%= a.attribute :name %>
42 <%= a.attribute :confirmed? %>
43 <%= a.attribute :created_at, :format => :short %>
44 <%= a.attribute :last_sign_in_at, :if_blank => "Administrator did not access yet"
45 :wrapper_html => { :id => "sign_in_timestamp" } %>
46
47 <% a.attribute :photo do %>
48 <%= image_url(@admin.photo_url) %>
49 <% end %>
50 <% end %>
51
52 Will generate something like:
53
54 <div id="admin_1" class="show_for admin">
55 <p id="admin_name" class="wrapper">
56 <b class="label">Name</b><br />
57 José Valim
58 </p>
59 <p id="admin_confirmed" class="wrapper">
60 <b class="label">Confirmed?</b><br />
61 Yes
62 </p>
63 <p id="admin_created_at" class="wrapper">
64 <b class="label">Created at</b><br />
65 13/12/2009 - 19h17
66 </p>
67 <p id="sign_in_timestamp" class="wrapper">
68 <b class="label">Last sign in at</b><br />
69 Administrator did not access yet
70 </p>
71 <p id="admin_photo" class="wrapper">
72 <b class="label">Photo</b><br />
73 <img src="path/to/photo" />
74 </p>
75 </div>
76
77 == Value lookup
78
79 To show the proper value, before retrieving the attribute value, show_for first looks if a
80 block without argument was given, otherwise checks if a :"human_#{attribute}" method is defined
81 and, if not, only then retrieve the attribute.
82
83 == Options
84
85 show_for handles a series of options. Those are:
86
87 * :escape * - When the attribute should be escaped. True by default.
88
89 * :format * - Sent to I18n.localize when the attribute is a date/time object.
90
91 * :if_blank * - An object to be used if the value is blank. Not escaped as well.
92
93 Besides, all containers (:label, :content and :wrapper) can have their html
94 options configured through the :label_html, :content_html and :wrapper_html
95 options. Containers can have their tags configured on demand as well through
96 :label_tag, :content_tag and :wrapper_tag options.
97
98 == Label
99
100 show_for also exposes the label method. In case you want to use the default
101 human_attribute_name lookup and the default wrapping:
102
103 a.label :name #=> <b class="label">Name</b>
104 a.label "Name", :id => "my_name" #=> <b class="label" id="my_name">Name</b>
105
106 == Associations
107
108 show_for also supports associations.
109
110 <% show_for @artwork do |a| %>
111 <%= a.association :artist %>
112 <%= a.association :artist, :method => :name_with_title %>
113
114 <%= a.association :tags %>
115 <%= a.association :tags, :to_sentence => true %>
116 <% a.association :tags do
117 @artwork.tags.map(&:name).to_sentence
118 end %>
119
120 <% a.association :fans, :collection_tag => :ol do |fan| %>
121 <li><%= link_to fan.name, fan %></li>
122 <% end %>
123 <% end %>
124
125 The first is a has_one or belongs_to association, which works like an attribute
126 to show_for, except it will retrieve the artist association and try to find a
127 proper method from ShowFor.association_methods to be used. You can pass
128 the option :method to tell (and not guess) which method from the association
129 to use.
130
131 :tags is a has_and_belongs_to_many association which will return a collection.
132 show_for can handle collections by default by wrapping them in list (<ul> with
133 each item wrapped by an <li>). However, it also allows you to give :to_sentence
134 or :join it you want to render them inline.
135
136 You can also pass a block which expects an argument to association. In such cases,
137 a wrapper for the collection is still created and the block just iterates over the
138 collection objects.
139
b522cc9 @josevalim Initial commit.
josevalim authored
140 == Contributors
141
142 * José Valim (http://github.com/josevalim)
143
144 == Bugs and Feedback
145
146 If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
147
148 http://github.com/plataformatec/show_for/issues
149
150 MIT License. Copyright 2009 Plataforma Tecnologia. http://blog.plataformatec.com.br
Something went wrong with that request. Please try again.