show_for 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
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 p.wrapper strong.label"
8
+ assert_no_select "div.show_for p.wrapper br"
9
+ assert_select "div.show_for p.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 => :p
15
+ assert_no_select "div.show_for p.wrapper strong.label"
16
+ assert_no_select "div.show_for p.wrapper br"
17
+ assert_select "div.show_for p.wrapper p.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 p.wrapper strong.label"
23
+ assert_no_select "div.show_for p.wrapper br"
24
+ assert_select "div.show_for p.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 p.wrapper strong.label"
30
+ assert_no_select "div.show_for p.wrapper br"
31
+ assert_select "div.show_for p.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 p.wrapper strong.label"
37
+ assert_no_select "div.show_for p.wrapper br"
38
+ assert_select "div.show_for p.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 p.wrapper strong.label"
44
+ assert_no_select "div.show_for p.wrapper br"
45
+ assert_select "div.show_for p.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 p.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
+ c = with_value_for @user, :birthday
55
+ assert_no_select "div.show_for p.wrapper strong.label"
56
+ assert_no_select "div.show_for p.wrapper br"
57
+ assert_select "div.show_for p.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 p.wrapper strong.label"
63
+ assert_no_select "div.show_for p.wrapper br"
64
+ assert_select "div.show_for p.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 p.wrapper strong.label"
70
+ assert_no_select "div.show_for p.wrapper br"
71
+ assert_select "div.show_for p.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 p.wrapper strong.label"
77
+ assert_no_select "div.show_for p.wrapper br"
78
+ assert_select "div.show_for p.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 p.wrapper strong.label"
85
+ assert_no_select "div.show_for p.wrapper br"
86
+ assert_no_select "div.show_for p.wrapper b"
87
+ assert_select "div.show_for p.wrapper", /&lt;b&gt;/
88
+ end
89
+ end
metadata CHANGED
@@ -1,47 +1,51 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: show_for
3
- version: !ruby/object:Gem::Version
4
- hash: 31
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.5
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 2
9
- - 4
10
- version: 0.2.4
11
6
  platform: ruby
12
- authors:
13
- - "Jos\xC3\xA9 Valim"
7
+ authors:
8
+ - José Valim
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-02-06 00:00:00 -02:00
19
- default_executable:
20
- dependencies: []
21
-
12
+ date: 2012-05-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: &70225886718860 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70225886718860
25
+ - !ruby/object:Gem::Dependency
26
+ name: actionpack
27
+ requirement: &70225886718220 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70225886718220
22
36
  description: Wrap your objects with a helper to easily show them
23
37
  email: contact@plataformatec.com.br
24
38
  executables: []
25
-
26
39
  extensions: []
27
-
28
40
  extra_rdoc_files: []
29
-
30
- files:
31
- - .gitignore
41
+ files:
32
42
  - CHANGELOG.rdoc
33
- - Gemfile
34
- - Gemfile.lock
35
43
  - MIT-LICENSE
36
- - README.rdoc
37
- - Rakefile
38
- - init.rb
39
- - lib/generators/show_for/USAGE
40
44
  - lib/generators/show_for/install_generator.rb
41
45
  - lib/generators/show_for/templates/en.yml
42
46
  - lib/generators/show_for/templates/show.html.erb
43
47
  - lib/generators/show_for/templates/show_for.rb
44
- - lib/show_for.rb
48
+ - lib/generators/show_for/USAGE
45
49
  - lib/show_for/association.rb
46
50
  - lib/show_for/attribute.rb
47
51
  - lib/show_for/builder.rb
@@ -49,49 +53,55 @@ files:
49
53
  - lib/show_for/helper.rb
50
54
  - lib/show_for/label.rb
51
55
  - lib/show_for/version.rb
52
- - show_for.gemspec
56
+ - lib/show_for.rb
57
+ - test/association_test.rb
58
+ - test/attribute_test.rb
53
59
  - test/builder_test.rb
60
+ - test/content_test.rb
54
61
  - test/helper_test.rb
62
+ - test/label_test.rb
55
63
  - test/support/misc_helpers.rb
56
64
  - test/support/models.rb
57
65
  - test/test_helper.rb
58
- has_rdoc: true
66
+ - test/value_test.rb
59
67
  homepage: http://github.com/plataformatec/show_for
60
68
  licenses: []
61
-
62
69
  post_install_message:
63
70
  rdoc_options: []
