simple_show 0.1.1 → 1.0.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 +7 -0
- data/.gitignore +1 -0
- data/Gemfile +3 -2
- data/README.rdoc +13 -2
- data/Rakefile +1 -1
- data/lib/generators/simple_show/install_generator.rb +1 -2
- data/lib/simple_show.rb +19 -9
- data/lib/simple_show/action_view_extensions/form_helper.rb +3 -4
- data/lib/simple_show/base.rb +80 -28
- data/lib/simple_show/version.rb +1 -1
- data/simple_show.gemspec +12 -12
- data/test/skip_helper.rb +128 -0
- data/test/test_helper.rb +54 -13
- data/test/test_simple_show.rb +16 -14
- data/test/test_simple_show_for.rb +11 -13
- data/test/test_simple_show_label.rb +2 -4
- data/test/test_simple_show_show.rb +3 -79
- data/test/test_simple_show_show_collection.rb +36 -0
- data/test/test_simple_show_value.rb +13 -14
- metadata +29 -45
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bda67008a5f7214b1c1ebcfcce7bcc358ff62920
|
4
|
+
data.tar.gz: 60b9f8ece19da67fea73d5d891f247baf038dbc8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 41ceb31d5349c3226378646a39aeb827de32c98aa88bda2ab27bc81c111b9f07a7d37caa1602fd66e112468e3e800647c750fac6e2d5ae80f917d0e12816df7c
|
7
|
+
data.tar.gz: c495af096bf0c3c4a98088cda34fed2fe3ca3292df0a0ed5e369dbcf787d718105cc779ba2adafaa8de82dba9e0aed9dbe3d43b99e11ea83bec05f6518d3bc00
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
source
|
1
|
+
source 'http://rubygems.org'
|
2
2
|
|
3
3
|
group :test do
|
4
|
-
gem 'rake', '
|
4
|
+
gem 'rake', '10.4.2'
|
5
5
|
gem 'rails', '3.1.3'
|
6
6
|
gem 'sqlite3'
|
7
7
|
gem 'nokogiri'
|
8
|
+
gem 'test-unit'
|
8
9
|
end
|
9
10
|
|
10
11
|
# Specify your gem's dependencies in simple_show.gemspec
|
data/README.rdoc
CHANGED
@@ -39,10 +39,15 @@ In our show action we can then do this:
|
|
39
39
|
<%= s.show :cash_in_wallet, :to_currency => true %>
|
40
40
|
<%= s.show :height, :if => :tall %>
|
41
41
|
<%= s.show :weight, :unless => :sensitive %>
|
42
|
+
<%= s.show :title, :unless_attr => :nil? %>
|
43
|
+
<%= s.show_collection :friends do |friend| %>
|
44
|
+
<%= friend.name %> - <%= friend.age %>
|
45
|
+
<% end %>
|
42
46
|
<% end %>
|
43
47
|
|
44
48
|
Which would generate (using the defaults) output like this, assuming that
|
45
|
-
@
|
49
|
+
@johndoe.tall returns false, @johndoe.sensitive returns true, and
|
50
|
+
@johndoe.title is nil:
|
46
51
|
|
47
52
|
<div class="simple_show person" id="person_123">
|
48
53
|
<div class="show">
|
@@ -73,6 +78,13 @@ Which would generate (using the defaults) output like this, assuming that
|
|
73
78
|
<label>Cash in wallet:</label>
|
74
79
|
<span class="value">$123.45</span>
|
75
80
|
</div>
|
81
|
+
<div class="show">
|
82
|
+
<label>Friends:</label>
|
83
|
+
<ul class="show_collection">
|
84
|
+
<li class="show_collection_item">Jim - 32</li>
|
85
|
+
<li class="show_collection_item">Tina - 23</li>
|
86
|
+
</ul>
|
87
|
+
</div>
|
76
88
|
<br clear="all">
|
77
89
|
</div>
|
78
90
|
|
@@ -84,7 +96,6 @@ if you don't want that BR tag then set SimpleShow.clear_on_close to false.
|
|
84
96
|
|
85
97
|
== TODO:
|
86
98
|
|
87
|
-
- Add support for associations.
|
88
99
|
- Add options for booleans.
|
89
100
|
|
90
101
|
== License:
|
data/Rakefile
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
module SimpleShow
|
2
2
|
module Generators
|
3
3
|
class InstallGenerator < Rails::Generators::Base
|
4
|
-
desc
|
4
|
+
desc 'Copy SimpleShow configuration'
|
5
5
|
source_root File.expand_path('../templates', __FILE__)
|
6
6
|
|
7
7
|
def copy_config
|
8
8
|
directory 'config'
|
9
9
|
end
|
10
|
-
|
11
10
|
end
|
12
11
|
end
|
13
12
|
end
|
data/lib/simple_show.rb
CHANGED
@@ -3,7 +3,6 @@ require 'simple_show/action_view_extensions/form_helper'
|
|
3
3
|
require 'simple_show/base'
|
4
4
|
|
5
5
|
module SimpleShow
|
6
|
-
|
7
6
|
mattr_accessor :show_class
|
8
7
|
@@show_class = :simple_show
|
9
8
|
|
@@ -37,18 +36,30 @@ module SimpleShow
|
|
37
36
|
mattr_accessor :value_suffix
|
38
37
|
@@value_suffix = nil
|
39
38
|
|
39
|
+
mattr_accessor :collection_tag
|
40
|
+
@@collection_tag = :ul
|
41
|
+
|
42
|
+
mattr_accessor :collection_class
|
43
|
+
@@collection_class = :show_collection
|
44
|
+
|
45
|
+
mattr_accessor :collection_item_tag
|
46
|
+
@@collection_item_tag = :li
|
47
|
+
|
48
|
+
mattr_accessor :collection_item_class
|
49
|
+
@@collection_item_class = :show_collection_item
|
50
|
+
|
40
51
|
mattr_accessor :clear_on_close
|
41
52
|
@@clear_on_close = true
|
42
53
|
|
43
54
|
mattr_accessor :helpers
|
44
55
|
@@helpers = {
|
45
|
-
:
|
46
|
-
:
|
47
|
-
:
|
48
|
-
:
|
49
|
-
:
|
50
|
-
:
|
51
|
-
:
|
56
|
+
to_currency: :number_to_currency,
|
57
|
+
to_human: :number_to_human,
|
58
|
+
to_human_size: :number_to_human_size,
|
59
|
+
to_percentage: :number_to_percentage,
|
60
|
+
to_phone: :number_to_phone,
|
61
|
+
with_delimiter: :number_with_delimiter,
|
62
|
+
with_precision: :number_with_precision
|
52
63
|
}
|
53
64
|
|
54
65
|
# Default way to setup SimpleShow. Run rails generate simple_show:install
|
@@ -56,5 +67,4 @@ module SimpleShow
|
|
56
67
|
def self.setup
|
57
68
|
yield self
|
58
69
|
end
|
59
|
-
|
60
70
|
end
|
@@ -2,20 +2,19 @@ module SimpleShow
|
|
2
2
|
module ActionViewExtensions
|
3
3
|
module FormHelper
|
4
4
|
def simple_show_for(record, options = {}, &block)
|
5
|
-
|
5
|
+
fail ArgumentError, 'Missing block' unless block_given?
|
6
6
|
|
7
7
|
options[:html] ||= {}
|
8
8
|
options[:html][:id] ||= dom_id(record)
|
9
9
|
options[:html][:class] = "#{SimpleShow.show_class} #{dom_class(record)} #{options[:html][:class]}".strip
|
10
10
|
|
11
11
|
output = capture(SimpleShow::Base.new(self, record, options), &block)
|
12
|
-
output.concat content_tag(:div, '', :
|
12
|
+
output.concat content_tag(:div, '', class: 'clear') if SimpleShow.clear_on_close
|
13
13
|
|
14
|
-
content_tag(:div, output, :
|
14
|
+
content_tag(:div, output, id: options[:html][:id], class: options[:html][:class])
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
ActionView::Base.send :include, SimpleShow::ActionViewExtensions::FormHelper
|
21
|
-
|
data/lib/simple_show/base.rb
CHANGED
@@ -7,42 +7,43 @@ module SimpleShow
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def show(attr, options = {}, &block)
|
10
|
-
if
|
11
|
-
case options[:if]
|
12
|
-
when Symbol
|
13
|
-
options[:if] = @record.send(options[:if])
|
14
|
-
when Proc
|
15
|
-
options[:if] = options[:if].call
|
16
|
-
end
|
17
|
-
return nil if options[:if] == false
|
18
|
-
end
|
19
|
-
if options.key? :unless
|
20
|
-
case options[:unless]
|
21
|
-
when Symbol
|
22
|
-
options[:unless] = @record.send(options[:unless])
|
23
|
-
when Proc
|
24
|
-
options[:unless] = options[:unless].call
|
25
|
-
end
|
26
|
-
return nil unless options[:unless] == false
|
27
|
-
end
|
10
|
+
return nil if skip?(attr, options)
|
28
11
|
|
29
12
|
output = label(attr, options, &block)
|
30
13
|
output += value(attr, options, &block)
|
31
14
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
15
|
+
wrap_output(SimpleShow.wrapper_tag, SimpleShow.wrapper_class, output)
|
16
|
+
end
|
17
|
+
|
18
|
+
def show_collection(attr, options = {}, &block)
|
19
|
+
return nil if skip?(attr, options)
|
20
|
+
|
21
|
+
output = label(attr, options, &block)
|
22
|
+
|
23
|
+
value = wrap_output(SimpleShow.collection_tag, SimpleShow.collection_class) do
|
24
|
+
@record.send(attr).map do |attr_item|
|
25
|
+
wrap_output(SimpleShow.collection_item_tag, SimpleShow.collection_item_class) do
|
26
|
+
block_given? ? @binding.capture(attr_item, &block) : attr_item
|
27
|
+
end
|
28
|
+
end.join.html_safe
|
36
29
|
end
|
30
|
+
|
31
|
+
output += value
|
32
|
+
|
33
|
+
wrap_output(SimpleShow.wrapper_tag, SimpleShow.wrapper_class, output)
|
37
34
|
end
|
38
35
|
|
39
36
|
def label(attr, options = {}, &block)
|
40
|
-
|
41
|
-
|
42
|
-
|
37
|
+
return nil if skip?(attr, options)
|
38
|
+
|
39
|
+
output = [SimpleShow.label_prefix, options[:label] || @record.class.human_attribute_name(attr), SimpleShow.label_suffix].compact.join
|
40
|
+
|
41
|
+
wrap_output(SimpleShow.label_tag, SimpleShow.label_class, output)
|
43
42
|
end
|
44
43
|
|
45
44
|
def value(attr, options = {}, &block)
|
45
|
+
return nil if skip?(attr, options)
|
46
|
+
|
46
47
|
if block_given?
|
47
48
|
value = yield(@record)
|
48
49
|
else
|
@@ -63,15 +64,66 @@ module SimpleShow
|
|
63
64
|
end
|
64
65
|
end
|
65
66
|
|
67
|
+
output = [SimpleShow.value_prefix, value, SimpleShow.value_suffix].compact.join.html_safe
|
66
68
|
field_type = @record.class.columns_hash[attr.to_s].instance_variable_get(:@type)
|
67
|
-
|
68
|
-
|
69
|
-
|
69
|
+
html_class = [SimpleShow.value_class, field_type].compact
|
70
|
+
|
71
|
+
wrap_output(SimpleShow.value_tag, html_class, output)
|
70
72
|
end
|
71
73
|
|
72
74
|
def object
|
73
75
|
@record
|
74
76
|
end
|
75
77
|
|
78
|
+
private
|
79
|
+
|
80
|
+
def wrap_output(tag, html_class, output=nil, &block)
|
81
|
+
output = yield if block_given?
|
82
|
+
if tag.nil?
|
83
|
+
output
|
84
|
+
else
|
85
|
+
@binding.content_tag(tag, output, :class => html_class)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def skip?(attr, options)
|
90
|
+
if options.key? :if
|
91
|
+
case options[:if]
|
92
|
+
when Symbol
|
93
|
+
options[:if] = @record.send(options[:if])
|
94
|
+
when Proc
|
95
|
+
options[:if] = options[:if].call
|
96
|
+
end
|
97
|
+
return true if options[:if] == false
|
98
|
+
end
|
99
|
+
|
100
|
+
if options.key? :unless
|
101
|
+
case options[:unless]
|
102
|
+
when Symbol
|
103
|
+
options[:unless] = @record.send(options[:unless])
|
104
|
+
when Proc
|
105
|
+
options[:unless] = options[:unless].call
|
106
|
+
end
|
107
|
+
return true unless options[:unless] == false
|
108
|
+
end
|
109
|
+
|
110
|
+
if options.key? :if_attr
|
111
|
+
case options[:if_attr]
|
112
|
+
when Symbol
|
113
|
+
options[:if_attr] = @record.send(attr).send(options[:if_attr])
|
114
|
+
end
|
115
|
+
return true if options[:if_attr] == false
|
116
|
+
end
|
117
|
+
|
118
|
+
if options.key? :unless_attr
|
119
|
+
case options[:unless_attr]
|
120
|
+
when Symbol
|
121
|
+
options[:unless_attr] = @record.send(attr).send(options[:unless_attr])
|
122
|
+
end
|
123
|
+
return true unless options[:unless_attr] == false
|
124
|
+
end
|
125
|
+
|
126
|
+
return false
|
127
|
+
end
|
76
128
|
end
|
77
129
|
end
|
data/lib/simple_show/version.rb
CHANGED
data/simple_show.gemspec
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
require
|
2
|
+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'simple_show/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
|
-
s.name =
|
6
|
+
s.name = 'simple_show'
|
7
7
|
s.version = SimpleShow::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors = [
|
10
|
-
s.email = [
|
11
|
-
s.homepage =
|
12
|
-
s.summary =
|
13
|
-
s.description =
|
14
|
-
s.license =
|
9
|
+
s.authors = ['Philip Hallstrom']
|
10
|
+
s.email = ['philip@pjkh.com']
|
11
|
+
s.homepage = ''
|
12
|
+
s.summary = 'SimpleShow is to #show what SimpleForm is to #edit'
|
13
|
+
s.description = 'SimpleShow is to #show what SimpleForm is to #edit'
|
14
|
+
s.license = 'MIT'
|
15
15
|
|
16
|
-
s.rubyforge_project =
|
16
|
+
s.rubyforge_project = 'simple_show'
|
17
17
|
|
18
18
|
s.files = `git ls-files`.split("\n")
|
19
19
|
s.files = s.files.delete_if { |path| path =~ /\.rvmrc/ }
|
20
20
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
-
s.require_paths = [
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
22
|
+
s.require_paths = ['lib']
|
23
23
|
end
|
data/test/skip_helper.rb
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
def test_skip(method, field1, field2, label1, label2)
|
2
|
+
first_label = field1.to_s.titleize
|
3
|
+
|
4
|
+
test ':if => true/false' do
|
5
|
+
doc = Nokogiri::HTML.fragment(
|
6
|
+
simple_show_for(@philip) do |s|
|
7
|
+
o = ActiveSupport::SafeBuffer.new
|
8
|
+
o += s.send(method, field1, if: true)
|
9
|
+
o += s.send(method, field2, if: false)
|
10
|
+
o
|
11
|
+
end
|
12
|
+
)
|
13
|
+
assert_equal [label1], doc.css('label').map(&:text)
|
14
|
+
end
|
15
|
+
|
16
|
+
test ':if => :record_method' do
|
17
|
+
def @philip.method_returning_true
|
18
|
+
true
|
19
|
+
end
|
20
|
+
def @philip.method_returning_false
|
21
|
+
false
|
22
|
+
end
|
23
|
+
doc = Nokogiri::HTML.fragment(
|
24
|
+
simple_show_for(@philip) do |s|
|
25
|
+
o = ActiveSupport::SafeBuffer.new
|
26
|
+
o += s.send(method, field1, if: :method_returning_true)
|
27
|
+
o += s.send(method, field2, if: :method_returning_false)
|
28
|
+
o
|
29
|
+
end
|
30
|
+
)
|
31
|
+
assert_equal [label1], doc.css('label').map(&:text)
|
32
|
+
end
|
33
|
+
|
34
|
+
test ':if => {lambda}' do
|
35
|
+
doc = Nokogiri::HTML.fragment(
|
36
|
+
simple_show_for(@philip) do |s|
|
37
|
+
o = ActiveSupport::SafeBuffer.new
|
38
|
+
o += s.send(method, field1, if: -> { true })
|
39
|
+
o += s.send(method, field2, if: -> { false })
|
40
|
+
o
|
41
|
+
end
|
42
|
+
)
|
43
|
+
assert_equal [label1], doc.css('label').map(&:text)
|
44
|
+
end
|
45
|
+
|
46
|
+
test ':unless => true/false' do
|
47
|
+
doc = Nokogiri::HTML.fragment(
|
48
|
+
simple_show_for(@philip) do |s|
|
49
|
+
o = ActiveSupport::SafeBuffer.new
|
50
|
+
o += s.send(method, field1, unless: true)
|
51
|
+
o += s.send(method, field2, unless: false)
|
52
|
+
o
|
53
|
+
end
|
54
|
+
)
|
55
|
+
assert_equal [label2], doc.css('label').map(&:text)
|
56
|
+
end
|
57
|
+
|
58
|
+
test ':unless => :record_method' do
|
59
|
+
def @philip.method_returning_true
|
60
|
+
true
|
61
|
+
end
|
62
|
+
def @philip.method_returning_false
|
63
|
+
false
|
64
|
+
end
|
65
|
+
doc = Nokogiri::HTML.fragment(
|
66
|
+
simple_show_for(@philip) do |s|
|
67
|
+
o = ActiveSupport::SafeBuffer.new
|
68
|
+
o += s.send(method, field1, unless: :method_returning_true)
|
69
|
+
o += s.send(method, field2, unless: :method_returning_false)
|
70
|
+
o
|
71
|
+
end
|
72
|
+
)
|
73
|
+
assert_equal [label2], doc.css('label').map(&:text)
|
74
|
+
end
|
75
|
+
|
76
|
+
test ':unless => {lambda}' do
|
77
|
+
doc = Nokogiri::HTML.fragment(
|
78
|
+
simple_show_for(@philip) do |s|
|
79
|
+
o = ActiveSupport::SafeBuffer.new
|
80
|
+
o += s.send(method, field1, unless: -> { true })
|
81
|
+
o += s.send(method, field2, unless: -> { false })
|
82
|
+
o
|
83
|
+
end
|
84
|
+
)
|
85
|
+
assert_equal [label2], doc.css('label').map(&:text)
|
86
|
+
end
|
87
|
+
|
88
|
+
test ':if_attr => :record_method' do
|
89
|
+
field1_attr = @philip.send(field1)
|
90
|
+
def field1_attr.method_returning_true
|
91
|
+
true
|
92
|
+
end
|
93
|
+
field2_attr = @philip.send(field2)
|
94
|
+
def field2_attr.method_returning_false
|
95
|
+
false
|
96
|
+
end
|
97
|
+
doc = Nokogiri::HTML.fragment(
|
98
|
+
simple_show_for(@philip) do |s|
|
99
|
+
o = ActiveSupport::SafeBuffer.new
|
100
|
+
o += s.send(method, field1, if_attr: :method_returning_true)
|
101
|
+
o += s.send(method, field2, if_attr: :method_returning_false)
|
102
|
+
o
|
103
|
+
end
|
104
|
+
)
|
105
|
+
assert_equal [label1], doc.css('label').map(&:text)
|
106
|
+
end
|
107
|
+
|
108
|
+
test ':unless_attr => :record_method' do
|
109
|
+
field1_attr = @philip.send(field1)
|
110
|
+
def field1_attr.method_returning_true
|
111
|
+
true
|
112
|
+
end
|
113
|
+
field2_attr = @philip.send(field2)
|
114
|
+
def field2_attr.method_returning_false
|
115
|
+
false
|
116
|
+
end
|
117
|
+
doc = Nokogiri::HTML.fragment(
|
118
|
+
simple_show_for(@philip) do |s|
|
119
|
+
o = ActiveSupport::SafeBuffer.new
|
120
|
+
o += s.send(method, field1, unless_attr: :method_returning_true)
|
121
|
+
o += s.send(method, field2, unless_attr: :method_returning_false)
|
122
|
+
o
|
123
|
+
end
|
124
|
+
)
|
125
|
+
assert_equal [label2], doc.css('label').map(&:text)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
data/test/test_helper.rb
CHANGED
@@ -16,17 +16,32 @@ require 'simple_show'
|
|
16
16
|
|
17
17
|
################################################################################
|
18
18
|
|
19
|
-
ActiveRecord::Base.establish_connection(:
|
19
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
20
20
|
ActiveRecord::Migration.verbose = false
|
21
21
|
|
22
|
-
ActiveRecord::Schema.define(:
|
22
|
+
ActiveRecord::Schema.define(version: 1) do
|
23
23
|
create_table :golfers do |t|
|
24
24
|
t.column :name, :string
|
25
25
|
t.column :phone, :string
|
26
26
|
t.column :email, :string
|
27
27
|
t.column :born_on, :date
|
28
28
|
t.column :is_left_handed, :boolean
|
29
|
-
t.column :handicap, :decimal, :
|
29
|
+
t.column :handicap, :decimal, scale: 3, precision: 1
|
30
|
+
t.timestamps
|
31
|
+
end
|
32
|
+
|
33
|
+
create_table :rounds do |t|
|
34
|
+
t.references :golfer
|
35
|
+
|
36
|
+
t.column :name, :string
|
37
|
+
t.column :score, :integer
|
38
|
+
t.timestamps
|
39
|
+
end
|
40
|
+
|
41
|
+
create_table :clubs do |t|
|
42
|
+
t.references :golfer
|
43
|
+
|
44
|
+
t.column :name, :string
|
30
45
|
t.timestamps
|
31
46
|
end
|
32
47
|
end
|
@@ -38,6 +53,24 @@ end
|
|
38
53
|
################################################################################
|
39
54
|
|
40
55
|
class Golfer < ActiveRecord::Base
|
56
|
+
has_many :clubs
|
57
|
+
has_many :rounds
|
58
|
+
end
|
59
|
+
|
60
|
+
class Round < ActiveRecord::Base
|
61
|
+
belongs_to :golfer
|
62
|
+
|
63
|
+
def to_s
|
64
|
+
name
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class Club < ActiveRecord::Base
|
69
|
+
belongs_to :golfer
|
70
|
+
|
71
|
+
def to_s
|
72
|
+
name
|
73
|
+
end
|
41
74
|
end
|
42
75
|
|
43
76
|
################################################################################
|
@@ -46,14 +79,13 @@ module ActionView
|
|
46
79
|
module Helpers
|
47
80
|
module TagHelper
|
48
81
|
def piglatin(str)
|
49
|
-
str[1..-1] + str[0,1] + 'ay'
|
82
|
+
str[1..-1] + str[0, 1] + 'ay'
|
50
83
|
end
|
51
84
|
end
|
52
85
|
end
|
53
86
|
end
|
54
|
-
|
55
|
-
################################################################################
|
56
87
|
|
88
|
+
################################################################################
|
57
89
|
|
58
90
|
class SimpleShowTestCase < ActiveSupport::TestCase
|
59
91
|
include SimpleShow::ActionViewExtensions::FormHelper
|
@@ -65,13 +97,22 @@ class SimpleShowTestCase < ActiveSupport::TestCase
|
|
65
97
|
|
66
98
|
def setup
|
67
99
|
@philip = Golfer.create!(
|
68
|
-
:
|
69
|
-
:
|
70
|
-
:
|
71
|
-
:
|
72
|
-
:
|
73
|
-
:
|
100
|
+
name: 'Philip Hallstrom',
|
101
|
+
phone: '3604801209',
|
102
|
+
email: 'philip@pjkh.com',
|
103
|
+
born_on: Date.civil(1974, 5, 24),
|
104
|
+
is_left_handed: true,
|
105
|
+
handicap: 6.5
|
106
|
+
)
|
107
|
+
@round1 = Round.create!(
|
108
|
+
name: 'Round 1',
|
109
|
+
score: 72,
|
110
|
+
golfer: @philip
|
111
|
+
)
|
112
|
+
@round2 = Round.create!(
|
113
|
+
name: 'Round 2',
|
114
|
+
score: 68,
|
115
|
+
golfer: @philip
|
74
116
|
)
|
75
117
|
end
|
76
118
|
end
|
77
|
-
|
data/test/test_simple_show.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class SimpleShowTest < SimpleShowTestCase
|
4
|
-
|
5
4
|
test 'setup block yields self' do
|
6
5
|
SimpleShow.setup do |config|
|
7
6
|
assert_equal SimpleShow, config
|
@@ -9,18 +8,21 @@ class SimpleShowTest < SimpleShowTestCase
|
|
9
8
|
end
|
10
9
|
|
11
10
|
test 'default configuration' do
|
12
|
-
assert_equal :simple_show
|
13
|
-
assert_equal :div
|
14
|
-
assert_equal :show
|
15
|
-
assert_equal :label
|
16
|
-
assert_equal nil
|
17
|
-
assert_equal nil
|
18
|
-
assert_equal ':'
|
19
|
-
assert_equal :span
|
20
|
-
assert_equal :value
|
21
|
-
assert_equal nil
|
22
|
-
assert_equal nil
|
23
|
-
assert_equal
|
11
|
+
assert_equal :simple_show, SimpleShow.show_class
|
12
|
+
assert_equal :div, SimpleShow.wrapper_tag
|
13
|
+
assert_equal :show, SimpleShow.wrapper_class
|
14
|
+
assert_equal :label, SimpleShow.label_tag
|
15
|
+
assert_equal nil, SimpleShow.label_class
|
16
|
+
assert_equal nil, SimpleShow.label_prefix
|
17
|
+
assert_equal ':', SimpleShow.label_suffix
|
18
|
+
assert_equal :span, SimpleShow.value_tag
|
19
|
+
assert_equal :value, SimpleShow.value_class
|
20
|
+
assert_equal nil, SimpleShow.value_prefix
|
21
|
+
assert_equal nil, SimpleShow.value_suffix
|
22
|
+
assert_equal :ul, SimpleShow.collection_tag
|
23
|
+
assert_equal :show_collection, SimpleShow.collection_class
|
24
|
+
assert_equal :li, SimpleShow.collection_item_tag
|
25
|
+
assert_equal :show_collection_item, SimpleShow.collection_item_class
|
26
|
+
assert_equal true, SimpleShow.clear_on_close
|
24
27
|
end
|
25
|
-
|
26
28
|
end
|
@@ -1,40 +1,38 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class SimpleShowForTest < SimpleShowTestCase
|
4
|
-
|
5
4
|
test 'simple_show_for raises error without a block' do
|
6
5
|
assert_raise ArgumentError do
|
7
|
-
simple_show_for
|
6
|
+
simple_show_for
|
8
7
|
end
|
9
8
|
end
|
10
9
|
|
11
10
|
test 'html wrapper includes the correct id and class' do
|
12
|
-
doc = Nokogiri::HTML.fragment(simple_show_for(@philip) do |
|
11
|
+
doc = Nokogiri::HTML.fragment(simple_show_for(@philip) do |_s|
|
13
12
|
''
|
14
13
|
end)
|
15
|
-
assert_equal %w
|
14
|
+
assert_equal %w(simple_show golfer), doc.at('div').attr('class').split(/\s+/)
|
16
15
|
assert_equal "golfer_#{@philip.id}", doc.at('div').attr('id')
|
17
16
|
end
|
18
17
|
|
19
18
|
test 'last tag is a BR that clears all when enabled' do
|
20
|
-
clear_on_close = SimpleShow
|
21
|
-
SimpleShow
|
22
|
-
doc = Nokogiri::HTML.fragment(simple_show_for(@philip) do |
|
19
|
+
clear_on_close = SimpleShow.clear_on_close
|
20
|
+
SimpleShow.clear_on_close = true
|
21
|
+
doc = Nokogiri::HTML.fragment(simple_show_for(@philip) do |_s|
|
23
22
|
''
|
24
23
|
end)
|
25
|
-
SimpleShow
|
24
|
+
SimpleShow.clear_on_close = clear_on_close
|
26
25
|
assert_equal 'div', doc.at('div').elements.last.name
|
27
26
|
assert_equal 'clear', doc.at('div').elements.last.attr('class')
|
28
27
|
end
|
29
28
|
|
30
29
|
test 'last tag is not a div/clear when disabled' do
|
31
|
-
clear_on_close = SimpleShow
|
32
|
-
SimpleShow
|
33
|
-
doc = Nokogiri::HTML.fragment(simple_show_for(@philip) do |
|
30
|
+
clear_on_close = SimpleShow.clear_on_close
|
31
|
+
SimpleShow.clear_on_close = false
|
32
|
+
doc = Nokogiri::HTML.fragment(simple_show_for(@philip) do |_s|
|
34
33
|
''
|
35
34
|
end)
|
36
35
|
assert_not_equal 'clear', doc.at('div').elements.last.try(:attr, 'class')
|
37
|
-
SimpleShow
|
36
|
+
SimpleShow.clear_on_close = clear_on_close
|
38
37
|
end
|
39
|
-
|
40
38
|
end
|
@@ -1,18 +1,16 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class SimpleShowLabelTest < SimpleShowTestCase
|
4
|
-
|
5
4
|
test 'labels and label overrides' do
|
6
5
|
doc = Nokogiri::HTML.fragment(
|
7
|
-
simple_show_for(@philip) do |s|
|
6
|
+
simple_show_for(@philip) do |s|
|
8
7
|
o = ActiveSupport::SafeBuffer.new
|
9
8
|
o += s.show :name
|
10
|
-
o += s.show :phone, :
|
9
|
+
o += s.show :phone, label: 'Cell Phone'
|
11
10
|
o
|
12
11
|
end
|
13
12
|
)
|
14
13
|
assert_equal 'Name:', doc.css('label').first.text
|
15
14
|
assert_equal 'Cell Phone:', doc.css('label').last.text
|
16
15
|
end
|
17
|
-
|
18
16
|
end
|
@@ -1,83 +1,7 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
require 'skip_helper'
|
2
3
|
|
3
4
|
class SimpleShowShowTest < SimpleShowTestCase
|
4
|
-
|
5
|
-
test ':if => true/false' do
|
6
|
-
doc = Nokogiri::HTML.fragment(
|
7
|
-
simple_show_for(@philip) do |s|
|
8
|
-
o = ActiveSupport::SafeBuffer.new
|
9
|
-
o += s.show :name, :if => true
|
10
|
-
o += s.show :phone, :if => false
|
11
|
-
o
|
12
|
-
end
|
13
|
-
)
|
14
|
-
assert_equal ['Name:'], doc.css('label').map(&:text)
|
15
|
-
end
|
16
|
-
|
17
|
-
test ':if => :record_method' do
|
18
|
-
def @philip.method_returning_true; true; end
|
19
|
-
def @philip.method_returning_false; false; end
|
20
|
-
doc = Nokogiri::HTML.fragment(
|
21
|
-
simple_show_for(@philip) do |s|
|
22
|
-
o = ActiveSupport::SafeBuffer.new
|
23
|
-
o += s.show :name, :if => :method_returning_true
|
24
|
-
o += s.show :phone, :if => :method_returning_false
|
25
|
-
o
|
26
|
-
end
|
27
|
-
)
|
28
|
-
assert_equal ['Name:'], doc.css('label').map(&:text)
|
29
|
-
end
|
30
|
-
|
31
|
-
test ':if => {lambda}' do
|
32
|
-
doc = Nokogiri::HTML.fragment(
|
33
|
-
simple_show_for(@philip) do |s|
|
34
|
-
o = ActiveSupport::SafeBuffer.new
|
35
|
-
o += s.show :name, :if => lambda { true }
|
36
|
-
o += s.show :phone, :if => lambda { false }
|
37
|
-
o
|
38
|
-
end
|
39
|
-
)
|
40
|
-
assert_equal ['Name:'], doc.css('label').map(&:text)
|
41
|
-
end
|
42
|
-
|
43
|
-
test ':unless => true/false' do
|
44
|
-
doc = Nokogiri::HTML.fragment(
|
45
|
-
simple_show_for(@philip) do |s|
|
46
|
-
o = ActiveSupport::SafeBuffer.new
|
47
|
-
o += s.show :name, :unless => true
|
48
|
-
o += s.show :phone, :unless => false
|
49
|
-
o
|
50
|
-
end
|
51
|
-
)
|
52
|
-
assert_equal ['Phone:'], doc.css('label').map(&:text)
|
53
|
-
end
|
54
|
-
|
55
|
-
test ':unless => :record_method' do
|
56
|
-
def @philip.method_returning_true; true; end
|
57
|
-
def @philip.method_returning_false; false; end
|
58
|
-
doc = Nokogiri::HTML.fragment(
|
59
|
-
simple_show_for(@philip) do |s|
|
60
|
-
o = ActiveSupport::SafeBuffer.new
|
61
|
-
o += s.show :name, :unless => :method_returning_true
|
62
|
-
o += s.show :phone, :unless => :method_returning_false
|
63
|
-
o
|
64
|
-
end
|
65
|
-
)
|
66
|
-
assert_equal ['Phone:'], doc.css('label').map(&:text)
|
67
|
-
end
|
68
|
-
|
69
|
-
test ':unless => {lambda}' do
|
70
|
-
doc = Nokogiri::HTML.fragment(
|
71
|
-
simple_show_for(@philip) do |s|
|
72
|
-
o = ActiveSupport::SafeBuffer.new
|
73
|
-
o += s.show :name, :unless => lambda { true }
|
74
|
-
o += s.show :phone, :unless => lambda { false }
|
75
|
-
o
|
76
|
-
end
|
77
|
-
)
|
78
|
-
assert_equal ['Phone:'], doc.css('label').map(&:text)
|
79
|
-
end
|
80
|
-
|
81
|
-
|
82
|
-
|
5
|
+
test_skip(:show, :name, :phone, 'Name:', 'Phone:')
|
83
6
|
end
|
7
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'skip_helper'
|
3
|
+
|
4
|
+
class SimpleShowShowCollectionTest < SimpleShowTestCase
|
5
|
+
test_skip(:show_collection, :rounds, :clubs, 'Rounds:', 'Clubs:')
|
6
|
+
|
7
|
+
test 'default display works' do
|
8
|
+
doc = Nokogiri::HTML.fragment(
|
9
|
+
simple_show_for(@philip) do |s|
|
10
|
+
o = ActiveSupport::SafeBuffer.new
|
11
|
+
o += s.show :name
|
12
|
+
o += s.show_collection :rounds
|
13
|
+
o
|
14
|
+
end
|
15
|
+
)
|
16
|
+
assert_equal 'Rounds:', doc.css('label').last.text
|
17
|
+
assert_equal [@round1, @round2].map(&:to_s).sort, doc.css('.show_collection_item').map(&:text).sort
|
18
|
+
end
|
19
|
+
|
20
|
+
test 'block display works' do
|
21
|
+
doc = Nokogiri::HTML.fragment(
|
22
|
+
simple_show_for(@philip) do |s|
|
23
|
+
o = ActiveSupport::SafeBuffer.new
|
24
|
+
o += s.show :name
|
25
|
+
o += s.show_collection :rounds do |round|
|
26
|
+
nested_o = ActiveSupport::SafeBuffer.new
|
27
|
+
nested_o += "#{round} - #{round.score}"
|
28
|
+
nested_o
|
29
|
+
end
|
30
|
+
o
|
31
|
+
end
|
32
|
+
)
|
33
|
+
assert_equal 'Rounds:', doc.css('label').last.text
|
34
|
+
assert_equal [@round1, @round2].map{|r| "#{r} - #{r.score}" }.sort, doc.css('.show_collection_item').map(&:text).sort
|
35
|
+
end
|
36
|
+
end
|
@@ -1,31 +1,31 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class SimpleShowValueTest < SimpleShowTestCase
|
4
|
-
|
5
4
|
def setup
|
6
5
|
super
|
7
6
|
SimpleShow.setup do |config|
|
8
7
|
config.helpers[:to_piglatin] = :piglatin
|
9
8
|
end
|
10
9
|
@doc = Nokogiri::HTML.fragment(
|
11
|
-
simple_show_for(@philip) do |s|
|
10
|
+
simple_show_for(@philip) do |s|
|
12
11
|
o = ActiveSupport::SafeBuffer.new
|
13
12
|
o += s.show :name
|
14
|
-
o += s.show(:email) {|o| o.email.upcase }
|
15
|
-
o += s.show :phone, :
|
16
|
-
o += s.show :born_on, :
|
17
|
-
o += s.show :born_on, :
|
18
|
-
o += s.show(:html) {|
|
19
|
-
o += s.show :handicap, :
|
20
|
-
o += s.show :name, :
|
21
|
-
o += s.show :to_currency, :
|
22
|
-
o += s.show :with_delimeter, :
|
23
|
-
o += s.show :with_delimeter, :
|
24
|
-
o += s.show :custom_helper, :
|
13
|
+
o += s.show(:email) { |o| o.email.upcase }
|
14
|
+
o += s.show :phone, value: '867-5309'
|
15
|
+
o += s.show :born_on, format: '%B %d'
|
16
|
+
o += s.show :born_on, format: :mmddyy
|
17
|
+
o += s.show(:html) { |_o| '<b>html</b>' }
|
18
|
+
o += s.show :handicap, format: '%.3f'
|
19
|
+
o += s.show :name, format: '%20s'
|
20
|
+
o += s.show :to_currency, value: '12345.67', to_currency: true
|
21
|
+
o += s.show :with_delimeter, value: '12345.67', with_delimiter: true
|
22
|
+
o += s.show :with_delimeter, value: '12345.67', with_delimiter: { delimiter: ' ' }
|
23
|
+
o += s.show :custom_helper, value: 'piglatin', to_piglatin: true
|
25
24
|
o
|
26
25
|
end
|
27
26
|
)
|
28
27
|
end
|
28
|
+
|
29
29
|
test 'css class for data types' do
|
30
30
|
assert @doc.css('span.value')[0].attr('class').split(/\s+/).include?('string') # :name
|
31
31
|
assert @doc.css('span.value')[6].attr('class').split(/\s+/).include?('decimal') # :handicap
|
@@ -64,5 +64,4 @@ class SimpleShowValueTest < SimpleShowTestCase
|
|
64
64
|
test 'custom helpers' do
|
65
65
|
assert_equal 'iglatinpay', @doc.css('span.value')[11].text
|
66
66
|
end
|
67
|
-
|
68
67
|
end
|
metadata
CHANGED
@@ -1,34 +1,23 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_show
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 1
|
10
|
-
version: 0.1.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- Philip Hallstrom
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2013-09-30 00:00:00 Z
|
11
|
+
date: 2015-05-04 00:00:00.000000000 Z
|
19
12
|
dependencies: []
|
20
|
-
|
21
|
-
|
22
|
-
email:
|
13
|
+
description: 'SimpleShow is to #show what SimpleForm is to #edit'
|
14
|
+
email:
|
23
15
|
- philip@pjkh.com
|
24
16
|
executables: []
|
25
|
-
|
26
17
|
extensions: []
|
27
|
-
|
28
18
|
extra_rdoc_files: []
|
29
|
-
|
30
|
-
|
31
|
-
- .gitignore
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
32
21
|
- Gemfile
|
33
22
|
- README.rdoc
|
34
23
|
- Rakefile
|
@@ -40,49 +29,44 @@ files:
|
|
40
29
|
- lib/simple_show/base.rb
|
41
30
|
- lib/simple_show/version.rb
|
42
31
|
- simple_show.gemspec
|
32
|
+
- test/skip_helper.rb
|
43
33
|
- test/test_helper.rb
|
44
34
|
- test/test_simple_show.rb
|
45
35
|
- test/test_simple_show_for.rb
|
46
36
|
- test/test_simple_show_label.rb
|
47
37
|
- test/test_simple_show_show.rb
|
38
|
+
- test/test_simple_show_show_collection.rb
|
48
39
|
- test/test_simple_show_value.rb
|
49
|
-
homepage:
|
50
|
-
licenses:
|
40
|
+
homepage: ''
|
41
|
+
licenses:
|
51
42
|
- MIT
|
43
|
+
metadata: {}
|
52
44
|
post_install_message:
|
53
45
|
rdoc_options: []
|
54
|
-
|
55
|
-
require_paths:
|
46
|
+
require_paths:
|
56
47
|
- lib
|
57
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
-
|
59
|
-
requirements:
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
60
50
|
- - ">="
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
version: "0"
|
66
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
-
none: false
|
68
|
-
requirements:
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
69
55
|
- - ">="
|
70
|
-
- !ruby/object:Gem::Version
|
71
|
-
|
72
|
-
segments:
|
73
|
-
- 0
|
74
|
-
version: "0"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
75
58
|
requirements: []
|
76
|
-
|
77
59
|
rubyforge_project: simple_show
|
78
|
-
rubygems_version:
|
60
|
+
rubygems_version: 2.4.3
|
79
61
|
signing_key:
|
80
|
-
specification_version:
|
81
|
-
summary:
|
82
|
-
test_files:
|
62
|
+
specification_version: 4
|
63
|
+
summary: 'SimpleShow is to #show what SimpleForm is to #edit'
|
64
|
+
test_files:
|
65
|
+
- test/skip_helper.rb
|
83
66
|
- test/test_helper.rb
|
84
67
|
- test/test_simple_show.rb
|
85
68
|
- test/test_simple_show_for.rb
|
86
69
|
- test/test_simple_show_label.rb
|
87
70
|
- test/test_simple_show_show.rb
|
71
|
+
- test/test_simple_show_show_collection.rb
|
88
72
|
- test/test_simple_show_value.rb
|