view 1.0.0.alpha.3 → 1.0.0.alpha.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/README.rdoc +269 -51
  2. data/lib/view/configuration.rb +55 -0
  3. data/lib/view/formatter.rb +8 -12
  4. data/lib/view/formatters/array.rb +1 -1
  5. data/lib/view/formatters/auto.rb +6 -2
  6. data/lib/view/formatters/definition_list.rb +110 -0
  7. data/lib/view/formatters/file_link.rb +1 -1
  8. data/lib/view/formatters/guess.rb +2 -2
  9. data/lib/view/formatters/human.rb +1 -0
  10. data/lib/view/formatters/image.rb +49 -8
  11. data/lib/view/formatters/link.rb +29 -4
  12. data/lib/view/formatters/percentage.rb +3 -1
  13. data/lib/view/formatters/phone.rb +1 -0
  14. data/lib/view/formatters/precision.rb +3 -1
  15. data/lib/view/formatters/size.rb +7 -0
  16. data/lib/view/formatters/table.rb +208 -0
  17. data/lib/view/helper.rb +27 -0
  18. data/lib/view/railtie.rb +1 -1
  19. data/lib/view/version.rb +1 -1
  20. data/lib/view.rb +28 -56
  21. data/spec/spec_helper.rb +52 -4
  22. data/spec/view/configuration_spec.rb +11 -0
  23. data/spec/view/formatters/currency_spec.rb +2 -6
  24. data/spec/view/formatters/definition_list_spec.rb +59 -0
  25. data/spec/view/formatters/delimited_spec.rb +2 -6
  26. data/spec/view/formatters/html_safe_spec.rb +13 -0
  27. data/spec/view/formatters/human_spec.rb +9 -0
  28. data/spec/view/formatters/image_spec.rb +36 -0
  29. data/spec/view/formatters/link_spec.rb +20 -0
  30. data/spec/view/formatters/percentage_spec.rb +15 -0
  31. data/spec/view/formatters/phone_spec.rb +15 -0
  32. data/spec/view/formatters/precision_spec.rb +15 -0
  33. data/spec/view/formatters/size_spec.rb +15 -0
  34. data/spec/view/formatters/table_spec.rb +131 -0
  35. data/spec/view/helper_spec.rb +26 -4
  36. metadata +29 -4
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Definition List formatter" do
4
+
5
+ it "creates a definition list" do
6
+ obj = Struct.new(:foo).new("bar")
7
+ subject = helper.view obj, :as => :definition_list do |dl|
8
+ dl.view :foo
9
+ end
10
+ subject.should == %|<dl><dt class="foo">Foo</dt><dd class="foo">bar</dd></dl>|
11
+ end
12
+
13
+ it "shows all column names" do
14
+ klass = Struct.new(:foo, :baz) do
15
+ def self.column_names
16
+ [ :foo ]
17
+ end
18
+ end
19
+ obj = klass.new("bar", "bang")
20
+ subject = helper.view obj, :as => :definition_list
21
+ subject.should == %|<dl><dt class="foo">Foo</dt><dd class="foo">bar</dd></dl>|
22
+ end
23
+
24
+ it "has a :fields option" do
25
+ klass = Struct.new(:foo, :baz)
26
+ obj = klass.new("bar", "bang")
27
+ subject = helper.view obj, :as => :definition_list, :fields => [:foo]
28
+ subject.should == %|<dl><dt class="foo">Foo</dt><dd class="foo">bar</dd></dl>|
29
+ end
30
+
31
+ it "formats the attributes themselves" do
32
+ time = Time.now
33
+ obj = Struct.new(:foo).new(time)
34
+ subject = helper.view obj, :as => :definition_list do |dl|
35
+ dl.view :foo, :as => :date
36
+ end
37
+ subject.should == %|<dl><dt class="foo">Foo</dt><dd class="foo">#{I18n.l(time.to_date)}</dd></dl>|
38
+ end
39
+
40
+ it "uses the options as options for the dl-tag" do
41
+ obj = Struct.new(:foo).new("bar")
42
+ subject = helper.view obj, :as => :definition_list, :id => "genesis" do |dl|
43
+ dl.view :foo
44
+ end
45
+ subject.should == %|<dl id="genesis"><dt class="foo">Foo</dt><dd class="foo">bar</dd></dl>|
46
+ end
47
+
48
+ it "uses human_attribute_name on dt-tags when it can" do
49
+ klass = Struct.new(:foo) do
50
+ def self.human_attribute_name(key)
51
+ key.to_s.upcase
52
+ end
53
+ end
54
+ obj = klass.new("bar")
55
+ subject = helper.view obj, :as => :definition_list, :fields => [:foo]
56
+ subject.should == %|<dl><dt class="foo">FOO</dt><dd class="foo">bar</dd></dl>|
57
+ end
58
+
59
+ end
@@ -2,15 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe "Delimited formatter" do
4
4
 
