show_for 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +117 -0
- data/MIT-LICENSE +1 -1
- data/README.md +225 -0
- data/lib/generators/show_for/templates/show.html.slim +8 -0
- data/lib/generators/show_for/templates/show_for.rb +15 -0
- data/lib/show_for.rb +4 -1
- data/lib/show_for/association.rb +2 -8
- data/lib/show_for/builder.rb +10 -1
- data/lib/show_for/content.rb +7 -10
- data/lib/show_for/version.rb +1 -1
- data/test/association_test.rb +87 -27
- data/test/attribute_test.rb +52 -37
- data/test/builder_test.rb +31 -9
- data/test/content_test.rb +7 -0
- data/test/generators/show_for_generator_test.rb +1 -1
- data/test/label_test.rb +8 -8
- data/test/support/misc_helpers.rb +4 -4
- data/test/support/models.rb +4 -3
- data/test/test_helper.rb +8 -1
- data/test/value_test.rb +37 -36
- metadata +6 -4
- data/CHANGELOG.rdoc +0 -100
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf7d8896e8ef6cf4029dec5b1be980ab4fdda13c
|
4
|
+
data.tar.gz: 8bcebe6966ece81c1485ce5fb94b691601817433
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f87342c9090ec41e3e52e12a2aa5b78f54707060c26240b23d91f50a2f1883aa3c3e95c87b6d53064bf9c1168e2f12105db5c7c16caef43a5b9f1d0c287ec74
|
7
|
+
data.tar.gz: 90ae998a6a90834359f9df100d55e7fb525a3e5d97ecc0b90e6c70fcb311f93b9a6cfd2aee8e2931ef18448f9c46017298577306b337829562c10d8b4a5c77de
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
## 0.4.0
|
2
|
+
|
3
|
+
### enhancements
|
4
|
+
* Change default `wrapper_tag` to generate a `div` instead of `p`. The `p` tag generates
|
5
|
+
invalid HTML with collections: `p > ul`, and failed tests on Rails 4.2. If you depend
|
6
|
+
on the `p` tag being generated, change your Show For config to set `wrapper_tag` to `:p`.
|
7
|
+
* Remove deprecated `:method` in favor of `:using`.
|
8
|
+
* Improve support to Rails 4.x associations with more duck typing instead of Array checks.
|
9
|
+
* Support Rails 4.1/4.2 and Ruby 2.1/2.2.
|
10
|
+
* Add `skip_blanks` configuration option to skip generating blank attributes
|
11
|
+
instead of generating them with a default message. (by github.com/moktin)
|
12
|
+
* Add `slim` template for the install generator. (by github.com/voanhduy1512)
|
13
|
+
* Add `separator` option that overrides the global configuration (by github.com/bernardofire)
|
14
|
+
|
15
|
+
### bugfix
|
16
|
+
* Do not yield default blank value to the block when using an empty association. (by github.com/aptinio)
|
17
|
+
|
18
|
+
## 0.3.0
|
19
|
+
|
20
|
+
### bug fix
|
21
|
+
* Fix blank value not being applied when using a block. (by github.com/tfwright)
|
22
|
+
|
23
|
+
## 0.3.0.rc
|
24
|
+
|
25
|
+
### enhancements
|
26
|
+
* Support Rails 4.
|
27
|
+
* Drop support to Rails < 3.2 and Ruby 1.8.
|
28
|
+
|
29
|
+
## 0.2.6
|
30
|
+
|
31
|
+
### enhancements
|
32
|
+
* Ruby 2.0 support.
|
33
|
+
* Add Haml template. (by github.com/nashby) Closes #12.
|
34
|
+
* Add `blank_html` tanslation. (by github.com/nashby)
|
35
|
+
* Add `show_for_class` configuration option. (by github.com/nashby)
|
36
|
+
|
37
|
+
### bug fix
|
38
|
+
* Make show_for works with namespaced models. (by github.com/fabiokr)
|
39
|
+
* Add `blank_content_class` to the attributes. (by github.com/blakehilscher). Closes #24.
|
40
|
+
* Don't call `association.map` if association method is nil (by github.com/nashby). Closes #40.
|
41
|
+
|
42
|
+
## 0.2.5
|
43
|
+
|
44
|
+
### enhancements
|
45
|
+
* Add a `:value` option for attribute (by github.com/ml-gt)
|
46
|
+
* Add `label_class`, `content_class`, `wrapper_class` and `collection_class` configuration options (by github.com/wojtekmach)
|
47
|
+
|
48
|
+
### bug fix
|
49
|
+
* Fix problem with `label => false` and `html_safe` (label => false) (by github.com/nashby)
|
50
|
+
|
51
|
+
## 0.2.4
|
52
|
+
|
53
|
+
### enhancements
|
54
|
+
* Do not add separator if label is not present (by github.com/eugenebolshakov)
|
55
|
+
* Add method for output value only (by github.com/jenkek)
|
56
|
+
|
57
|
+
### bug fix
|
58
|
+
* Fix empty labels to be `html_safe` (label => false) (by github.com/eugenebolshakov)
|
59
|
+
|
60
|
+
## 0.2.3
|
61
|
+
|
62
|
+
### enhancements
|
63
|
+
* added :attributes method to generate all attributes given
|
64
|
+
* update generator to use the new syntax show_for:install
|
65
|
+
|
66
|
+
### bug fix
|
67
|
+
* fix numeric types
|
68
|
+
|
69
|
+
## 0.2.2
|
70
|
+
|
71
|
+
### bug fix
|
72
|
+
* allow show_for to work with AR association proxies
|
73
|
+
|
74
|
+
### enhancements
|
75
|
+
* improvements on html safe and output buffers
|
76
|
+
|
77
|
+
## 0.2.1
|
78
|
+
|
79
|
+
### enhancements
|
80
|
+
* added label_proc
|
81
|
+
* compatibility with latest Rails
|
82
|
+
|
83
|
+
## 0.2.0
|
84
|
+
|
85
|
+
### enhancements
|
86
|
+
* Rails 3 compatibility
|
87
|
+
|
88
|
+
## 0.1.4
|
89
|
+
|
90
|
+
### bug fix
|
91
|
+
* allow show_for to work with AR association proxies
|
92
|
+
|
93
|
+
## 0.1.3
|
94
|
+
|
95
|
+
### enhancements
|
96
|
+
* allow builder to be given to show_for
|
97
|
+
|
98
|
+
### bug fix
|
99
|
+
* Fix typo in yaml
|
100
|
+
|
101
|
+
## 0.1.2
|
102
|
+
|
103
|
+
### enhancements
|
104
|
+
* allow `f.attribute :nickname, :in => :profile` as association shortcut
|
105
|
+
|
106
|
+
### deprecations
|
107
|
+
* `:method` now becomes `:using`
|
108
|
+
|
109
|
+
## 0.1.1
|
110
|
+
|
111
|
+
### enhancements
|
112
|
+
* HAML compatibility (by github.com/grimen)
|
113
|
+
* blank_content support (by github.com/grimen)
|
114
|
+
|
115
|
+
## 0.1
|
116
|
+
|
117
|
+
### First release
|
data/MIT-LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2012-
|
1
|
+
Copyright (c) 2012-2015 Plataformatec http://plataformatec.com.br/
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
a copy of this software and associated documentation files (the
|
data/README.md
ADDED
@@ -0,0 +1,225 @@
|
|
1
|
+
# ShowFor
|
2
|
+
|
3
|
+
[](http://badge.fury.io/rb/show_for)
|
4
|
+
[](http://travis-ci.org/plataformatec/show_for)
|
5
|
+
[](https://codeclimate.com/github/plataformatec/show_for)
|
6
|
+
|
7
|
+
ShowFor allows you to quickly show a model information with I18n features.
|
8
|
+
|
9
|
+
```erb
|
10
|
+
<%= show_for @user do |u| %>
|
11
|
+
<%= u.attribute :name %>
|
12
|
+
<%= u.attribute :nickname, :in => :profile %>
|
13
|
+
<%= u.attribute :confirmed? %>
|
14
|
+
<%= u.attribute :created_at, :format => :short %>
|
15
|
+
<%= u.attribute :last_sign_in_at, :if_blank => "User did not access yet",
|
16
|
+
:wrapper_html => { :id => "sign_in_timestamp" } %>
|
17
|
+
|
18
|
+
<%= u.attribute :photo do %>
|
19
|
+
<%= image_tag(@user.photo_url) %>
|
20
|
+
<% end %>
|
21
|
+
|
22
|
+
<%= u.association :company %>
|
23
|
+
<%= u.association :tags, :to_sentence => true %>
|
24
|
+
<% end %>
|
25
|
+
```
|
26
|
+
|
27
|
+
## Installation
|
28
|
+
|
29
|
+
Install the gem:
|
30
|
+
|
31
|
+
gem install show_for
|
32
|
+
|
33
|
+
Or add ShowFor to your Gemfile and bundle it up:
|
34
|
+
|
35
|
+
gem 'show_for'
|
36
|
+
|
37
|
+
Run the generator:
|
38
|
+
|
39
|
+
rails generate show_for:install
|
40
|
+
|
41
|
+
And you are ready to go.
|
42
|
+
|
43
|
+
Note: This branch aims Rails 3.2 and 4 support, so if you want to use it with
|
44
|
+
older versions of Rails, check out the available branches.
|
45
|
+
|
46
|
+
## Usage
|
47
|
+
|
48
|
+
ShowFor allows you to quickly show a model information with I18n features.
|
49
|
+
|
50
|
+
```erb
|
51
|
+
<%= show_for @admin do |a| %>
|
52
|
+
<%= a.attribute :name %>
|
53
|
+
<%= a.attribute :login, :value => :upcase %>
|
54
|
+
<%= a.attribute :confirmed? %>
|
55
|
+
<%= a.attribute :created_at, :format => :short %>
|
56
|
+
<%= a.attribute :last_sign_in_at, :if_blank => "Administrator did not access yet"
|
57
|
+
:wrapper_html => { :id => "sign_in_timestamp" } %>
|
58
|
+
|
59
|
+
<%= a.attribute :photo do %>
|
60
|
+
<%= image_tag(@admin.photo_url) %>
|
61
|
+
<% end %>
|
62
|
+
|
63
|
+
<% a.value :biography %>
|
64
|
+
<% end %>
|
65
|
+
```
|
66
|
+
|
67
|
+
Will generate something like:
|
68
|
+
|
69
|
+
```html
|
70
|
+
<div id="admin_1" class="show_for admin">
|
71
|
+
<div class="wrapper admin_name">
|
72
|
+
<strong class="label">Name</strong><br />
|
73
|
+
José Valim
|
74
|
+
</div>
|
75
|
+
<div class="wrapper admin_login">
|
76
|
+
<strong class="label">Login</strong><br />
|
77
|
+
JVALIM
|
78
|
+
</div>
|
79
|
+
<div class="wrapper admin_confirmed">
|
80
|
+
<strong class="label">Confirmed?</strong><br />
|
81
|
+
Yes
|
82
|
+
</div>
|
83
|
+
<div class="wrapper admin_created_at">
|
84
|
+
<strong class="label">Created at</strong><br />
|
85
|
+
13/12/2009 - 19h17
|
86
|
+
</div>
|
87
|
+
<div id="sign_in_timestamp" class="wrapper admin_last_sign_in_at">
|
88
|
+
<strong class="label">Last sign in at</strong><br />
|
89
|
+
Administrator did not access yet
|
90
|
+
</div>
|
91
|
+
<div class="wrapper admin_photo">
|
92
|
+
<strong class="label">Photo</strong><br />
|
93
|
+
<img src="path/to/photo" />
|
94
|
+
</div>
|
95
|
+
<div class="wrapper admin_biography">
|
96
|
+
Etiam porttitor eros ut diam vestibulum et blandit lectus tempor. Donec
|
97
|
+
venenatis fermentum nunc ac dignissim. Pellentesque volutpat eros quis enim
|
98
|
+
mollis bibendum. Ut cursus sem ac sem accumsan nec porttitor felis luctus.
|
99
|
+
Sed purus nunc, auctor vitae consectetur pharetra, tristique non nisi.
|
100
|
+
</div>
|
101
|
+
</div>
|
102
|
+
```
|
103
|
+
|
104
|
+
You can also show a list of attributes, useful if you don't need to change any configuration:
|
105
|
+
|
106
|
+
```erb
|
107
|
+
<%= show_for @admin do |a| %>
|
108
|
+
<%= a.attributes :name, :confirmed?, :created_at %>
|
109
|
+
<% end %>
|
110
|
+
```
|
111
|
+
|
112
|
+
## Value lookup
|
113
|
+
|
114
|
+
ShowFor uses the following sequence to get the attribute value:
|
115
|
+
|
116
|
+
* use the output of a block argument if given
|
117
|
+
* use the output of the `:value` argument if given
|
118
|
+
* check if a `:"human_#{attribute}"` method is defined
|
119
|
+
* retrieve the attribute directly.
|
120
|
+
|
121
|
+
## Options
|
122
|
+
|
123
|
+
ShowFor handles a series of options. Those are:
|
124
|
+
|
125
|
+
* __:format__ - Sent to `I18n.localize` when the attribute is a date/time object.
|
126
|
+
|
127
|
+
* __:value__ - Can be used instead of block. If a `Symbol` is called as instance method.
|
128
|
+
|
129
|
+
* __:if_blank__ - An object to be used if the value is blank. Not escaped as well.
|
130
|
+
|
131
|
+
* __:separator__ - The piece of html that separates label and content, overriding the global configuration.
|
132
|
+
|
133
|
+
In addition, all containers (`:label`, `:content` and `:wrapper`) can have their html
|
134
|
+
options configured through the `:label_html`, `:content_html` and `:wrapper_html`
|
135
|
+
options. Containers can have their tags configured on demand as well through
|
136
|
+
`:label_tag,` `:content_tag` and `:wrapper_tag` options.
|
137
|
+
|
138
|
+
## Label
|
139
|
+
|
140
|
+
ShowFor also exposes the label method. In case you want to use the default
|
141
|
+
`human_attribute_name` lookup and the default wrapping:
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
a.label :name #=> <strong class="label">Name</strong>
|
145
|
+
a.label "Name", :id => "my_name" #=> <strong class="label" id="my_name">Name</strong>
|
146
|
+
```
|
147
|
+
|
148
|
+
Optionally, if you want to wrap the inner part of the label with some text
|
149
|
+
(e.g. adding a semicolon), you can do so by specifying a proc for `ShowFor.label_proc`
|
150
|
+
that will be called with any label text. E.g.:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
ShowFor.label_proc = lambda { |l| l + ":" }
|
154
|
+
```
|
155
|
+
|
156
|
+
When taking this route, you can also skip on a per label basis by passing the
|
157
|
+
`:wrap_label` option with a value of false.
|
158
|
+
|
159
|
+
## Associations
|
160
|
+
|
161
|
+
ShowFor also supports associations.
|
162
|
+
|
163
|
+
```erb
|
164
|
+
<%= show_for @artwork do |a| %>
|
165
|
+
<%= a.association :artist %>
|
166
|
+
<%= a.association :artist, :using => :name_with_title %>
|
167
|
+
<%= a.attribute :name_with_title, :in => :artist %>
|
168
|
+
|
169
|
+
<%= a.association :tags %>
|
170
|
+
<%= a.association :tags, :to_sentence => true %>
|
171
|
+
<%= a.association :tags do
|
172
|
+
@artwork.tags.map(&:name).to_sentence
|
173
|
+
end %>
|
174
|
+
|
175
|
+
<%= a.association :fans, :collection_tag => :ol do |fan| %>
|
176
|
+
<li><%= link_to fan.name, fan %></li>
|
177
|
+
<% end %>
|
178
|
+
<% end %>
|
179
|
+
```
|
180
|
+
|
181
|
+
The first is a `has_one` or `belongs_to` association, which works like an attribute
|
182
|
+
to ShowFor, except it will retrieve the artist association and try to find a
|
183
|
+
proper method from `ShowFor.association_methods` to be used. You can pass
|
184
|
+
the option :using to tell (and not guess) which method from the association
|
185
|
+
to use.
|
186
|
+
|
187
|
+
:tags is a `has_and_belongs_to_many` association which will return a collection.
|
188
|
+
ShowFor can handle collections by default by wrapping them in list (`<ul>` with
|
189
|
+
each item wrapped by an `<li>`). However, it also allows you to give `:to_sentence`
|
190
|
+
or `:join` it you want to render them inline.
|
191
|
+
|
192
|
+
You can also pass a block which expects an argument to association. In such cases,
|
193
|
+
a wrapper for the collection is still created and the block just iterates over the
|
194
|
+
collection objects.
|
195
|
+
|
196
|
+
Here are some other examples of the many possibilites to custom the output content:
|
197
|
+
|
198
|
+
```erb
|
199
|
+
<%= u.association :relationships, :label => 'test' do %>
|
200
|
+
<% @user.relationships.each do |relation| %>
|
201
|
+
<%= relation.related_user.name if relation.related_user_role == 'supervisor' %>
|
202
|
+
<% end %>
|
203
|
+
<% end %>
|
204
|
+
|
205
|
+
<%= u.attribute :gender do %>
|
206
|
+
<%= content_tag :span, t("helpers.enum_select.user.gender.#{@user.gender}") %>
|
207
|
+
<% end %>
|
208
|
+
```
|
209
|
+
|
210
|
+
## Maintainers
|
211
|
+
|
212
|
+
* José Valim (http://github.com/josevalim)
|
213
|
+
* Carlos Antonio da Silva (https://github.com/carlosantoniodasilva)
|
214
|
+
|
215
|
+
## Contributors
|
216
|
+
|
217
|
+
* Jonas Grimfelt (http://github.com/grimen)
|
218
|
+
|
219
|
+
## Bugs and Feedback
|
220
|
+
|
221
|
+
If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
|
222
|
+
|
223
|
+
http://github.com/plataformatec/show_for/issues
|
224
|
+
|
225
|
+
MIT License. Copyright 2012-2015 Plataformatec. http://plataformatec.com.br
|
@@ -0,0 +1,8 @@
|
|
1
|
+
= show_for(@<%= singular_name %>) do |s|
|
2
|
+
<%- attributes.each do |attribute| -%>
|
3
|
+
= s.<%= attribute.reference? ? :association : :attribute %> :<%= attribute.name %>
|
4
|
+
<% end -%>
|
5
|
+
|
6
|
+
= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>)
|
7
|
+
|
|
8
|
+
= link_to 'Back', <%= plural_name %>_path
|
@@ -9,21 +9,36 @@ ShowFor.setup do |config|
|
|
9
9
|
# The tag which wraps each attribute/association call. Default is :p.
|
10
10
|
# config.wrapper_tag = :dl
|
11
11
|
|
12
|
+
# The DOM class set for the wrapper tag. Default is :wrapper.
|
13
|
+
# config.wrapper_class = :custom
|
14
|
+
|
12
15
|
# The tag used to wrap each label. Default is :strong.
|
13
16
|
# config.label_tag = :dt
|
14
17
|
|
18
|
+
# The DOM class of each label tag. Default is :label.
|
19
|
+
# config.label_class = :custom
|
20
|
+
|
15
21
|
# The tag used to wrap each content (value). Default is nil.
|
16
22
|
# config.content_tag = :dd
|
17
23
|
|
24
|
+
# The DOM class of each content tag. Default is :content.
|
25
|
+
# config.content_class = :custom
|
26
|
+
|
18
27
|
# The DOM class set for blank content tags. Default is "blank".
|
19
28
|
# config.blank_content_class = 'no_content'
|
20
29
|
|
30
|
+
# Skip blank attributes instead of generating with a default message. Default is false.
|
31
|
+
# config.skip_blanks = true
|
32
|
+
|
21
33
|
# The separator between label and content. Default is "<br />".
|
22
34
|
# config.separator = "<br />"
|
23
35
|
|
24
36
|
# The tag used to wrap collections. Default is :ul.
|
25
37
|
# config.collection_tag = :ul
|
26
38
|
|
39
|
+
# The DOM class set for the collection tag. Default is :collection.
|
40
|
+
# config.collection_class = :custom
|
41
|
+
|
27
42
|
# The default iterator to be used when invoking a collection/association.
|
28
43
|
# config.default_collection_proc = lambda { |value| "<li>#{ERB::Util.h(value)}</li>".html_safe }
|
29
44
|
|
data/lib/show_for.rb
CHANGED
@@ -28,8 +28,11 @@ module ShowFor
|
|
28
28
|
mattr_accessor :blank_content_class
|
29
29
|
@@blank_content_class = "blank"
|
30
30
|
|
31
|
+
mattr_accessor :skip_blanks
|
32
|
+
@@skip_blanks = false
|
33
|
+
|
31
34
|
mattr_accessor :wrapper_tag
|
32
|
-
@@wrapper_tag = :
|
35
|
+
@@wrapper_tag = :div
|
33
36
|
|
34
37
|
mattr_accessor :wrapper_class
|
35
38
|
@@wrapper_class = :wrapper
|
data/lib/show_for/association.rb
CHANGED
@@ -38,17 +38,11 @@ module ShowFor
|
|
38
38
|
protected
|
39
39
|
|
40
40
|
def values_from_association(association, options) #:nodoc:
|
41
|
-
sample = association.
|
42
|
-
|
43
|
-
if options[:method]
|
44
|
-
options[:using] = options.delete(:method)
|
45
|
-
ActiveSupport::Deprecation.warn ":method is deprecated. Please use :using instead", caller
|
46
|
-
end
|
47
|
-
|
41
|
+
sample = association.respond_to?(:to_ary) ? association.first : association
|
48
42
|
method = options.delete(:using) || ShowFor.association_methods.find { |m| sample.respond_to?(m) }
|
49
43
|
|
50
44
|
if method
|
51
|
-
association.
|
45
|
+
association.respond_to?(:to_ary) ? association.map(&method) : association.try(method)
|
52
46
|
end
|
53
47
|
end
|
54
48
|
end
|
data/lib/show_for/builder.rb
CHANGED
@@ -32,8 +32,11 @@ module ShowFor
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def wrap_label_and_content(name, value, options, &block) #:nodoc:
|
35
|
+
return if skip_blanks?(value)
|
35
36
|
label = label(name, options, false)
|
36
|
-
label
|
37
|
+
if label.present? && separator = options.fetch(:separator) { ShowFor.separator }
|
38
|
+
label += separator.html_safe
|
39
|
+
end
|
37
40
|
wrap_with(:wrapper, label + content(value, options, false, &block), apply_wrapper_options!(:wrapper, options, value))
|
38
41
|
end
|
39
42
|
|
@@ -58,6 +61,7 @@ module ShowFor
|
|
58
61
|
# around the given content. It also check for html_options in @options
|
59
62
|
# hash related to the current type.
|
60
63
|
def wrap_with(type, content, options) #:nodoc:
|
64
|
+
return if skip_blanks?(content)
|
61
65
|
tag = options.delete(:"#{type}_tag") || ShowFor.send(:"#{type}_tag")
|
62
66
|
|
63
67
|
if tag
|
@@ -75,6 +79,11 @@ module ShowFor
|
|
75
79
|
def collection_block?(block) #:nodoc:
|
76
80
|
block && block.arity == 1
|
77
81
|
end
|
82
|
+
|
83
|
+
# Verifies whether the value is blank and its configured to skip blank values.
|
84
|
+
def skip_blanks?(value) #:nodoc:
|
85
|
+
ShowFor.skip_blanks && value.blank? && value != false
|
86
|
+
end
|
78
87
|
end
|
79
88
|
end
|
80
89
|
|