simple_show 0.0.4 → 0.0.5
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.
- data/README.rdoc +6 -5
- data/lib/simple_show/base.rb +23 -0
- data/lib/simple_show/version.rb +1 -1
- data/test/test_helper.rb +1 -0
- data/test/test_simple_show_show.rb +83 -0
- data/test/test_simple_show_value.rb +7 -0
- metadata +7 -7
data/README.rdoc
CHANGED
@@ -36,10 +36,13 @@ In our show action we can then do this:
|
|
36
36
|
<%= s.show :born_on, :value => '**censored**' %>
|
37
37
|
<%= s.show :updated_at, :format => '%B %d' %>
|
38
38
|
<%= s.show :created_at, :format => :short # Assumes you've defined a DATE_FORMAT for :short %>
|
39
|
-
<%= s.show :cash_in_wallet, :
|
39
|
+
<%= s.show :cash_in_wallet, :to_currency => true %>
|
40
|
+
<%= s.show :height, :if => :tall %>
|
41
|
+
<%= s.show :weight, :unless => :sensitive %>
|
40
42
|
<% end %>
|
41
43
|
|
42
|
-
Which would generate (using the defaults) output like this
|
44
|
+
Which would generate (using the defaults) output like this, assuming that
|
45
|
+
@johndor.tall returns false and @johndoe.sensitive returns true:
|
43
46
|
|
44
47
|
<div class="simple_show person" id="person_123">
|
45
48
|
<div class="show">
|
@@ -82,9 +85,7 @@ if you don't want that BR tag then set SimpleShow.clear_on_close to false.
|
|
82
85
|
== TODO:
|
83
86
|
|
84
87
|
- Add support for associations.
|
85
|
-
- Add options for
|
86
|
-
- Add option :if/:unless => :method_sym so we can say things like "s.show...
|
87
|
-
:unless => :blank?" to hide the field entirely.
|
88
|
+
- Add options for booleans.
|
88
89
|
|
89
90
|
== License:
|
90
91
|
|
data/lib/simple_show/base.rb
CHANGED
@@ -7,8 +7,28 @@ module SimpleShow
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def show(attr, options = {}, &block)
|
10
|
+
if options.key? :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
|
28
|
+
|
10
29
|
output = label(attr, options, &block)
|
11
30
|
output += value(attr, options, &block)
|
31
|
+
|
12
32
|
if SimpleShow.wrapper_tag.nil?
|
13
33
|
output
|
14
34
|
else
|
@@ -34,6 +54,9 @@ module SimpleShow
|
|
34
54
|
else
|
35
55
|
value = options[:format] % value
|
36
56
|
end
|
57
|
+
elsif (helper = %w[to_currency to_human to_human_size to_percentage to_phone with_delimiter with_precision].detect{|e| options[e.to_sym].present?})
|
58
|
+
options[helper.to_sym] = {} unless options[helper.to_sym].is_a? Hash
|
59
|
+
value = @binding.send("number_#{helper}", value, options[helper.to_sym])
|
37
60
|
end
|
38
61
|
|
39
62
|
@binding.content_tag(SimpleShow.value_tag, :class => SimpleShow.value_class) do
|
data/lib/simple_show/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -48,6 +48,7 @@ class SimpleShowTestCase < ActiveSupport::TestCase
|
|
48
48
|
include ActionController::RecordIdentifier
|
49
49
|
include ActionView::Helpers::CaptureHelper
|
50
50
|
include ActionView::Helpers::TagHelper
|
51
|
+
include ActionView::Helpers::NumberHelper
|
51
52
|
attr_accessor :output_buffer
|
52
53
|
|
53
54
|
def setup
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
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
|
+
|
83
|
+
end
|
@@ -15,6 +15,8 @@ class SimpleShowValueTest < SimpleShowTestCase
|
|
15
15
|
o += s.show(:html) {|o| '<b>html</b>' }
|
16
16
|
o += s.show :handicap, :format => '%.3f'
|
17
17
|
o += s.show :name, :format => '%20s'
|
18
|
+
o += s.show :to_currency, :value => '12345.67', :to_currency => true
|
19
|
+
o += s.show :with_delimeter, :value => '12345.67', :with_delimiter => true
|
18
20
|
o
|
19
21
|
end
|
20
22
|
)
|
@@ -43,4 +45,9 @@ class SimpleShowValueTest < SimpleShowTestCase
|
|
43
45
|
assert_equal ' Philip Hallstrom', @doc.css('span.value')[7].text
|
44
46
|
end
|
45
47
|
|
48
|
+
test 'number helpers' do
|
49
|
+
assert_equal '$12,345.67', @doc.css('span.value')[8].text
|
50
|
+
assert_equal '12,345.67', @doc.css('span.value')[9].text
|
51
|
+
end
|
52
|
+
|
46
53
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_show
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Philip Hallstrom
|
@@ -15,8 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-08-
|
19
|
-
default_executable:
|
18
|
+
date: 2011-08-11 00:00:00 Z
|
20
19
|
dependencies: []
|
21
20
|
|
22
21
|
description: "SimpleShow is to #show what SimpleForm is to #edit"
|
@@ -47,8 +46,8 @@ files:
|
|
47
46
|
- test/test_simple_show.rb
|
48
47
|
- test/test_simple_show_for.rb
|
49
48
|
- test/test_simple_show_label.rb
|
49
|
+
- test/test_simple_show_show.rb
|
50
50
|
- test/test_simple_show_value.rb
|
51
|
-
has_rdoc: true
|
52
51
|
homepage: ""
|
53
52
|
licenses: []
|
54
53
|
|
@@ -78,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
77
|
requirements: []
|
79
78
|
|
80
79
|
rubyforge_project: simple_show
|
81
|
-
rubygems_version: 1.
|
80
|
+
rubygems_version: 1.8.7
|
82
81
|
signing_key:
|
83
82
|
specification_version: 3
|
84
83
|
summary: "SimpleShow is to #show what SimpleForm is to #edit"
|
@@ -87,4 +86,5 @@ test_files:
|
|
87
86
|
- test/test_simple_show.rb
|
88
87
|
- test/test_simple_show_for.rb
|
89
88
|
- test/test_simple_show_label.rb
|
89
|
+
- test/test_simple_show_show.rb
|
90
90
|
- test/test_simple_show_value.rb
|