5
- before do
6
- helper.stub(:number_with_delimiter).and_return("called")
7
- end
8
-
9
5
  it "calls number_with_delimiter" do
10
- helper.view(19.99, :as => :delimited).should == "called"
6
+ helper.view(19_999.99, :as => :delimited).should == "19,999.99"
11
7
  end
12
8
 
13
- it "allowes no other options" do
9
+ it "allows no other options" do
14
10
  helper.should_receive(:number_with_delimiter).with(19.99, :delimiter => ";")
15
11
  helper.view(19.99, :as => :delimited, :delimiter => ";", :foo => "bar")
16
12
  end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe "HTML Safe Helper" do
4
+
5
+ it "should make something safe" do
6
+ value = "foo"
7
+ value.should_not be_html_safe
8
+ subject = View.format(value, :as => :html_safe)
9
+ subject.should be_html_safe
10
+ subject.should == value
11
+ end
12
+
13
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Human formatter" do
4
+
5
+ it "uses number to human" do
6
+ helper.view(10_000, :as => :human).should == "10 Thousand"
7
+ end
8
+
9
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Image formatter" do
4
+
5
+ let :image do
6
+ Struct.new(:url) {
7
+ def file?
8
+ true
9
+ end
10
+ }.new("/images/foo.jpg")
11
+ end
12
+
13
+ before do
14
+ helper.stub(:path_to_image).and_return("/images/foo.jpg")
15
+ end
16
+
17
+ it "shows an img" do
18
+ helper.view(image, :as => :image).should == %|<img alt="Foo" src="/images/foo.jpg" />|
19
+ end
20
+
21
+ it "doesn't show an image when it's not here" do
22
+ image.stub(:file?).and_return(false)
23
+ helper.view(image, :as => :image).should == nil
24
+ end
25
+
26
+ it "shows an thumbnail" do
27
+ image.should_receive(:url).with(:thumbnail)
28
+ helper.view(image, :as => :image, :style => :thumbnail)
29
+ end
30
+
31
+ it "passes html options to image tag" do
32
+ subject =helper.view(image, :as => :image, :html => { :size => "20x20" })
33
+ subject.should == %|<img alt="Foo" height="20" src="/images/foo.jpg" width="20" />|
34
+ end
35
+
36
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Link formatter" do
4
+
5
+ it "links" do
6
+ helper.view("foo", :as => :link, :to => "bar.com").should == %|<a href="bar.com">foo</a>|
7
+ end
8
+
9
+ it "links objects" do
10
+ post = Struct.new(:to_label).new("post")
11
+ helper.should_receive(:polymorphic_path).with([:edit, post]).and_return("/posts/2/edit")
12
+ helper.view(post, :as => :link, :path => :edit).should == %|<a href="/posts/2/edit">post</a>|
13
+ end
14
+
15
+ it "has option :text" do
16
+ subject = helper.view("foo", :as => :link, :to => "bar.com", :text => "bang")
17
+ subject.should == %|<a href="bar.com">bang</a>|
18
+ end
19
+
20
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe "Percentage formatter" do
5
+
6
+ it "calls number_to_percentage" do
7
+ helper.view(19.99, :as => :percentage).should == "19.990%"
8
+ end
9
+
10
+ it "allows no other options" do
11
+ helper.should_receive(:number_to_percentage).with(19.99, {})
12
+ helper.view(19.99, :as => :percentage, :foo => "bar")
13
+ end
14
+
15
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe "Phone formatter" do
5
+
6
+ it "calls number_to_phone" do
7
+ helper.view(5551234, :as => :phone).should == "555-1234"
8
+ end
9
+
10
+ it "allows no other options" do
11
+ helper.should_receive(:number_to_phone).with(5551234, {})
12
+ helper.view(5551234, :as => :phone, :foo => "bar")
13
+ end
14
+
15
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe "Precision formatter" do
5
+
6
+ it "calls number_with_precision" do
7
+ helper.view(19.99, :as => :precision).should == "19.990"
8
+ end
9
+
10
+ it "allows no other options" do
11
+ helper.should_receive(:number_with_precision).with(19.99, {})
12
+ helper.view(19.99, :as => :precision, :foo => "bar")
13
+ end
14
+
15
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe "Size formatter" do
5
+
6
+ it "calls number_to_human_size" do
7
+ helper.view(1999, :as => :size).should == "1.95 KB"
8
+ end
9
+
10
+ it "allows no other options" do
11
+ helper.should_receive(:number_to_currency).with(19.99, {})
12
+ helper.view(19.99, :as => :currency, :foo => "bar")
13
+ end
14
+
15
+ end
@@ -0,0 +1,131 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Table formatter" do
4
+
5
+ it "renders a table" do
6
+ time = Time.now
7
+ obj = Struct.new(:foo, :bar).new("baz", time)
8
+ collection = [ obj ] * 2
9
+ html = helper.view collection, :as => :table do |tb|
10
+ tb.view :foo
11
+ tb.view :bar, :as => :date
12
+ end
13
+ bar = I18n.l(time.to_date)
14
+ html.squish.should == <<-HTML.squish
15
+ <table>
16
+ <thead>
17
+ <tr>
18
+ <th class="foo">Foo</th>
19
+ <th class="bar">Bar</th>
20
+ </tr>
21
+ </thead>
22
+ <tbody>
23
+ <tr class="odd">
24
+ <td class="foo">baz</td>
25
+ <td class="bar">#{bar}</td>
26
+ </tr>
27
+ <tr class="even">
28
+ <td class="foo">baz</td>
29
+ <td class="bar">#{bar}</td>
30
+ </tr>
31
+ </tbody>
32
+ </table>
33
+ HTML
34
+ end
35
+
36
+ it "renders links when the link option is used" do
37
+ helper.instance_eval do
38
+ def polymorphic_path(opts)
39
+ "link"
40
+ end
41
+ end
42
+ collection = [ Struct.new(:foo).new("Fooz") ]
43
+ html = helper.view collection, :as => :table do |tb|
44
+ tb.view :foo, :as => :link
45
+ end
46
+ html.squish.should == <<-HTML.squish
47
+ <table>
48
+ <thead>
49
+ <tr>
50
+ <th class="foo">Foo</th>
51
+ </tr>
52
+ </thead>
53
+ <tbody>
54
+ <tr class="odd">
55
+ <td class="foo"><a href="link">Fooz</a></td>
56
+ </tr>
57
+ </tbody>
58
+ </table>
59
+ HTML
60
+ end
61
+
62
+ it "renders links when the path option is used" do
63
+ helper.instance_eval do
64
+ def polymorphic_path(opts)
65
+ "link"
66
+ end
67
+ end
68
+ collection = [ Struct.new(:foo).new("Fooz") ]
69
+ html = helper.view collection, :as => :table do |tb|
70
+ tb.view :foo, :path => :edit
71
+ end
72
+ html.squish.should == <<-HTML.squish
73
+ <table>
74
+ <thead>
75
+ <tr>
76
+ <th class="foo">Foo</th>
77
+ </tr>
78
+ </thead>
79
+ <tbody>
80
+ <tr class="odd">
81
+ <td class="foo"><a href="link">Fooz</a></td>
82
+ </tr>
83
+ </tbody>
84
+ </table>
85
+ HTML
86
+ end
87
+
88
+ it "renders fields" do
89
+ collection = [ Struct.new(:foo).new("Fooz") ]
90
+ html = helper.view collection, :as => :table, :fields => [ :foo ]
91
+ html.squish.should == <<-HTML.squish
92
+ <table>
93
+ <thead>
94
+ <tr>
95
+ <th class="foo">Foo</th>
96
+ </tr>
97
+ </thead>
98
+ <tbody>
99
+ <tr class="odd">
100
+ <td class="foo">Fooz</td>
101
+ </tr>
102
+ </tbody>
103
+ </table>
104
+ HTML
105
+ end
106
+
107
+ it "renders a link on a field" do
108
+ helper.instance_eval do
109
+ def polymorphic_path(opts)
110
+ "link"
111
+ end
112
+ end
113
+ collection = [ Struct.new(:foo).new("Fooz") ]
114
+ html = helper.view collection, :as => :table, :fields => [ :foo ], :link => "foo"
115
+ html.squish.should == <<-HTML.squish
116
+ <table>
117
+ <thead>
118
+ <tr>
119
+ <th class="foo">Foo</th>
120
+ </tr>
121
+ </thead>
122
+ <tbody>
123
+ <tr class="odd">
124
+ <td class="foo"><a href="link">Fooz</a></td>
125
+ </tr>
126
+ </tbody>
127
+ </table>
128
+ HTML
129
+ end
130
+
131
+ end
@@ -2,10 +2,32 @@ require 'spec_helper'
2
2
 
