show_for 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0ec28a78975708c9329cdb9d30f82d98b713ab4f
4
+ data.tar.gz: 0c59a01bff303106a9a3adbe014ec55656f3b5a5
5
+ SHA512:
6
+ metadata.gz: 655e5c23fc65094e2c833d985c9fdc4a8575c165d9bb969193ec8522f8efa5ac01f30235685a5cc112fc7b6f0a50220afbc560ba2bfcb40a0cce3544ff306072
7
+ data.tar.gz: 58ff69a4ffd41d8c628514b7e37064dad1b12a037011ca8b903e391dee776786f54145bfeb5b8a9988d6399d133556229f1ade3a3c12c20bc0f40d276035e4b0
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,16 @@
1
+ == 0.2.6
2
+
3
+ * enhancements
4
+ * Ruby 2.0 support.
5
+ * Add Haml template. (by github.com/nashby) Closes #12.
6
+ * Add `blank_html` tanslation. (by github.com/nashby)
7
+ * Add `show_for_class` configuration option. (by github.com/nashby)
8
+
9
+ * bug fix
10
+ * Make show_for works with namespaced models. (by github.com/fabiokr)
11
+ * Add `blank_content_class` to the attributes. (by github.com/blakehilscher). Closes #24.
12
+ * Don't call `association.map` if association method is nil (by github.com/nashby). Closes #40.
13
+
1
14
  == 0.2.5
2
15
 
3
16
  * enhancements
@@ -2,6 +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, :desc => 'Template engine to be invoked (erb or haml or slim).'
5
6
  source_root File.expand_path('../templates', __FILE__)
6
7
 
7
8
  def copy_initializers
@@ -13,8 +14,9 @@ module ShowFor
13
14
  end
14
15
 
15
16
  def copy_generator_template
16
- copy_file 'show.html.erb', 'lib/templates/erb/scaffold/show.html.erb'
17
+ engine = options[:template_engine]
18
+ copy_file "show.html.#{engine}", "lib/templates/#{engine}/scaffold/show.html.#{engine}"
17
19
  end
18
20
  end
19
21
  end
20
- end
22
+ end
@@ -1,5 +1,7 @@
1
1
  en:
2
2
  show_for:
3
3
  blank: "Not specified"
4
+ # If you want to use html tags with blank value use blank html translation
5
+ # blank_html: "<span>Not specified</span>"
4
6
  "yes": "Yes"
5
7
  "no": "No"
@@ -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
@@ -3,6 +3,9 @@ ShowFor.setup do |config|
3
3
  # The tag which wraps show_for calls.
4
4
  # config.show_for_tag = :div
5
5
 
6
+ # The DOM class set for show_for tag. Default is nil
7
+ # config.show_for_class = :custom
8
+
6
9
  # The tag which wraps each attribute/association call. Default is :p.
7
10
  # config.wrapper_tag = :dl
8
11
 
@@ -46,7 +46,10 @@ module ShowFor
46
46
  end
47
47
 
48
48
  method = options.delete(:using) || ShowFor.association_methods.find { |m| sample.respond_to?(m) }
49
- association.is_a?(Array) ? association.map(&method) : association.try(method)
49
+
50
+ if method
51
+ association.is_a?(Array) ? association.map(&method) : association.try(method)
52
+ end
50
53
  end
51
54
  end
52
- end
55
+ end
@@ -19,17 +19,26 @@ module ShowFor
19
19
  protected
20
20
 
21
21
  def object_name #:nodoc:
22
- @object_name ||= @object.class.name.underscore
22
+ @object_name ||= begin
23
+ model_name = @object.class.model_name
24
+
25
+ # TODO Remove this check when we drop support to Rails 3.0
26
+ if model_name.respond_to?(:param_key)
27
+ model_name.param_key
28
+ else
29
+ model_name.singular
30
+ end
31
+ end
23
32
  end
24
33
 
25
34
  def wrap_label_and_content(name, value, options, &block) #:nodoc:
26
35
  label = label(name, options, false)
27
36
  label += ShowFor.separator.to_s.html_safe if label.present?