64
-
65
- require_paths:
71
+ require_paths:
66
72
  - lib
67
- required_ruby_version: !ruby/object:Gem::Requirement
73
+ required_ruby_version: !ruby/object:Gem::Requirement
68
74
  none: false
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- hash: 3
73
- segments:
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ segments:
74
80
  - 0
75
- version: "0"
76
- required_rubygems_version: !ruby/object:Gem::Requirement
81
+ hash: -4500315708210946925
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
83
  none: false
78
- requirements:
79
- - - ">="
80
- - !ruby/object:Gem::Version
81
- hash: 3
82
- segments:
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ segments:
83
89
  - 0
84
- version: "0"
90
+ hash: -4500315708210946925
85
91
  requirements: []
86
-
87
92
  rubyforge_project: show_for
88
- rubygems_version: 1.5.0
93
+ rubygems_version: 1.8.17
89
94
  signing_key:
90
95
  specification_version: 3
91
96
  summary: Wrap your objects with a helper to easily show them
92
- test_files:
97
+ test_files:
98
+ - test/association_test.rb
99
+ - test/attribute_test.rb
93
100
  - test/builder_test.rb
101
+ - test/content_test.rb
94
102
  - test/helper_test.rb
103
+ - test/label_test.rb
95
104
  - test/support/misc_helpers.rb
96
105
  - test/support/models.rb
97
106
  - test/test_helper.rb