3
3
  describe "View::Helper" do
4
4
 
5
- pending "views" do
6
- template = Class.new do
7
- include View::Helper
8
- end
5
+ it "views" do
6
+ View.should_receive(:format).with('x', {:foo => 'bar'}, helper)
7
+ helper.view('x', :foo => 'bar')
8
+ end
9
+
10
+ it "renders a table" do
11
+ View.should_receive(:format).with('x', {:as => :table}, helper)
12
+ helper.table_for('x')
13
+ end
14
+
15
+ it "renders a definition list" do
16
+ View.should_receive(:format).with('x', {:as => :definition_list}, helper)
17
+ helper.definition_list_for('x')
18
+ end
19
+
20
+ it "renders a definition list for a resource" do
21
+ helper.should_receive(:resource).and_return('x')
22
+ View.should_receive(:format).with('x', {:as => :definition_list}, helper)
23
+ helper.definition_list
24
+ end
25
+
26
+ it "renders a definition list for a resource" do
27
+ helper.should_receive(:collection).and_return(['x'])
28
+ helper.should_receive(:resource_class).and_return(String)
29
+ View.should_receive(:format).with(['x'], {:as => :table, :class => String}, helper)
30
+ helper.table
9
31
  end
10
32
 
11
33
  end
metadata CHANGED
@@ -7,8 +7,8 @@ version: !ruby/object:Gem::Version
7
7
  - 0