28
- wrap_with(:wrapper, label + content(value, options, false, &block), options)
37
+ wrap_with(:wrapper, label + content(value, options, false, &block), apply_wrapper_options!(:wrapper, options, value))
29
38
  end
30
39
 
31
40
  def wrap_content(name, value, options, &block) #:nodoc:
32
- wrap_with(:wrapper, content(value, options, false, &block), options)
41
+ wrap_with(:wrapper, content(value, options, false, &block), apply_wrapper_options!(:wrapper, options, value))
33
42
  end
34
43
 
35
44
  # Set "#{object_name}_#{attribute_name}" as in the wrapper tag.
@@ -39,6 +48,12 @@ module ShowFor
39
48
  wrapper_html[:class] = "#{html_class} #{wrapper_html[:class]}".rstrip
40
49
  end
41
50
 
51
+ def apply_wrapper_options!(type, options, value)
52
+ options[:"#{type}_html"] ||= {}
53
+ options[:"#{type}_html"][:class] = [options[:"#{type}_html"][:class], ShowFor.blank_content_class].join(' ') if value.blank? && value != false
54
+ options
55
+ end
56
+
42
57
  # Gets the default tag set in ShowFor module and apply (if defined)
43
58
  # around the given content. It also check for html_options in @options
44
59
  # hash related to the current type.
@@ -1,9 +1,12 @@
1
1
  module ShowFor
2
2
  module Content
3
3
  def content(value, options={}, apply_options=true, &block)
4
+
5
+ # cache value for apply_wrapper_options!
6
+ sample_value = value
7
+
4
8
  if value.blank? && value != false
5
- value = options.delete(:if_blank) || I18n.t(:'show_for.blank', :default => "Not specified")
6
- options[:class] = [options[:class], ShowFor.blank_content_class].join(' ')
9
+ value = blank_value(options)
7
10
  end
8
11
 
9
12
  # We need to convert value to_a because when dealing with ActiveRecord
@@ -24,11 +27,15 @@ module ShowFor
24
27
  when NilClass, Numeric
25
28
  value.to_s
26
29
  else
27
- value
30
+ if block
31
+ template.capture(value, &block)
32
+ else
33
+ value
34
+ end
28
35
  end
29
36
 
30
37
  options[:content_html] = options.except(:content_tag) if apply_options
31
- wrap_with(:content, content, options)
38
+ wrap_with(:content, content, apply_wrapper_options!(:content, options, sample_value) )
32
39
  end
33
40
 
34
41
  protected
@@ -42,5 +49,17 @@ module ShowFor
42
49
 
43
50
  wrap_with(:collection, response, options)
44
51
  end
52
+
53
+ def translate_blank_html
54
+ template.t(:'show_for.blank_html', :default => translate_blank_text)
55
+ end
56
+
57
+ def translate_blank_text
58
+ I18n.t(:'show_for.blank', :default => "Not specified")
59
+ end
60
+
61
+ def blank_value(options)
62
+ options.delete(:if_blank) || translate_blank_html
63
+ end
45
64
  end
46
65
  end
@@ -10,16 +10,24 @@ module ShowFor
10
10
  # end
11
11
  #
12
12
  def show_for(object, html_options={}, &block)
13
+ html_options = html_options.dup
14
+
13
15
  tag = html_options.delete(:show_for_tag) || ShowFor.show_for_tag
14
16
 
15
17
  html_options[:id] ||= dom_id(object)
16
- html_options[:class] = "show_for #{dom_class(object)} #{html_options[:class]}".rstrip
18
+ html_options[:class] = show_for_html_class(object, html_options)
17
19
 
18
20
  builder = html_options.delete(:builder) || ShowFor::Builder
19
21
  content = capture(builder.new(object, self), &block)
20
22
 
21
23
  content_tag(tag, content, html_options)
22
24
  end
25
+
26
+ private
27
+
28
+ def show_for_html_class(object, html_options)
29
+ "show_for #{dom_class(object)} #{html_options[:class]} #{ShowFor.show_for_class}".squeeze(" ").rstrip
30
+ end
23
31
  end
24
32
  end
25
33
 
@@ -1,3 +1,3 @@
1
1
  module ShowFor
