show_for 0.7.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -2
- data/MIT-LICENSE +2 -1
- data/README.md +27 -20
- data/lib/generators/show_for/install_generator.rb +1 -1
- data/lib/show_for/association.rb +2 -2
- data/lib/show_for/attribute.rb +4 -6
- data/lib/show_for/content.rb +6 -6
- data/lib/show_for/helper.rb +1 -1
- data/lib/show_for/label.rb +1 -1
- data/lib/show_for/version.rb +1 -1
- metadata +17 -35
- data/test/association_test.rb +0 -152
- data/test/attribute_test.rb +0 -234
- data/test/builder_test.rb +0 -81
- data/test/content_test.rb +0 -33
- data/test/generators/show_for_generator_test.rb +0 -26
- data/test/helper_test.rb +0 -54
- data/test/label_test.rb +0 -78
- data/test/support/misc_helpers.rb +0 -63
- data/test/support/models.rb +0 -57
- data/test/test_helper.rb +0 -48
- data/test/value_test.rb +0 -90
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c8de9d2240c250f8dd8846d1bc53c3b372eaeffb1710ec6b4b16fd23c9afe3a
|
4
|
+
data.tar.gz: bdd646396d9c3dc0fa4cd553bc25c90e7e650277e2db944d35448ba4f8fec369
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1cef95848285f3d701b8f6cd7c91b472c8aae487fb5fa342909d07cd0366a42ce2af27ff4ea4261868339c8de91dfbadc2683f561a06d69448cbe086e9f8568
|
7
|
+
data.tar.gz: 4a31e66d11690af071f8ddda98ce40cf7177a278e6b3a6d12d72f1f920c2ad81ade5f029c49f767eab147dde0cd43f54ab7d867781192d7d6f2819efe22024b6
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,14 @@
|
|
1
|
-
##
|
1
|
+
## 0.8.1
|
2
|
+
|
3
|
+
* Add support for Rails 7.0/7.1 and Ruby 3.1/3.2 (no changes required)
|
4
|
+
* Remove test files from the gem package.
|
5
|
+
|
6
|
+
## 0.8.0
|
7
|
+
|
8
|
+
* Respect `nil` value when provided, by skipping model attribute value.
|
9
|
+
* Add support for Ruby 3.0, drop support for Ruby < 2.5.
|
10
|
+
* Add support for Rails 6.1, drop support for Rails < 5.2.
|
11
|
+
* Move CI to GitHub Actions.
|
2
12
|
|
3
13
|
## 0.7.0
|
4
14
|
|
@@ -125,7 +135,7 @@
|
|
125
135
|
## 0.1.2
|
126
136
|
|
127
137
|
### enhancements
|
128
|
-
* allow `f.attribute :nickname, :
|
138
|
+
* allow `f.attribute :nickname, in: :profile` as association shortcut
|
129
139
|
|
130
140
|
### deprecations
|
131
141
|
* `:method` now becomes `:using`
|
data/MIT-LICENSE
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
Copyright (c)
|
1
|
+
Copyright (c) 2020-2023 Rafael França, Carlos Antônio da Silva
|
2
|
+
Copyright (c) 2012-2019 Plataformatec
|
2
3
|
|
3
4
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
5
|
a copy of this software and associated documentation files (the
|
data/README.md
CHANGED
@@ -1,26 +1,24 @@
|
|
1
1
|
# ShowFor
|
2
2
|
|
3
3
|
[![Gem Version](https://fury-badge.herokuapp.com/rb/show_for.png)](http://badge.fury.io/rb/show_for)
|
4
|
-
[![Build Status](https://api.travis-ci.org/plataformatec/show_for.png?branch=master)](http://travis-ci.org/plataformatec/show_for)
|
5
|
-
[![Code Climate](https://codeclimate.com/github/plataformatec/show_for.png)](https://codeclimate.com/github/plataformatec/show_for)
|
6
4
|
|
7
5
|
ShowFor allows you to quickly show a model information with I18n features.
|
8
6
|
|
9
7
|
```erb
|
10
8
|
<%= show_for @user do |u| %>
|
11
9
|
<%= u.attribute :name %>
|
12
|
-
<%= u.attribute :nickname, :
|
10
|
+
<%= u.attribute :nickname, in: :profile %>
|
13
11
|
<%= u.attribute :confirmed? %>
|
14
|
-
<%= u.attribute :created_at, :
|
15
|
-
<%= u.attribute :last_sign_in_at, :
|
16
|
-
:
|
12
|
+
<%= u.attribute :created_at, format: :short %>
|
13
|
+
<%= u.attribute :last_sign_in_at, if_blank: "User did not access yet",
|
14
|
+
wrapper_html: { id: "sign_in_timestamp" } %>
|
17
15
|
|
18
16
|
<%= u.attribute :photo do %>
|
19
17
|
<%= image_tag(@user.photo_url) %>
|
20
18
|
<% end %>
|
21
19
|
|
22
20
|
<%= u.association :company %>
|
23
|
-
<%= u.association :tags, :
|
21
|
+
<%= u.association :tags, to_sentence: true %>
|
24
22
|
<% end %>
|
25
23
|
```
|
26
24
|
|
@@ -50,11 +48,11 @@ ShowFor allows you to quickly show a model information with I18n features.
|
|
50
48
|
```erb
|
51
49
|
<%= show_for @admin do |a| %>
|
52
50
|
<%= a.attribute :name %>
|
53
|
-
<%= a.attribute :login, :
|
51
|
+
<%= a.attribute :login, value: :upcase %>
|
54
52
|
<%= a.attribute :confirmed? %>
|
55
|
-
<%= a.attribute :created_at, :
|
56
|
-
<%= a.attribute :last_sign_in_at, :
|
57
|
-
:
|
53
|
+
<%= a.attribute :created_at, format: :short %>
|
54
|
+
<%= a.attribute :last_sign_in_at, if_blank: "Administrator did not access yet"
|
55
|
+
wrapper_html: { id: "sign_in_timestamp" } %>
|
58
56
|
|
59
57
|
<%= a.attribute :photo do %>
|
60
58
|
<%= image_tag(@admin.photo_url) %>
|
@@ -141,8 +139,8 @@ ShowFor also exposes the label method. In case you want to use the default
|
|
141
139
|
`human_attribute_name` lookup and the default wrapping:
|
142
140
|
|
143
141
|
```ruby
|
144
|
-
a.label :name
|
145
|
-
a.label "Name", :
|
142
|
+
a.label :name #=> <strong class="label">Name</strong>
|
143
|
+
a.label "Name", id: "my_name" #=> <strong class="label" id="my_name">Name</strong>
|
146
144
|
```
|
147
145
|
|
148
146
|
Optionally, if you want to wrap the inner part of the label with some text
|
@@ -163,16 +161,16 @@ ShowFor also supports associations.
|
|
163
161
|
```erb
|
164
162
|
<%= show_for @artwork do |a| %>
|
165
163
|
<%= a.association :artist %>
|
166
|
-
<%= a.association :artist, :
|
167
|
-
<%= a.attribute :name_with_title, :
|
164
|
+
<%= a.association :artist, using: :name_with_title %>
|
165
|
+
<%= a.attribute :name_with_title, in: :artist %>
|
168
166
|
|
169
167
|
<%= a.association :tags %>
|
170
|
-
<%= a.association :tags, :
|
168
|
+
<%= a.association :tags, to_sentence: true %>
|
171
169
|
<%= a.association :tags do
|
172
170
|
@artwork.tags.map(&:name).to_sentence
|
173
171
|
end %>
|
174
172
|
|
175
|
-
<%= a.association :fans, :
|
173
|
+
<%= a.association :fans, collection_tag: :ol do |fan| %>
|
176
174
|
<li><%= link_to fan.name, fan %></li>
|
177
175
|
<% end %>
|
178
176
|
<% end %>
|
@@ -196,7 +194,7 @@ collection objects.
|
|
196
194
|
Here are some other examples of the many possibilites to custom the output content:
|
197
195
|
|
198
196
|
```erb
|
199
|
-
<%= u.association :relationships, :
|
197
|
+
<%= u.association :relationships, label: 'test' do %>
|
200
198
|
<% @user.relationships.each do |relation| %>
|
201
199
|
<%= relation.related_user.name if relation.related_user_role == 'supervisor' %>
|
202
200
|
<% end %>
|
@@ -216,10 +214,19 @@ Here are some other examples of the many possibilites to custom the output conte
|
|
216
214
|
|
217
215
|
* Jonas Grimfelt (http://github.com/grimen)
|
218
216
|
|
217
|
+
## Supported Ruby / Rails versions
|
218
|
+
|
219
|
+
We intend to maintain support for all Ruby / Rails versions that haven't reached end-of-life.
|
220
|
+
|
221
|
+
For more information about specific versions please check [Ruby](https://www.ruby-lang.org/en/downloads/branches/)
|
222
|
+
and [Rails](https://guides.rubyonrails.org/maintenance_policy.html) maintenance policies, and our test matrix.
|
223
|
+
|
219
224
|
## Bugs and Feedback
|
220
225
|
|
221
226
|
If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
|
222
227
|
|
223
|
-
http://github.com/
|
228
|
+
http://github.com/heartcombo/show_for/issues
|
229
|
+
|
230
|
+
## License
|
224
231
|
|
225
|
-
MIT License. Copyright 2012-2019 Plataformatec.
|
232
|
+
MIT License. Copyright 2020-2023 Rafael França, Carlos Antônio da Silva. Copyright 2012-2019 Plataformatec.
|
@@ -2,7 +2,7 @@ module ShowFor
|
|
2
2
|
module Generators
|
3
3
|
class InstallGenerator < Rails::Generators::Base
|
4
4
|
desc "Copy ShowFor installation files"
|
5
|
-
class_option :template_engine, :
|
5
|
+
class_option :template_engine, desc: 'Template engine to be invoked (erb or haml or slim).'
|
6
6
|
source_root File.expand_path('../templates', __FILE__)
|
7
7
|
|
8
8
|
def copy_initializers
|
data/lib/show_for/association.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module ShowFor
|
2
2
|
module Association
|
3
|
-
def attribute(attribute_name, options={}, &block)
|
3
|
+
def attribute(attribute_name, options = {}, &block)
|
4
4
|
if association_name = options.delete(:in)
|
5
5
|
options[:using] = attribute_name
|
6
6
|
association(association_name, options, &block)
|
@@ -9,7 +9,7 @@ module ShowFor
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
def association(association_name, options={}, &block)
|
12
|
+
def association(association_name, options = {}, &block)
|
13
13
|
apply_default_options!(association_name, options)
|
14
14
|
|
15
15
|
# If a block with an iterator was given, no need to calculate the labels
|
data/lib/show_for/attribute.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module ShowFor
|
2
2
|
module Attribute
|
3
|
-
def attribute(attribute_name, options={}, &block)
|
3
|
+
def attribute(attribute_name, options = {}, &block)
|
4
4
|
apply_default_options!(attribute_name, options)
|
5
5
|
block = block_from_value_option(attribute_name, options) unless block
|
6
6
|
collection_block, block = block, nil if collection_block?(block)
|
@@ -10,7 +10,7 @@ module ShowFor
|
|
10
10
|
wrap_label_and_content(attribute_name, value, options, &collection_block)
|
11
11
|
end
|
12
12
|
|
13
|
-
def value(attribute_name, options={}, &block)
|
13
|
+
def value(attribute_name, options = {}, &block)
|
14
14
|
apply_default_options!(attribute_name, options)
|
15
15
|
collection_block, block = block, nil if collection_block?(block)
|
16
16
|
|
@@ -38,10 +38,9 @@ module ShowFor
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def block_from_value_option(attribute_name, options)
|
41
|
-
|
42
|
-
when nil
|
41
|
+
if !options.has_key?(:value)
|
43
42
|
nil
|
44
|
-
|
43
|
+
elsif options[:value].is_a?(Symbol)
|
45
44
|
block_from_symbol(attribute_name, options)
|
46
45
|
else
|
47
46
|
lambda { options[:value].to_s }
|
@@ -59,4 +58,3 @@ module ShowFor
|
|
59
58
|
end
|
60
59
|
end
|
61
60
|
end
|
62
|
-
|
data/lib/show_for/content.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module ShowFor
|
2
2
|
module Content
|
3
|
-
def content(value, options={}, apply_options=true, &block)
|
3
|
+
def content(value, options = {}, apply_options = true, &block)
|
4
4
|
# cache value for apply_wrapper_options!
|
5
5
|
sample_value = value
|
6
6
|
|
@@ -10,11 +10,11 @@ module ShowFor
|
|
10
10
|
|
11
11
|
content = case value
|
12
12
|
when Date, Time, DateTime
|
13
|
-
I18n.l value, :
|
13
|
+
I18n.l value, format: options.delete(:format) || ShowFor.i18n_format
|
14
14
|
when TrueClass
|
15
|
-
I18n.t :"show_for.yes", :
|
15
|
+
I18n.t :"show_for.yes", default: "Yes"
|
16
16
|
when FalseClass
|
17
|
-
I18n.t :"show_for.no", :
|
17
|
+
I18n.t :"show_for.no", default: "No"
|
18
18
|
when Array, Hash
|
19
19
|
collection_handler(value, options, &block) unless value.empty?
|
20
20
|
when Proc
|
@@ -48,11 +48,11 @@ module ShowFor
|
|
48
48
|
end
|
49
49
|
|
50
50
|
def translate_blank_html
|
51
|
-
template.t(:'show_for.blank_html', :
|
51
|
+
template.t(:'show_for.blank_html', default: translate_blank_text)
|
52
52
|
end
|
53
53
|
|
54
54
|
def translate_blank_text
|
55
|
-
I18n.t(:'show_for.blank', :
|
55
|
+
I18n.t(:'show_for.blank', default: "Not specified")
|
56
56
|
end
|
57
57
|
|
58
58
|
def blank_value(options)
|
data/lib/show_for/helper.rb
CHANGED
data/lib/show_for/label.rb
CHANGED
data/lib/show_for/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: show_for
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- José Valim
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '5.
|
19
|
+
version: '5.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '5.
|
26
|
+
version: '5.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: actionpack
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '5.
|
33
|
+
version: '5.2'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '5.
|
40
|
+
version: '5.2'
|
41
41
|
description: Wrap your objects with a helper to easily show them
|
42
42
|
email: contact@plataformatec.com.br
|
43
43
|
executables: []
|
@@ -62,22 +62,15 @@ files:
|
|
62
62
|
- lib/show_for/helper.rb
|
63
63
|
- lib/show_for/label.rb
|
64
64
|
- lib/show_for/version.rb
|
65
|
-
- test/association_test.rb
|
66
|
-
- test/attribute_test.rb
|
67
|
-
- test/builder_test.rb
|
68
|
-
- test/content_test.rb
|
69
|
-
- test/generators/show_for_generator_test.rb
|
70
|
-
- test/helper_test.rb
|
71
|
-
- test/label_test.rb
|
72
|
-
- test/support/misc_helpers.rb
|
73
|
-
- test/support/models.rb
|
74
|
-
- test/test_helper.rb
|
75
|
-
- test/value_test.rb
|
76
65
|
homepage: http://github.com/plataformatec/show_for
|
77
66
|
licenses:
|
78
67
|
- MIT
|
79
|
-
metadata:
|
80
|
-
|
68
|
+
metadata:
|
69
|
+
homepage_uri: https://github.com/heartcombo/show_for
|
70
|
+
changelog_uri: https://github.com/heartcombo/show_for/blob/main/CHANGELOG.md
|
71
|
+
source_code_uri: https://github.com/heartcombo/show_for
|
72
|
+
bug_tracker_uri: https://github.com/heartcombo/show_for/issues
|
73
|
+
post_install_message:
|
81
74
|
rdoc_options: []
|
82
75
|
require_paths:
|
83
76
|
- lib
|
@@ -85,26 +78,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
78
|
requirements:
|
86
79
|
- - ">="
|
87
80
|
- !ruby/object:Gem::Version
|
88
|
-
version: 2.
|
81
|
+
version: 2.5.0
|
89
82
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
83
|
requirements:
|
91
84
|
- - ">="
|
92
85
|
- !ruby/object:Gem::Version
|
93
86
|
version: '0'
|
94
87
|
requirements: []
|
95
|
-
rubygems_version: 3.
|
96
|
-
signing_key:
|
88
|
+
rubygems_version: 3.4.10
|
89
|
+
signing_key:
|
97
90
|
specification_version: 4
|
98
91
|
summary: Wrap your objects with a helper to easily show them
|
99
|
-
test_files:
|
100
|
-
- test/builder_test.rb
|
101
|
-
- test/attribute_test.rb
|
102
|
-
- test/value_test.rb
|
103
|
-
- test/support/misc_helpers.rb
|
104
|
-
- test/support/models.rb
|
105
|
-
- test/label_test.rb
|
106
|
-
- test/test_helper.rb
|
107
|
-
- test/helper_test.rb
|
108
|
-
- test/association_test.rb
|
109
|
-
- test/generators/show_for_generator_test.rb
|
110
|
-
- test/content_test.rb
|
92
|
+
test_files: []
|
data/test/association_test.rb
DELETED
@@ -1,152 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class AssociationTest < ActionView::TestCase
|
4
|
-
test "show_for works with belongs_to/has_one associations" do
|
5
|
-
with_association_for @user, :company
|
6
|
-
assert_select "div.show_for div.wrapper", /Plataformatec/
|
7
|
-
end
|
8
|
-
|
9
|
-
test "show_for accepts :using as option to tell how to retrieve association value" do
|
10
|
-
with_association_for @user, :company, :using => :alternate_name
|
11
|
-
assert_select "div.show_for div.wrapper", /Alternate Plataformatec/
|
12
|
-
end
|
13
|
-
|
14
|
-
test "show_for accepts :in to tell to retrieve an attribute from association" do
|
15
|
-
with_attribute_for @user, :alternate_name, :in => :company
|
16
|
-
assert_select "div.show_for div.wrapper", /Alternate Plataformatec/
|
17
|
-
end
|
18
|
-
|
19
|
-
test "show_for forwards all options send with :in to association" do
|
20
|
-
with_attribute_for @user, :alternate_name, :in => :tags, :to_sentence => true
|
21
|
-
assert_no_select "div.show_for div.wrapper ul.collection"
|
22
|
-
assert_select "div.show_for div.wrapper", /Alternate Tag 1, Alternate Tag 2, and Alternate Tag 3/
|
23
|
-
end
|
24
|
-
|
25
|
-
test "show_for works with has_many/has_and_belongs_to_many associations" do
|
26
|
-
with_association_for @user, :tags
|
27
|
-
assert_select "div.show_for div.wrapper ul.collection"
|
28
|
-
assert_select "div.show_for div.wrapper ul.collection li", "Tag 1"
|
29
|
-
assert_select "div.show_for div.wrapper ul.collection li", "Tag 2"
|
30
|
-
assert_select "div.show_for div.wrapper ul.collection li", "Tag 3"
|
31
|
-
end
|
32
|
-
|
33
|
-
test "show_for works with has_many/has_and_belongs_to_many blank associations" do
|
34
|
-
def @user.tags
|
35
|
-
[]
|
36
|
-
end
|
37
|
-
|
38
|
-
swap ShowFor, :association_methods => [:name] do
|
39
|
-
with_association_for @user, :tags
|
40
|
-
assert_no_select "div.show_for div.wrapper ul.collection"
|
41
|
-
assert_no_select "div.show_for div.wrapper", /Enumerator/
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
test "show_for accepts a block with has_many/has_and_belongs_to_many blank associations" do
|
46
|
-
def @user.tags
|
47
|
-
[]
|
48
|
-
end
|
49
|
-
|
50
|
-
swap ShowFor, :association_methods => [:name] do
|
51
|
-
with_association_for @user, :tags do |tag|
|
52
|
-
tag.name
|
53
|
-
end
|
54
|
-
assert_no_select "div.show_for div.wrapper ul.collection"
|
55
|
-
assert_no_select "div.show_for div.wrapper", /Enumerator/
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
test "show_for uses :if_blank when has_many/has_and_belongs_to_many association is blank" do
|
60
|
-
def @user.tags
|
61
|
-
[]
|
62
|
-
end
|
63
|
-
|
64
|
-
with_association_for @user, :tags, if_blank: 'No tags' do |tag|
|
65
|
-
tag.name
|
66
|
-
end
|
67
|
-
assert_select "div.show_for div.wrapper.blank", /No tags/
|
68
|
-
assert_no_select "div.show_for div.wrapper ul.collection"
|
69
|
-
assert_no_select "div.show_for div.wrapper", /Enumerator/
|
70
|
-
end
|
71
|
-
|
72
|
-
test "show_for uses :if_blank if given a block when has_many/has_and_belongs_to_many association is blank" do
|
73
|
-
def @user.tags
|
74
|
-
[]
|
75
|
-
end
|
76
|
-
|
77
|
-
with_association_for @user, :tags, if_blank: 'No tags'
|
78
|
-
assert_select "div.show_for div.wrapper.blank", /No tags/
|
79
|
-
assert_no_select "div.show_for div.wrapper ul.collection"
|
80
|
-
assert_no_select "div.show_for div.wrapper", /Enumerator/
|
81
|
-
end
|
82
|
-
|
83
|
-
test "show_for accepts a block with an argument in belongs_to associations" do
|
84
|
-
with_association_for @user, :company do |company|
|
85
|
-
company.name.upcase
|
86
|
-
end
|
87
|
-
|
88
|
-
assert_select "div.show_for div.wrapper", /PLATAFORMATEC/
|
89
|
-
end
|
90
|
-
|
91
|
-
test "show_for accepts :using as option to tell how to retrieve association values" do
|
92
|
-
with_association_for @user, :tags, :using => :alternate_name
|
93
|
-
assert_select "div.show_for div.wrapper ul.collection"
|
94
|
-
assert_select "div.show_for div.wrapper ul.collection li", "Alternate Tag 1"
|
95
|
-
assert_select "div.show_for div.wrapper ul.collection li", "Alternate Tag 2"
|
96
|
-
assert_select "div.show_for div.wrapper ul.collection li", "Alternate Tag 3"
|
97
|
-
end
|
98
|
-
|
99
|
-
test "show_for accepts :to_sentence as option in collection associations" do
|
100
|
-
with_association_for @user, :tags, :to_sentence => true
|
101
|
-
assert_no_select "div.show_for div.wrapper ul.collection"
|
102
|
-
assert_select "div.show_for div.wrapper", /Tag 1, Tag 2, and Tag 3/
|
103
|
-
end
|
104
|
-
|
105
|
-
test "show_for accepts :join as option in collection associations" do
|
106
|
-
with_association_for @user, :tags, :join => ", "
|
107
|
-
assert_no_select "div.show_for div.wrapper ul.collection"
|
108
|
-
assert_select "div.show_for div.wrapper", /Tag 1, Tag 2, Tag 3/
|
109
|
-
end
|
110
|
-
|
111
|
-
test "show_for accepts a block without argument in collection associations" do
|
112
|
-
with_association_for @user, :tags do
|
113
|
-
@user.tags.map(&:name).to_sentence
|
114
|
-
end
|
115
|
-
assert_no_select "div.show_for div.wrapper ul.collection"
|
116
|
-
assert_select "div.show_for div.wrapper", /Tag 1, Tag 2, and Tag 3/
|
117
|
-
end
|
118
|
-
|
119
|
-
test "show_for accepts a block with argument in collection associations" do
|
120
|
-
with_association_for @user, :tags, :collection_tag => :p do |tag|
|
121
|
-
assert_kind_of Tag, tag
|
122
|
-
content_tag(:span, tag.name)
|
123
|
-
end
|
124
|
-
assert_no_select "div.show_for div.wrapper ul.collection"
|
125
|
-
assert_select "div.show_for div.wrapper p.collection"
|
126
|
-
assert_select "div.show_for div.wrapper p.collection span", "Tag 1"
|
127
|
-
assert_select "div.show_for div.wrapper p.collection span", "Tag 2"
|
128
|
-
assert_select "div.show_for div.wrapper p.collection span", "Tag 3"
|
129
|
-
end
|
130
|
-
|
131
|
-
test "show_for does not display empty has_many/has_and_belongs_to_many association if skip_blanks option is passed" do
|
132
|
-
def @user.tags
|
133
|
-
[]
|
134
|
-
end
|
135
|
-
|
136
|
-
swap ShowFor, :skip_blanks => true do
|
137
|
-
with_association_for @user, :tags
|
138
|
-
assert_no_select "div.show_for div.wrapper"
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
test "show_for does not display empty belongs_to/has_one association if skip_blanks option is passed" do
|
143
|
-
def @user.company
|
144
|
-
nil
|
145
|
-
end
|
146
|
-
|
147
|
-
swap ShowFor, :skip_blanks => true do
|
148
|
-
with_association_for @user, :company
|
149
|
-
assert_no_select "div.show_for div.wrapper"
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
data/test/attribute_test.rb
DELETED
@@ -1,234 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class AttributeTest < ActionView::TestCase
|
4
|
-
# COLLECTIONS
|
5
|
-
test "show_for accepts an attribute as a collection" do
|
6
|
-
with_attribute_for @user, :scopes
|
7
|
-
assert_select "div.show_for div.wrapper ul.collection"
|
8
|
-
assert_select "div.show_for div.wrapper ul.collection li", :count => 3
|
9
|
-
end
|
10
|
-
|
11
|
-
test "show_for accepts an attribute as a collection with a block to iterate the collection" do
|
12
|
-
with_attribute_for @user, :scopes do |scope|
|
13
|
-
content_tag :span, scope
|
14
|
-
end
|
15
|
-
assert_select "div.show_for div.wrapper ul.collection"
|
16
|
-
assert_select "div.show_for div.wrapper ul.collection span", :count => 3
|
17
|
-
end
|
18
|
-
|
19
|
-
test "show_for treats symbol for :value as method on each element of collection" do
|
20
|
-
with_attribute_for @user, :scopes, :value => :upcase
|
21
|
-
@user.scopes.each do |scope|
|
22
|
-
assert_select "div.show_for div.wrapper ul.collection", /#{scope.upcase}/
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
test "show_for allows collection tag to be configured globally" do
|
27
|
-
swap ShowFor, :collection_tag => :ol, :collection_class => "my_collection" do
|
28
|
-
with_attribute_for @user, :scopes
|
29
|
-
assert_select "div.show_for div.wrapper ol.my_collection"
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
test "show_for allows collection class to be disabled globally" do
|
34
|
-
swap ShowFor, :collection_tag => :ol, :collection_class => nil do
|
35
|
-
with_attribute_for @user, :scopes
|
36
|
-
assert_select "div.show_for div.wrapper ol"
|
37
|
-
assert_no_select "ol[class]"
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
test "show_for allows collection tag to be changed by attribute" do
|
42
|
-
with_attribute_for @user, :scopes, :collection_tag => :ol
|
43
|
-
assert_select "div.show_for div.wrapper ol.collection"
|
44
|
-
end
|
45
|
-
|
46
|
-
test "show_for allows collection tag html to be configured by attribute" do
|
47
|
-
with_attribute_for @user, :scopes, :collection_html => { :id => "thecollection", :class => "special" }
|
48
|
-
assert_select "div.show_for div.wrapper ul#thecollection.special.collection"
|
49
|
-
end
|
50
|
-
|
51
|
-
# CONTENT
|
52
|
-
test "show_for allows content tag to be configured globally" do
|
53
|
-
swap ShowFor, :content_tag => :span, :content_class => :my_content do
|
54
|
-
with_attribute_for @user, :name
|
55
|
-
assert_select "div.show_for div.wrapper span.my_content"
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
test "show_for allows content class to be disabled globally" do
|
60
|
-
swap ShowFor, :content_tag => :span, :content_class => nil do
|
61
|
-
with_attribute_for @user, :name
|
62
|
-
assert_select "div.show_for div.wrapper span"
|
63
|
-
assert_no_select "span[class]"
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
test "show_for allows content tag to be changed by attribute" do
|
68
|
-
with_attribute_for @user, :name, :content_tag => :span
|
69
|
-
assert_select "div.show_for div.wrapper span.content"
|
70
|
-
end
|
71
|
-
|
72
|
-
test "show_for allows content tag html to be configured by attribute" do
|
73
|
-
with_attribute_for @user, :name, :content_tag => :span, :content_html => { :id => "thecontent", :class => "special" }
|
74
|
-
assert_select "div.show_for div.wrapper span#thecontent.special.content"
|
75
|
-
end
|
76
|
-
|
77
|
-
test "show_for accepts an attribute as string" do
|
78
|
-
with_attribute_for @user, :name
|
79
|
-
assert_select "div.show_for div.wrapper", /ShowFor/
|
80
|
-
end
|
81
|
-
|
82
|
-
test "show_for accepts an attribute as time" do
|
83
|
-
with_attribute_for @user, :created_at
|
84
|
-
assert_select "div.show_for div.wrapper", /#{Regexp.escape(I18n.l(@user.created_at))}/
|
85
|
-
end
|
86
|
-
|
87
|
-
test "show_for accepts an attribute as date" do
|
88
|
-
with_attribute_for @user, :updated_at
|
89
|
-
assert_select "div.show_for div.wrapper", /#{Regexp.escape(I18n.l(@user.updated_at))}/
|
90
|
-
end
|
91
|
-
|
92
|
-
test "show_for accepts an attribute as time with format options" do
|
93
|
-
with_attribute_for @user, :created_at, :format => :long
|
94
|
-
assert_select "div.show_for div.wrapper", /#{Regexp.escape(I18n.l(@user.created_at, :format => :long))}/
|
95
|
-
end
|
96
|
-
|
97
|
-
test "show_for accepts an attribute as true" do
|
98
|
-
with_attribute_for @user, :active
|
99
|
-
assert_select "div.show_for div.wrapper", /Yes/
|
100
|
-
end
|
101
|
-
|
102
|
-
test "show_for accepts an attribute as true which can be localized" do
|
103
|
-
store_translations(:en, :show_for => { :yes => "Hell yeah!" }) do
|
104
|
-
with_attribute_for @user, :active
|
105
|
-
assert_select "div.show_for div.wrapper", /Hell yeah!/
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
test "show_for accepts an attribute as false" do
|
110
|
-
with_attribute_for @user, :invalid
|
111
|
-
assert_select "div.show_for div.wrapper", /No/
|
112
|
-
end
|
113
|
-
|
114
|
-
test "show_for accepts an attribute as false which can be localized" do
|
115
|
-
store_translations(:en, :show_for => { :no => "Hell no!" }) do
|
116
|
-
with_attribute_for @user, :invalid
|
117
|
-
assert_select "div.show_for div.wrapper", /Hell no!/
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
test "show_for accepts nil and or blank attributes" do
|
122
|
-
with_attribute_for @user, :description
|
123
|
-
assert_select "div.show_for div.wrapper", /Not specified/
|
124
|
-
end
|
125
|
-
|
126
|
-
test "show_for accepts not spcified message can be localized" do
|
127
|
-
store_translations(:en, :show_for => { :blank => "OMG! It's blank!" }) do
|
128
|
-
with_attribute_for @user, :description
|
129
|
-
assert_select "div.show_for div.wrapper", /OMG! It's blank!/
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
test "show_for accepts not spcified message can be localized with html" do
|
134
|
-
store_translations(:en, :show_for => { :blank_html => "<span>OMG! It's blank!</span>" }) do
|
135
|
-
with_attribute_for @user, :description
|
136
|
-
assert_select "div.show_for div.wrapper span", "OMG! It's blank!"
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
test "show_for uses :if_blank if attribute is blank" do
|
141
|
-
with_attribute_for @user, :description, :if_blank => "No description provided"
|
142
|
-
assert_select "div.show_for div.wrapper", /No description provided/
|
143
|
-
end
|
144
|
-
|
145
|
-
test "show_for accepts a block to supply the content" do
|
146
|
-
with_attribute_for @user, :description do
|
147
|
-
"This description is not blank"
|
148
|
-
end
|
149
|
-
assert_select "div.show_for div.wrapper", /This description/
|
150
|
-
end
|
151
|
-
|
152
|
-
test "show_for uses :if_blank if the block content is blank" do
|
153
|
-
with_attribute_for @user, :description, :if_blank => "No description provided" do
|
154
|
-
""
|
155
|
-
end
|
156
|
-
assert_select "div.show_for div.wrapper", /No description provided/
|
157
|
-
end
|
158
|
-
|
159
|
-
test "show_for#content given a block should be wrapped in the result" do
|
160
|
-
with_attribute_for @user, :name do |name|
|
161
|
-
"<div class='block'>#{name}</div>".html_safe
|
162
|
-
end
|
163
|
-
assert_select "div.wrapper.user_name div.block", /ShowFor/
|
164
|
-
end
|
165
|
-
|
166
|
-
test "show_for escapes content by default" do
|
167
|
-
@user.name = "<b>hack you!</b>"
|
168
|
-
with_attribute_for @user, :name
|
169
|
-
assert_no_select "div.show_for div.wrapper b"
|
170
|
-
assert_select "div.show_for div.wrapper",
|
171
|
-
rails_42? ? "Super User Name!<b>hack you!</b>" : "Super User Name!<b>hack you!</b>"
|
172
|
-
end
|
173
|
-
|
174
|
-
test "show_for works with html_safe marked strings" do
|
175
|
-
@user.name = "<b>hack you!</b>".html_safe
|
176
|
-
with_attribute_for @user, :name
|
177
|
-
assert_select "div.show_for div.wrapper b", "hack you!"
|
178
|
-
end
|
179
|
-
|
180
|
-
test "show_for uses :value if supplied" do
|
181
|
-
with_attribute_for @user, :name, :value => "Calculated Value"
|
182
|
-
assert_select "div.show_for div.wrapper", /Calculated Value/
|
183
|
-
end
|
184
|
-
|
185
|
-
test "show_for uses :value and casts to string if supplied" do
|
186
|
-
with_attribute_for @user, :name, :value => 123
|
187
|
-
assert_select "div.show_for div.wrapper", /123/
|
188
|
-
end
|
189
|
-
|
190
|
-
test "show_for ignores :value if a block is supplied" do
|
191
|
-
with_attribute_for @user, :name, :value => "Calculated Value" do
|
192
|
-
@user.name.upcase
|
193
|
-
end
|
194
|
-
assert_select "div.show_for div.wrapper", /#{@user.name.upcase}/
|
195
|
-
end
|
196
|
-
|
197
|
-
test "show_for treats symbol for :value as method on attribute" do
|
198
|
-
with_attribute_for @user, :name, :value => :upcase
|
199
|
-
assert_select "div.show_for div.wrapper", /#{@user.name.upcase}/
|
200
|
-
end
|
201
|
-
|
202
|
-
test "show_for does not display blank attribute if skip_blanks option is passed" do
|
203
|
-
swap ShowFor, :skip_blanks => true do
|
204
|
-
with_attribute_for @user, :description
|
205
|
-
assert_no_select "div.show_for div.wrapper"
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
test "show_for display false attribute if skip_blanks option is passed" do
|
210
|
-
swap ShowFor, :skip_blanks => true do
|
211
|
-
with_attribute_for @user, :invalid
|
212
|
-
assert_select "div.show_for div.wrapper", /No/
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
# ATTRIBUTES
|
217
|
-
test "show_for attributes wraps each attribute with a label and content" do
|
218
|
-
with_attributes_for @user, :name, :email
|
219
|
-
assert_select "div.show_for div.user_name.wrapper", /ShowFor/
|
220
|
-
assert_select "div.user_name strong.label", "Super User Name!"
|
221
|
-
assert_select "div.show_for div.user_email.wrapper", /Not specified/
|
222
|
-
assert_select "div.user_email strong.label", "Email"
|
223
|
-
end
|
224
|
-
|
225
|
-
test "show_for should wrap blank attributes with no_attribute" do
|
226
|
-
swap ShowFor, :blank_content_class => 'no_attribute' do
|
227
|
-
with_attributes_for @user, :name, :birthday, :karma
|
228
|
-
assert_select ".wrapper.user_birthday.no_attribute"
|
229
|
-
assert_select ".wrapper.user_karma.no_attribute"
|
230
|
-
assert_select ".wrapper.user_name.no_attribute", false
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
end
|
data/test/builder_test.rb
DELETED
@@ -1,81 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class BuilderTest < ActionView::TestCase
|
4
|
-
# WRAPPER
|
5
|
-
test "show_for allows wrapper to be configured globally" do
|
6
|
-
swap ShowFor, :wrapper_tag => "li", :wrapper_class => "my_wrapper" do
|
7
|
-
with_attribute_for @user, :name
|
8
|
-
assert_select "div.show_for li.user_name.my_wrapper"
|
9
|
-
assert_select "div.show_for li.my_wrapper strong.label"
|
10
|
-
assert_select "div.show_for li.my_wrapper"
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
test "show_for allows wrapper class to be disabled globally" do
|
15
|
-
swap ShowFor, :wrapper_tag => "li", :wrapper_class => nil do
|
16
|
-
with_attribute_for @user, :name
|
17
|
-
assert_select "div.show_for li[class='user_name']"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
test "show_for attribute wraps each attribute with a label and content" do
|
22
|
-
with_attribute_for @user, :name
|
23
|
-
assert_select "div.show_for div.user_name.wrapper"
|
24
|
-
assert_select "div.show_for div.wrapper strong.label"
|
25
|
-
assert_select "div.show_for div.wrapper"
|
26
|
-
end
|
27
|
-
|
28
|
-
test "show_for properly deals with namespaced models" do
|
29
|
-
@user = Namespaced::User.new(:id => 1, :name => "ShowFor")
|
30
|
-
|
31
|
-
with_attribute_for @user, :name
|
32
|
-
assert_select "div.show_for div.namespaced_user_name.wrapper"
|
33
|
-
assert_select "div.show_for div.wrapper strong.label"
|
34
|
-
assert_select "div.show_for div.wrapper"
|
35
|
-
end
|
36
|
-
|
37
|
-
test "show_for allows wrapper tag to be changed by attribute" do
|
38
|
-
with_attribute_for @user, :name, :wrapper_tag => :span
|
39
|
-
assert_select "div.show_for span.user_name.wrapper"
|
40
|
-
end
|
41
|
-
|
42
|
-
test "show_for allows wrapper html to be configured by attribute" do
|
43
|
-
with_attribute_for @user, :name, :wrapper_html => { :id => "thewrapper", :class => "special" }
|
44
|
-
assert_select "div.show_for div#thewrapper.user_name.wrapper.special"
|
45
|
-
end
|
46
|
-
|
47
|
-
# SEPARATOR
|
48
|
-
test "show_for allows separator to be configured globally" do
|
49
|
-
swap ShowFor, :separator => '<span class="separator"></span>' do
|
50
|
-
with_attribute_for @user, :name
|
51
|
-
assert_select "div.show_for div.user_name span.separator"
|
52
|
-
assert_select "div.show_for div.wrapper span.separator"
|
53
|
-
assert_no_select "div.show_for br"
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
test "show_for allows separator to be changed by attribute"do
|
58
|
-
with_attribute_for @user, :name, :separator => '<span class="separator"></span>'
|
59
|
-
assert_select "div.show_for div.wrapper span.separator"
|
60
|
-
assert_no_select "div.show_for br"
|
61
|
-
end
|
62
|
-
|
63
|
-
test "show_for allows disabling separator by attribute" do
|
64
|
-
with_attribute_for @user, :name, :separator => false
|
65
|
-
assert_no_select "div.show_for div.wrapper span.separator"
|
66
|
-
assert_no_select "div.show_for div.wrapper br"
|
67
|
-
assert_no_select "div.show_for div.wrapper", /false/
|
68
|
-
end
|
69
|
-
|
70
|
-
test "show_for uses a separator if requested" do
|
71
|
-
with_attribute_for @user, :name
|
72
|
-
assert_select "div.show_for div.wrapper br"
|
73
|
-
end
|
74
|
-
|
75
|
-
test "show_for does not blow if a separator is not set" do
|
76
|
-
swap ShowFor, :separator => nil do
|
77
|
-
with_attribute_for @user, :name
|
78
|
-
assert_select "div.show_for div.wrapper"
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
data/test/content_test.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ContentTest < ActionView::TestCase
|
4
|
-
test "show_for#content accepts any object" do
|
5
|
-
with_content_for @user, "Special content"
|
6
|
-
assert_select "div.show_for", "Special content"
|
7
|
-
end
|
8
|
-
|
9
|
-
test "show_for#content accepts :if_blank as option" do
|
10
|
-
with_content_for @user, "", :if_blank => "Got blank"
|
11
|
-
assert_select "div.show_for", "Got blank"
|
12
|
-
end
|
13
|
-
|
14
|
-
test "show_for#content accepts html options" do
|
15
|
-
with_content_for @user, "Special content", :content_tag => :b, :id => "thecontent", :class => "special"
|
16
|
-
assert_select "div.show_for b#thecontent.special.content", "Special content"
|
17
|
-
assert_no_select "div.show_for b[content_tag]"
|
18
|
-
end
|
19
|
-
|
20
|
-
test "show_for#content with blank value has a 'no value'-class" do
|
21
|
-
swap ShowFor, :blank_content_class => "nothing" do
|
22
|
-
with_content_for @user, nil, :content_tag => :b
|
23
|
-
assert_select "div.show_for b.nothing"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
test "show_for#content with blank value does not display content if skip_blanks option is passed" do
|
28
|
-
swap ShowFor, :skip_blanks => true do
|
29
|
-
with_content_for @user, nil, :content_tag => :b
|
30
|
-
assert_no_select "div.show_for b"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ShowForGeneratorTest < Rails::Generators::TestCase
|
4
|
-
tests ShowFor::Generators::InstallGenerator
|
5
|
-
destination File.expand_path('../../tmp', __FILE__)
|
6
|
-
setup :prepare_destination
|
7
|
-
teardown { rm_rf(destination_root) }
|
8
|
-
|
9
|
-
test 'generates example locale file' do
|
10
|
-
run_generator
|
11
|
-
assert_file 'config/locales/show_for.en.yml'
|
12
|
-
end
|
13
|
-
|
14
|
-
test 'generates the show_for initializer' do
|
15
|
-
run_generator
|
16
|
-
assert_file 'config/initializers/show_for.rb',
|
17
|
-
/config.show_for_tag = :div/
|
18
|
-
end
|
19
|
-
|
20
|
-
%W(erb haml slim).each do |engine|
|
21
|
-
test "generates the scaffold template when using #{engine}" do
|
22
|
-
run_generator ['-e', engine]
|
23
|
-
assert_file "lib/templates/#{engine}/scaffold/show.html.#{engine}"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
data/test/helper_test.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
class CustomBuilder < ShowFor::Builder
|
4
|
-
end
|
5
|
-
|
6
|
-
class HelperTest < ActionView::TestCase
|
7
|
-
test "show for yields an instance of ShowFor::Builder" do
|
8
|
-
show_for(@user) do |f|
|
9
|
-
assert f.instance_of?(ShowFor::Builder)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
test "show for yields an instance of builder class specified by builder option" do
|
14
|
-
show_for(@user, :builder => CustomBuilder) do |f|
|
15
|
-
assert f.instance_of?(CustomBuilder)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
test "show for should add default class to form" do
|
20
|
-
concat(show_for(@user) do |f| end)
|
21
|
-
assert_select "div.show_for"
|
22
|
-
end
|
23
|
-
|
24
|
-
test "show for should add object class name as css class to form" do
|
25
|
-
concat(show_for(@user) do |f| end)
|
26
|
-
assert_select "div.show_for.user"
|
27
|
-
end
|
28
|
-
|
29
|
-
test "show for should pass options" do
|
30
|
-
concat(show_for(@user, :id => "my_div", :class => "common") do |f| end)
|
31
|
-
assert_select "div#my_div.show_for.user.common"
|
32
|
-
end
|
33
|
-
|
34
|
-
test "show for tag should be configurable" do
|
35
|
-
swap ShowFor, :show_for_tag => :p do
|
36
|
-
concat(show_for(@user) do |f| end)
|
37
|
-
assert_select "p.show_for"
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
test "show for class should be configurable" do
|
42
|
-
swap ShowFor, :show_for_class => :awesome do
|
43
|
-
concat(show_for(@user) do |f| end)
|
44
|
-
assert_select "div.show_for.user.awesome"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
test "show for options hash should not be modified" do
|
49
|
-
html_options = { :show_for_tag => :li }
|
50
|
-
concat(show_for(@user, html_options) do |f| end)
|
51
|
-
assert_equal({ :show_for_tag => :li }, html_options)
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
data/test/label_test.rb
DELETED
@@ -1,78 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class LabelTest < ActionView::TestCase
|
4
|
-
test "show_for shows a label using the humanized attribute name from model" do
|
5
|
-
with_attribute_for @user, :name
|
6
|
-
assert_select "div.show_for div.wrapper strong.label", "Super User Name!"
|
7
|
-
end
|
8
|
-
|
9
|
-
test "show_for skips label if requested" do
|
10
|
-
with_attribute_for @user, :name, :label => false
|
11
|
-
assert_no_select "div.show_for div.wrapper strong.label"
|
12
|
-
assert_no_select "div.show_for div.wrapper br"
|
13
|
-
end
|
14
|
-
|
15
|
-
test "show_for uses custom content_tag and skips label if requested" do
|
16
|
-
with_attribute_for @user, :name, :label => false, :content_tag => :h2
|
17
|
-
assert_select "div.show_for div.wrapper h2.content", "ShowFor"
|
18
|
-
end
|
19
|
-
|
20
|
-
test "show_for allows label to be configured globally" do
|
21
|
-
swap ShowFor, :label_tag => :span, :label_class => "my_label" do
|
22
|
-
with_attribute_for @user, :name
|
23
|
-
assert_select "div.show_for div.wrapper span.my_label"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
test "show_for allows label class to be disabled globally" do
|
28
|
-
swap ShowFor, :label_tag => :span, :label_class => nil do
|
29
|
-
with_attribute_for @user, :name
|
30
|
-
assert_select "div.show_for div.wrapper span"
|
31
|
-
assert_no_select "span[class]"
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
test "show_for allows label to be changed by attribute" do
|
36
|
-
with_attribute_for @user, :name, :label_tag => :span
|
37
|
-
assert_select "div.show_for div.wrapper span.label"
|
38
|
-
end
|
39
|
-
|
40
|
-
test "show_for allows label html to be configured by attribute" do
|
41
|
-
with_attribute_for @user, :name, :label_html => { :id => "thelabel", :class => "special" }
|
42
|
-
assert_select "div.show_for div.wrapper strong#thelabel.special.label"
|
43
|
-
end
|
44
|
-
|
45
|
-
test "show_for allows label to be set without lookup" do
|
46
|
-
with_attribute_for @user, :name, :label => "Special Label"
|
47
|
-
assert_select "div.show_for div.wrapper strong.label", "Special Label"
|
48
|
-
end
|
49
|
-
|
50
|
-
test "show_for#label accepts the text" do
|
51
|
-
with_label_for @user, "Special Label"
|
52
|
-
assert_select "div.show_for strong.label", "Special Label"
|
53
|
-
end
|
54
|
-
|
55
|
-
test "show_for#label accepts an attribute name" do
|
56
|
-
with_label_for @user, :name
|
57
|
-
assert_select "div.show_for strong.label", "Super User Name!"
|
58
|
-
end
|
59
|
-
|
60
|
-
test "show_for#label accepts html options" do
|
61
|
-
with_label_for @user, :name, :id => "thelabel", :class => "special"
|
62
|
-
assert_select "div.show_for strong#thelabel.special.label"
|
63
|
-
end
|
64
|
-
|
65
|
-
test "should let you override the label wrapper" do
|
66
|
-
swap ShowFor, :label_proc => proc { |l| l + ":" } do
|
67
|
-
with_label_for @user, "Special Label"
|
68
|
-
assert_select "div.show_for strong.label", "Special Label:"
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
test "should you skip wrapping the label on a per item basis" do
|
73
|
-
swap ShowFor, :label_proc => proc { |l| l + ":" } do
|
74
|
-
with_label_for @user, "Special Label", :wrap_label => false
|
75
|
-
assert_select "div.show_for strong.label", "Special Label"
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
@@ -1,63 +0,0 @@
|
|
1
|
-
module MiscHelpers
|
2
|
-
def store_translations(locale, translations, &block)
|
3
|
-
begin
|
4
|
-
I18n.backend.store_translations locale, translations
|
5
|
-
yield
|
6
|
-
ensure
|
7
|
-
I18n.reload!
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
def assert_no_select(selector, value=nil)
|
12
|
-
assert_select(selector, :text => value, :count => 0)
|
13
|
-
end
|
14
|
-
|
15
|
-
def swap(object, new_values)
|
16
|
-
old_values = {}
|
17
|
-
new_values.each do |key, value|
|
18
|
-
old_values[key] = object.send key
|
19
|
-
object.send :"#{key}=", value
|
20
|
-
end
|
21
|
-
yield
|
22
|
-
ensure
|
23
|
-
old_values.each do |key, value|
|
24
|
-
object.send :"#{key}=", value
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def with_attribute_for(object, attribute, options={}, &block)
|
29
|
-
concat(show_for(object) do |o|
|
30
|
-
concat o.attribute(attribute, options, &block)
|
31
|
-
end)
|
32
|
-
end
|
33
|
-
|
34
|
-
def with_value_for(object, attribute, options={}, &block)
|
35
|
-
concat(show_for(object) do |o|
|
36
|
-
concat o.value(attribute, options, &block)
|
37
|
-
end)
|
38
|
-
end
|
39
|
-
|
40
|
-
def with_association_for(object, association, options={}, &block)
|
41
|
-
concat(show_for(object) do |o|
|
42
|
-
concat o.association(association, options, &block)
|
43
|
-
end)
|
44
|
-
end
|
45
|
-
|
46
|
-
def with_label_for(object, attribute, options={})
|
47
|
-
concat(show_for(object) do |o|
|
48
|
-
concat o.label(attribute, options)
|
49
|
-
end)
|
50
|
-
end
|
51
|
-
|
52
|
-
def with_content_for(object, value, options={})
|
53
|
-
concat(show_for(object) do |o|
|
54
|
-
concat o.content(value, options)
|
55
|
-
end)
|
56
|
-
end
|
57
|
-
|
58
|
-
def with_attributes_for(object, *attributes)
|
59
|
-
concat(show_for(object) do |o|
|
60
|
-
concat o.attributes(*attributes)
|
61
|
-
end)
|
62
|
-
end
|
63
|
-
end
|
data/test/support/models.rb
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
require 'ostruct'
|
2
|
-
|
3
|
-
Company = Struct.new(:id, :name) do
|
4
|
-
extend ActiveModel::Naming
|
5
|
-
|
6
|
-
def alternate_name
|
7
|
-
"Alternate #{self.name}"
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
|
12
|
-
Tag = Struct.new(:id, :name) do
|
13
|
-
extend ActiveModel::Naming
|
14
|
-
|
15
|
-
def self.all(options={})
|
16
|
-
(1..3).map{ |i| Tag.new(i, "Tag #{i}") }
|
17
|
-
end
|
18
|
-
|
19
|
-
def alternate_name
|
20
|
-
"Alternate #{self.name}"
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
class User < OpenStruct
|
25
|
-
extend ActiveModel::Naming
|
26
|
-
|
27
|
-
# Get rid of deprecation warnings
|
28
|
-
undef_method :id if respond_to?(:id)
|
29
|
-
|
30
|
-
def tags
|
31
|
-
Tag.all
|
32
|
-
end
|
33
|
-
|
34
|
-
def company
|
35
|
-
Company.new(1, "Plataformatec")
|
36
|
-
end
|
37
|
-
|
38
|
-
def self.human_attribute_name(attribute)
|
39
|
-
case attribute
|
40
|
-
when 'name'
|
41
|
-
'Super User Name!'
|
42
|
-
when 'company'
|
43
|
-
'Company Human Name!'
|
44
|
-
else
|
45
|
-
attribute.humanize
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def self.human_name
|
50
|
-
"User"
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
module Namespaced
|
55
|
-
class User < ::User
|
56
|
-
end
|
57
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
require 'bundler/setup'
|
2
|
-
|
3
|
-
require 'minitest/autorun'
|
4
|
-
|
5
|
-
require 'active_model'
|
6
|
-
require 'action_controller'
|
7
|
-
require 'action_view'
|
8
|
-
require 'action_view/template'
|
9
|
-
require 'action_view/test_case'
|
10
|
-
|
11
|
-
require "rails/generators/test_case"
|
12
|
-
require 'generators/show_for/install_generator'
|
13
|
-
|
14
|
-
$:.unshift File.expand_path("../../lib", __FILE__)
|
15
|
-
require 'show_for'
|
16
|
-
|
17
|
-
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each { |f| require f }
|
18
|
-
I18n.enforce_available_locales = true
|
19
|
-
I18n.default_locale = :en
|
20
|
-
|
21
|
-
ActiveSupport::TestCase.test_order = :random if ActiveSupport::TestCase.respond_to?(:test_order=)
|
22
|
-
|
23
|
-
class ActionView::TestCase
|
24
|
-
include MiscHelpers
|
25
|
-
include ShowFor::Helper
|
26
|
-
|
27
|
-
setup :setup_new_user
|
28
|
-
|
29
|
-
def setup_new_user(options={})
|
30
|
-
@user = User.new({
|
31
|
-
:id => 1,
|
32
|
-
:name => 'ShowFor',
|
33
|
-
:description => '',
|
34
|
-
:active => true,
|
35
|
-
:invalid => false,
|
36
|
-
:scopes => ["admin", "manager", "visitor"],
|
37
|
-
:birthday => nil,
|
38
|
-
:karma => Proc.new { nil },
|
39
|
-
:created_at => Time.now,
|
40
|
-
:updated_at => Date.today
|
41
|
-
}.merge(options))
|
42
|
-
end
|
43
|
-
|
44
|
-
# TODO: remove after supporting Rails 4.2+ only.
|
45
|
-
def rails_42?
|
46
|
-
ActiveModel::VERSION::STRING >= "4.2.0"
|
47
|
-
end
|
48
|
-
end
|
data/test/value_test.rb
DELETED
@@ -1,90 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ValueTest < ActionView::TestCase
|
4
|
-
test "show_for allows content tag to be configured globally, without label and separator" do
|
5
|
-
swap ShowFor, :content_tag => :span do
|
6
|
-
with_value_for @user, :name
|
7
|
-
assert_no_select "div.show_for div.wrapper strong.label"
|
8
|
-
assert_no_select "div.show_for div.wrapper br"
|
9
|
-
assert_select "div.show_for div.wrapper span.content"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
test "show_for allows content with tag to be changed by attribute, without label and separator" do
|
14
|
-
with_value_for @user, :name, :content_tag => :span
|
15
|
-
assert_no_select "div.show_for div.wrapper strong.label"
|
16
|
-
assert_no_select "div.show_for div.wrapper br"
|
17
|
-
assert_select "div.show_for div.wrapper span.content"
|
18
|
-
end
|
19
|
-
|
20
|
-
test "show_for allows content tag html to be configured by attribute, without label and separator" do
|
21
|
-
with_value_for @user, :name, :content_tag => :span, :content_html => { :id => "thecontent", :class => "special" }
|
22
|
-
assert_no_select "div.show_for div.wrapper strong.label"
|
23
|
-
assert_no_select "div.show_for div.wrapper br"
|
24
|
-
assert_select "div.show_for div.wrapper span#thecontent.special.content"
|
25
|
-
end
|
26
|
-
|
27
|
-
test "show_for accepts an attribute as string, without label and separator" do
|
28
|
-
with_value_for @user, :name
|
29
|
-
assert_no_select "div.show_for div.wrapper strong.label"
|
30
|
-
assert_no_select "div.show_for div.wrapper br"
|
31
|
-
assert_select "div.show_for div.wrapper", /ShowFor/
|
32
|
-
end
|
33
|
-
|
34
|
-
test "show_for accepts an attribute as time, without label and separator" do
|
35
|
-
with_value_for @user, :created_at
|
36
|
-
assert_no_select "div.show_for div.wrapper strong.label"
|
37
|
-
assert_no_select "div.show_for div.wrapper br"
|
38
|
-
assert_select "div.show_for div.wrapper", /#{Regexp.escape(I18n.l(@user.created_at))}/
|
39
|
-
end
|
40
|
-
|
41
|
-
test "show_for accepts an attribute as date, without label and separator" do
|
42
|
-
with_value_for @user, :updated_at
|
43
|
-
assert_no_select "div.show_for div.wrapper strong.label"
|
44
|
-
assert_no_select "div.show_for div.wrapper br"
|
45
|
-
assert_select "div.show_for div.wrapper", /#{Regexp.escape(I18n.l(@user.updated_at))}/
|
46
|
-
end
|
47
|
-
|
48
|
-
test "show_for accepts an attribute as time with format options, without label and separator" do
|
49
|
-
with_value_for @user, :created_at, :format => :long
|
50
|
-
assert_select "div.show_for div.wrapper", /#{Regexp.escape(I18n.l(@user.created_at, :format => :long))}/
|
51
|
-
end
|
52
|
-
|
53
|
-
test "show_for accepts an attribute as nil, without label and separator" do
|
54
|
-
with_value_for @user, :birthday
|
55
|
-
assert_no_select "div.show_for div.wrapper strong.label"
|
56
|
-
assert_no_select "div.show_for div.wrapper br"
|
57
|
-
assert_select "div.show_for div.wrapper", /Not specified/
|
58
|
-
end
|
59
|
-
|
60
|
-
test "show_for accepts blank attributes, without label and separator" do
|
61
|
-
with_value_for @user, :description
|
62
|
-
assert_no_select "div.show_for div.wrapper strong.label"
|
63
|
-
assert_no_select "div.show_for div.wrapper br"
|
64
|
-
assert_select "div.show_for div.wrapper", /Not specified/
|
65
|
-
end
|
66
|
-
|
67
|
-
test "show_for uses :if_blank if attribute is nil, without label and separator" do
|
68
|
-
with_value_for @user, :birthday, :if_blank => "No description provided"
|
69
|
-
assert_no_select "div.show_for div.wrapper strong.label"
|
70
|
-
assert_no_select "div.show_for div.wrapper br"
|
71
|
-
assert_select "div.show_for div.wrapper", /No description provided/
|
72
|
-
end
|
73
|
-
|
74
|
-
test "show_for uses :if_blank if attribute is blank, without label and separator" do
|
75
|
-
with_value_for @user, :description, :if_blank => "No description provided"
|
76
|
-
assert_no_select "div.show_for div.wrapper strong.label"
|
77
|
-
assert_no_select "div.show_for div.wrapper br"
|
78
|
-
assert_select "div.show_for div.wrapper", /No description provided/
|
79
|
-
end
|
80
|
-
|
81
|
-
test "show_for escapes content by default, without label and separator" do
|
82
|
-
@user.name = "<b>hack you!</b>"
|
83
|
-
with_value_for @user, :name
|
84
|
-
assert_no_select "div.show_for div.wrapper strong.label"
|
85
|
-
assert_no_select "div.show_for div.wrapper br"
|
86
|
-
assert_no_select "div.show_for div.wrapper b"
|
87
|
-
assert_select "div.show_for div.wrapper",
|
88
|
-
rails_42? ? "<b>hack you!</b>" : "<b>hack you!</b>"
|
89
|
-
end
|
90
|
-
end
|