8
8
  - 0
9
9
  - alpha
10
- - 3
11
- version: 1.0.0.alpha.3
10
+ - 4
11
+ version: 1.0.0.alpha.4
12
12
  platform: ruby
13
13
  authors:
14
14
  - Iain Hecker
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-10-16 00:00:00 +02:00
19
+ date: 2010-10-18 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -34,7 +34,7 @@ dependencies:
34
34
  version: 3.0.0
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
- description: A very extensible way of viewing objects, easily integrated with other gems
37
+ description: A very extensible way of viewing objects
38
38
  email:
39
39
  - iain@iain.nl
40
40
  executables: []
@@ -44,6 +44,7 @@ extensions: []
44
44
  extra_rdoc_files:
45
45
  - README.rdoc
46
46
  files:
47
+ - lib/view/configuration.rb
47
48
  - lib/view/formatter.rb
48
49
  - lib/view/formatters/array.rb
49
50
  - lib/view/formatters/auto.rb
@@ -52,6 +53,7 @@ files:
52
53
  - lib/view/formatters/currency.rb
53
54
  - lib/view/formatters/date.rb
54
55
  - lib/view/formatters/datetime.rb
56
+ - lib/view/formatters/definition_list.rb
55
57
  - lib/view/formatters/delimited.rb