2
- VERSION = "0.2.5".freeze
2
+ VERSION = "0.2.6".freeze
3
3
  end
data/lib/show_for.rb CHANGED
@@ -7,6 +7,9 @@ module ShowFor
7
7
  mattr_accessor :show_for_tag
8
8
  @@show_for_tag = :div
9
9
 
10
+ mattr_accessor :show_for_class
11
+ @@show_for_class = nil
12
+
10
13
  mattr_accessor :label_tag
11
14
  @@label_tag = :strong
12
15
 
@@ -30,6 +30,26 @@ class AssociationTest < ActionView::TestCase
30
30
  assert_select "div.show_for p.wrapper ul.collection li", "Tag 3"
31
31
  end
32
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 p.wrapper ul.collection"
41
+ assert_no_select "div.show_for p.wrapper", /Enumerator/
42
+ end
43
+ end
44
+
45
+ test "show_for accepts a block with an argument in belongs_to associations" do
46
+ with_association_for @user, :company do |company|
47
+ company.name.upcase
48
+ end
49
+
50
+ assert_select "div.show_for p.wrapper", /PLATAFORMATEC/
51
+ end
52
+
33
53
  test "show_for accepts :using as option to tell how to retrieve association values" do
34
54
  with_association_for @user, :tags, :using => :alternate_name
35
55
  assert_select "div.show_for p.wrapper ul.collection"
@@ -114,6 +114,13 @@ class AttributeTest < ActionView::TestCase
114
114
  end
115
115
  end
116
116
 
117
+ test "show_for accepts not spcified message can be localized with html" do
118
+ store_translations(:en, :show_for => { :blank_html => "<span>OMG! It's blank!</span>" }) do
119
+ with_attribute_for @user, :description
120
+ assert_select "div.show_for p.wrapper span", "OMG! It's blank!"
121
+ end
122
+ end
123
+
117
124
  test "show_for uses :if_blank if attribute is blank" do
118
125
  with_attribute_for @user, :description, :if_blank => "No description provided"
119
126
  assert_select "div.show_for p.wrapper", /No description provided/
@@ -126,6 +133,13 @@ class AttributeTest < ActionView::TestCase
126
133
  assert_select "div.show_for p.wrapper", /This description/
127
134
  end
128
135
 
136
+ test "show_for#content given a block should be wrapped in the result" do
137
+ with_attribute_for @user, :name do |name|
138
+ "<div class='block'>#{name}</div>".html_safe
139
+ end
140
+ assert_select "p.wrapper.user_name div.block", /ShowFor/
141
+ end
142
+
129
143
  test "show_for escapes content by default" do
130
144
  @user.name = "<b>hack you!</b>"
131
145
  with_attribute_for @user, :name
@@ -164,4 +178,13 @@ class AttributeTest < ActionView::TestCase
164
178
  assert_select "div.show_for p.user_email.wrapper", /Not specified/
165
179
  assert_select "p.user_email strong.label", "Email"
166
180
  end
181
+
182
+ test "show_for should wrap blank attributes with no_attribute" do
183
+ swap ShowFor, :blank_content_class => 'no_attribute' do
184
+ with_attributes_for @user, :name, :birthday
185
+ assert_select ".wrapper.user_birthday.no_attribute"
186
+ assert_select ".wrapper.user_name.no_attribute", false
187
+ end
188
+ end
189
+
167
190
  end
data/test/builder_test.rb CHANGED
@@ -18,6 +18,15 @@ class BuilderTest < ActionView::TestCase
18
18
  assert_select "div.show_for p.wrapper"
19
19
  end
20
20
 
21
+ test "show_for properly deals with namespaced models" do
22
+ @user = Namespaced::User.new(:id => 1, :name => "ShowFor")
23
+
24
+ with_attribute_for @user, :name
25
+ assert_select "div.show_for p.namespaced_user_name.wrapper"
26
+ assert_select "div.show_for p.wrapper strong.label"
27
+ assert_select "div.show_for p.wrapper"
28
+ end
29
+
21
30
  test "show_for allows wrapper tag to be changed by attribute" do
22
31
  with_attribute_for @user, :name, :wrapper_tag => :span
23
32
  assert_select "div.show_for span.user_name.wrapper"
