Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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