56
58
  - lib/view/formatters/file_link.rb
57
59
  - lib/view/formatters/guess.rb
@@ -65,6 +67,7 @@ files:
65
67
  - lib/view/formatters/self.rb
66
68
  - lib/view/formatters/sentence.rb
67
69
  - lib/view/formatters/size.rb
70
+ - lib/view/formatters/table.rb
68
71
  - lib/view/helper.rb
69
72
  - lib/view/railtie.rb
70
73
  - lib/view/version.rb
@@ -72,6 +75,7 @@ files:
72
75
  - init.rb
73
76
  - README.rdoc
74
77
  - spec/spec_helper.rb
78
+ - spec/view/configuration_spec.rb
75
79
  - spec/view/formatter_spec.rb
76
80
  - spec/view/formatters/auto_spec.rb
77
81
  - spec/view/formatters/blank_spec.rb
@@ -79,11 +83,21 @@ files:
79
83
  - spec/view/formatters/currency_spec.rb
80
84
  - spec/view/formatters/date_spec.rb
81
85
  - spec/view/formatters/datetime_spec.rb
86
+ - spec/view/formatters/definition_list_spec.rb
82
87
  - spec/view/formatters/delimited_spec.rb
83
88
  - spec/view/formatters/file_link_spec.rb
84
89
  - spec/view/formatters/guess_spec.rb
90
+ - spec/view/formatters/html_safe_spec.rb
91
+ - spec/view/formatters/human_spec.rb
92
+ - spec/view/formatters/image_spec.rb
93
+ - spec/view/formatters/link_spec.rb
94
+ - spec/view/formatters/percentage_spec.rb
95
+ - spec/view/formatters/phone_spec.rb
96
+ - spec/view/formatters/precision_spec.rb
85
97
  - spec/view/formatters/self_spec.rb
86
98
  - spec/view/formatters/sentence_spec.rb
99
+ - spec/view/formatters/size_spec.rb
100
+ - spec/view/formatters/table_spec.rb
87
101
  - spec/view/helper_spec.rb
88
102
  has_rdoc: true
89
103
  homepage: http://github.com/iain/view
@@ -121,6 +135,7 @@ specification_version: 3
121
135
  summary: Displaying objects automatically
122
136
  test_files:
123
137
  - spec/spec_helper.rb
138
+ - spec/view/configuration_spec.rb
124
139
  - spec/view/formatter_spec.rb
125
140
  - spec/view/formatters/auto_spec.rb
126
141
  - spec/view/formatters/blank_spec.rb
@@ -128,9 +143,19 @@ test_files:
128
143
  - spec/view/formatters/currency_spec.rb
129
144
  - spec/view/formatters/date_spec.rb
130
145
  - spec/view/formatters/datetime_spec.rb
146
+ - spec/view/formatters/definition_list_spec.rb
131
147
  - spec/view/formatters/delimited_spec.rb
132
148
  - spec/view/formatters/file_link_spec.rb
133
149
  - spec/view/formatters/guess_spec.rb
150
+ - spec/view/formatters/html_safe_spec.rb
151
+ - spec/view/formatters/human_spec.rb
152
+ - spec/view/formatters/image_spec.rb
153
+ - spec/view/formatters/link_spec.rb
154
+ - spec/view/formatters/percentage_spec.rb
155
+ - spec/view/formatters/phone_spec.rb
156
+ - spec/view/formatters/precision_spec.rb
134
157
  - spec/view/formatters/self_spec.rb
135
158
  - spec/view/formatters/sentence_spec.rb
159
+ - spec/view/formatters/size_spec.rb
160
+ - spec/view/formatters/table_spec.rb
136
161
  - spec/view/helper_spec.rb