@@ -0,0 +1,26 @@
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).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 CHANGED
@@ -37,4 +37,18 @@ class HelperTest < ActionView::TestCase
37
37
  assert_select "p.show_for"
38
38
  end
39
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
+
40
54
  end
@@ -49,3 +49,8 @@ class User < OpenStruct
49
49
  "User"
50
50
  end
51
51
  end
52
+
53
+ module Namespaced
54
+ class User < ::User
55
+ end
56
+ end
data/test/test_helper.rb CHANGED
@@ -9,6 +9,9 @@ require 'action_view'
9
9
  require 'action_view/template'
10
10
  require 'action_view/test_case'
11
11
 
12
+ require "rails/generators/test_case"
13
+ require 'generators/show_for/install_generator'
14
+
12
15
  $:.unshift File.expand_path("../../lib", __FILE__)
13
16
  require 'show_for'
14
17
 
metadata CHANGED
@@ -1,38 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: show_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
5
- prerelease:
4
+ version: 0.2.6
6
5
  platform: ruby
7
6
  authors:
8
7
  - José Valim
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-05-12 00:00:00.000000000 Z
11
+ date: 2013-05-08 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activemodel
16
- requirement: &70225886718860 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
21
19
  version: '3.0'
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *70225886718860
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: actionpack
27
- requirement: &70225886718220 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
31
  - - ~>
31
32
  - !ruby/object:Gem::Version
32
33
  version: '3.0'
33
34
  type: :runtime
34
35
  prerelease: false
35
- version_requirements: *70225886718220
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
36
41
  description: Wrap your objects with a helper to easily show them
37
42
  email: contact@plataformatec.com.br
38
43
  executables: []
@@ -44,6 +49,7 @@ files:
44
49
  - lib/generators/show_for/install_generator.rb
45
50
  - lib/generators/show_for/templates/en.yml
46
51
  - lib/generators/show_for/templates/show.html.erb
52
+ - lib/generators/show_for/templates/show.html.haml
47
53
  - lib/generators/show_for/templates/show_for.rb
48
54
  - lib/generators/show_for/USAGE
49
55
  - lib/show_for/association.rb
@@ -58,6 +64,7 @@ files:
58
64
  - test/attribute_test.rb
59
65
  - test/builder_test.rb
60
66
  - test/content_test.rb
67
+ - test/generators/show_for_generator_test.rb
61
68
  - test/helper_test.rb
62
69
  - test/label_test.rb
63
70
  - test/support/misc_helpers.rb
@@ -66,39 +73,33 @@ files:
66
73
  - test/value_test.rb
67
74
  homepage: http://github.com/plataformatec/show_for
68
75
  licenses: []
76
+ metadata: {}
69
77
  post_install_message:
70
78
  rdoc_options: []
71
79
  require_paths:
72
80
  - lib
73
81
  required_ruby_version: !ruby/object:Gem::Requirement
74
- none: false
75
82
  requirements:
76
- - - ! '>='
83
+ - - '>='
77
84
  - !ruby/object:Gem::Version
78
85
  version: '0'
79
- segments:
80
- - 0
81
- hash: -4500315708210946925
82
86
  required_rubygems_version: !ruby/object:Gem::Requirement
83
- none: false
84
87
  requirements:
85
- - - ! '>='
88
+ - - '>='
86
89
  - !ruby/object:Gem::Version
87
90
  version: '0'
88
- segments:
89
- - 0
90
- hash: -4500315708210946925
91
91
  requirements: []
92
92
  rubyforge_project: show_for
93
- rubygems_version: 1.8.17
93
+ rubygems_version: 2.0.3
94
94
  signing_key:
95
- specification_version: 3
95
+ specification_version: 4
96
96
  summary: Wrap your objects with a helper to easily show them
97
97
  test_files:
98
98
  - test/association_test.rb
99
99
  - test/attribute_test.rb
100
100
  - test/builder_test.rb
101
101
  - test/content_test.rb
102
+ - test/generators/show_for_generator_test.rb
102
103
  - test/helper_test.rb
103
104
  - test/label_test.rb
104
105
  - test/support/misc_helpers.rb