107
+ - test/value_test.rb
data/.gitignore DELETED
@@ -1 +0,0 @@
1
- .bundle/
data/Gemfile DELETED
@@ -1,9 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gem 'rails', '~> 3.0.0'
4
-
5
- if RUBY_VERSION < "1.9"
6
- gem "ruby-debug"
7
- else
8
- gem "test-unit"
9
- end
data/Gemfile.lock DELETED
@@ -1,79 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- abstract (1.0.0)
5
- actionmailer (3.0.3)
6
- actionpack (= 3.0.3)
7
- mail (~> 2.2.9)
8
- actionpack (3.0.3)
9
- activemodel (= 3.0.3)
10
- activesupport (= 3.0.3)
11
- builder (~> 2.1.2)
12
- erubis (~> 2.6.6)
13
- i18n (~> 0.4)
14
- rack (~> 1.2.1)
15
- rack-mount (~> 0.6.13)
16
- rack-test (~> 0.5.6)
17
- tzinfo (~> 0.3.23)
18
- activemodel (3.0.3)
19
- activesupport (= 3.0.3)
20
- builder (~> 2.1.2)
21
- i18n (~> 0.4)
22
- activerecord (3.0.3)
23
- activemodel (= 3.0.3)
24
- activesupport (= 3.0.3)
25
- arel (~> 2.0.2)
26
- tzinfo (~> 0.3.23)
27
- activeresource (3.0.3)
28
- activemodel (= 3.0.3)
29
- activesupport (= 3.0.3)
30
- activesupport (3.0.3)
31
- arel (2.0.4)
32
- builder (2.1.2)
33
- columnize (0.3.1)
34
- erubis (2.6.6)
35
- abstract (>= 1.0.0)
36
- i18n (0.4.2)
37
- linecache (0.43)
38
- mail (2.2.10)
39
- activesupport (>= 2.3.6)
40
- i18n (~> 0.4.1)
41
- mime-types (~> 1.16)
42
- treetop (~> 1.4.8)
43
- mime-types (1.16)
44
- polyglot (0.3.1)
45
- rack (1.2.1)
46
- rack-mount (0.6.13)
47
- rack (>= 1.0.0)
48
- rack-test (0.5.6)
49
- rack (>= 1.0)
50
- rails (3.0.3)
51
- actionmailer (= 3.0.3)
52
- actionpack (= 3.0.3)
53
- activerecord (= 3.0.3)
54
- activeresource (= 3.0.3)
55
- activesupport (= 3.0.3)
56
- bundler (~> 1.0)
57
- railties (= 3.0.3)
58
- railties (3.0.3)
59
- actionpack (= 3.0.3)
60
- activesupport (= 3.0.3)
61
- rake (>= 0.8.7)
62
- thor (~> 0.14.4)
63
- rake (0.8.7)
64
- ruby-debug (0.10.3)
65
- columnize (>= 0.1)
66
- ruby-debug-base (~> 0.10.3.0)
67
- ruby-debug-base (0.10.3)
68
- linecache (>= 0.3)
69
- thor (0.14.6)
70
- treetop (1.4.9)
71
- polyglot (>= 0.3.1)
72
- tzinfo (0.3.23)
73
-
74
- PLATFORMS
75
- ruby
76
-
77
- DEPENDENCIES
78
- rails (~> 3.0.0)
79
- ruby-debug
data/README.rdoc DELETED
@@ -1,182 +0,0 @@
1
- == ShowFor
2
-
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 :nickname, :in => :profile %>
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
-
13
- <% u.attribute :photo do %>
14
- <%= image_tag(@user.photo_url) %>
15
- <% end %>
16
-
17
- <%= u.association :company %>
18
- <%= u.association :tags, :to_sentence => true %>
19
- <% end %>
20
-
21
- == Installation
22
-
23
- Install the gem:
24
-
25
- sudo gem install show_for
26
-
27
- Or add ShowFor to your Gemfile and bundle it up:
28
-
29
- gem 'show_for'
30
-
31
- Run the generator:
32
-
33
- rails generate show_for:install
34
-
35
- And you are ready to go. Since this branch is aims Rails 3 support,
36
- if you want to use it with Rails 2.3 you should check this branch:
37
-
38
- http://github.com/plataformatec/show_for/tree/v0.1
39
-
40
- == Usage
41
-
42
- ShowFor allows you to quickly show a model information with I18n features.
43
-
44
- <%= show_for @admin do |a| %>
45
- <%= a.attribute :name %>
46
- <%= a.attribute :confirmed? %>
47
- <%= a.attribute :created_at, :format => :short %>
48
- <%= a.attribute :last_sign_in_at, :if_blank => "Administrator did not access yet"
49
- :wrapper_html => { :id => "sign_in_timestamp" } %>
50
-
51
- <% a.attribute :photo do %>
52
- <%= image_tag(@admin.photo_url) %>
53
- <% end %>
54
-
55
- <% a.value :biography %>
56
- <% end %>
57
-
58
- Will generate something like:
59
-
60
- <div id="admin_1" class="show_for admin">
61
- <p class="wrapper admin_name">
62
- <strong class="label">Name</strong><br />
63
- José Valim
64
- </p>
65
- <p class="wrapper admin_confirmed">
66
- <strong class="label">Confirmed?</strong><br />
67
- Yes
68
- </p>
69
- <p class="wrapper admin_created_at">
70
- <strong class="label">Created at</strong><br />
71
- 13/12/2009 - 19h17
72
- </p>
73
- <p id="sign_in_timestamp" class="wrapper admin_last_sign_in_at">
74
- <strong class="label">Last sign in at</strong><br />
75
- Administrator did not access yet
76
- </p>
77
- <p class="wrapper admin_photo">
78
- <strong class="label">Photo</strong><br />
79
- <img src="path/to/photo" />
80
- </p>
81
- <p class="wrapper admin_biography">
82
- Etiam porttitor eros ut diam vestibulum et blandit lectus tempor. Donec venenatis fermentum nunc ac dignissim.
83
- Pellentesque volutpat eros quis enim mollis bibendum. Ut cursus sem ac sem accumsan nec porttitor felis luctus.
84
- Sed purus nunc, auctor vitae consectetur pharetra, tristique non nisi.
85
- </p>
86
- </div>
87
-
88
- You also have the possibility to show a list of attributes, useful if you don't need to change any configuration:
89
-
90
- <%= show_for @admin do |a| %>
91
- <%= a.attributes :name, :confirmed?, :created_at %>
92
- <% end %>
93
-
94
- == Value lookup
95
-
96
- To show the proper value, before retrieving the attribute value, show_for first looks if a
97
- block without argument was given, otherwise checks if a :"human_#{attribute}" method is defined
98
- and, if not, only then retrieve the attribute.
99
-
100
- == Options
101
-
102
- show_for handles a series of options. Those are:
103
-
104
- * :escape * - When the attribute should be escaped. True by default.
105
-
106
- * :format * - Sent to I18n.localize when the attribute is a date/time object.
107
-
108
- * :if_blank * - An object to be used if the value is blank. Not escaped as well.
109
-
110
- Besides, all containers (:label, :content and :wrapper) can have their html
111
- options configured through the :label_html, :content_html and :wrapper_html
112
- options. Containers can have their tags configured on demand as well through
113
- :label_tag, :content_tag and :wrapper_tag options.
114
-
115
- == Label
116
-
117
- show_for also exposes the label method. In case you want to use the default
118
- human_attribute_name lookup and the default wrapping:
119
-
120
- a.label :name #=> <strong class="label">Name</strong>
121
- a.label "Name", :id => "my_name" #=> <strong class="label" id="my_name">Name</strong>
122
-
123
- Optionally, if you want to wrap the inner part of the label with some text
124
- (e.g. adding a semicolon), you can do so by specifying a proc for ShowFor.label_proc
125
- that will be called with any label text. E.g.:
126
-
127
- ShowFor.label_proc = lambda { |l| l + ":" }
128
-
129
- When taking this route, you can also skip on a per label basis by passing the
130
- :wrap_label option with a value of false.
131
-
132
- == Associations
133
-
134
- show_for also supports associations.
135
-
136
- <%= show_for @artwork do |a| %>
137
- <%= a.association :artist %>
138
- <%= a.association :artist, :using => :name_with_title %>
139
- <%= a.attribute :name_with_title, :in => :artist %>
140
-
141
- <%= a.association :tags %>
142
- <%= a.association :tags, :to_sentence => true %>
143
- <% a.association :tags do
144
- @artwork.tags.map(&:name).to_sentence
145
- end %>
146
-
147
- <% a.association :fans, :collection_tag => :ol do |fan| %>
148
- <li><%= link_to fan.name, fan %></li>
149
- <% end %>
150
- <% end %>
151
-
152
- The first is a has_one or belongs_to association, which works like an attribute
153
- to show_for, except it will retrieve the artist association and try to find a
154
- proper method from ShowFor.association_methods to be used. You can pass
155
- the option :using to tell (and not guess) which method from the association
156
- to use.
157
-
158
- :tags is a has_and_belongs_to_many association which will return a collection.
159
- show_for can handle collections by default by wrapping them in list (<ul> with
160
- each item wrapped by an <li>). However, it also allows you to give :to_sentence
161
- or :join it you want to render them inline.
162
-
163
- You can also pass a block which expects an argument to association. In such cases,
164
- a wrapper for the collection is still created and the block just iterates over the
165
- collection objects.
166
-
167
- == Maintainers
168
-
169
- * José Valim (http://github.com/josevalim)
170
-
171
- == Contributors
172
-
173
- * Jonas Grimfelt (http://github.com/grimen)
174
-
175
- == Bugs and Feedback
176
-
177
- If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
178
-
179
- http://github.com/plataformatec/show_for/issues
180
-
181
- MIT License. Copyright 2010 Plataforma Tecnologia. http://blog.plataformatec.com.br
182
-
data/Rakefile DELETED
@@ -1,27 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'bundler'
4
- Bundler::GemHelper.install_tasks
5
-
6
- require 'rake/testtask'
7
- require 'rake/rdoctask'
8
-
9
- desc 'Default: run unit tests.'
10
- task :default => :test
11
-
12
- desc 'Test the show_form plugin.'
13
- Rake::TestTask.new(:test) do |t|
14
- t.libs << 'lib'
15
- t.libs << 'test'
16
- t.pattern = 'test/**/*_test.rb'
17
- t.verbose = true
18
- end
19
-
20
- desc 'Generate documentation for the show_for plugin.'
21
- Rake::RDocTask.new(:rdoc) do |rdoc|
22
- rdoc.rdoc_dir = 'rdoc'
23
- rdoc.title = 'ShowFor'
24
- rdoc.options << '--line-numbers' << '--inline-source'
25
- rdoc.rdoc_files.include('README.rdoc')
26
- rdoc.rdoc_files.include('lib/**/*.rb')
27
- end
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require 'show_for'
data/show_for.gemspec DELETED
@@ -1,21 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "show_for/version"
4
-
5
- Gem::Specification.new do |s|
6
- s.name = "show_for"
7
- s.version = ShowFor::VERSION.dup
8
- s.platform = Gem::Platform::RUBY
9
- s.summary = "Wrap your objects with a helper to easily show them"
10
- s.email = "contact@plataformatec.com.br"
11
- s.homepage = "http://github.com/plataformatec/show_for"
12
- s.description = "Wrap your objects with a helper to easily show them"
13
- s.authors = ['José Valim']
14
-
15
- s.files = `git ls-files`.split("\n")
16
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
- s.require_paths = ["lib"]
19
-
20
- s.rubyforge_project = "show_for"
21
- end