view 1.0.0.alpha.2 → 1.0.0.alpha.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +3 -3
- data/lib/view/formatter.rb +27 -27
- data/lib/view/formatters/array.rb +8 -5
- data/lib/view/formatters/auto.rb +7 -7
- data/lib/view/formatters/currency.rb +3 -1
- data/lib/view/formatters/delimited.rb +3 -1
- data/lib/view/formatters/file_link.rb +21 -2
- data/lib/view/railtie.rb +3 -3
- data/lib/view/version.rb +1 -1
- data/lib/view.rb +7 -8
- data/spec/spec_helper.rb +20 -2
- data/spec/view/formatter_spec.rb +2 -2
- data/spec/view/formatters/currency_spec.rb +19 -0
- data/spec/view/formatters/delimited_spec.rb +18 -0
- data/spec/view/formatters/file_link_spec.rb +41 -0
- metadata +9 -3
data/README.rdoc
CHANGED
@@ -33,10 +33,10 @@ View will automatically try to choose the proper formatter. You can specify a fo
|
|
33
33
|
|
34
34
|
<%= view @post.created_at, :as => :date %>
|
35
35
|
|
36
|
-
You can add a block to specify your own behavior. The
|
36
|
+
You can add a block to specify your own behavior. The formatter is passed in as a block-variable.
|
37
37
|
|
38
|
-
<%= view @post.author do |
|
39
|
-
<%= link_to
|
38
|
+
<%= view @post.author do |formatter| %>
|
39
|
+
<%= link_to formatter.to_s, formatter.value %>
|
40
40
|
<% end %>
|
41
41
|
|
42
42
|
Most formatters pass their options down to the method they use, such as the image formatter:
|
data/lib/view/formatter.rb
CHANGED
@@ -68,10 +68,7 @@ module View
|
|
68
68
|
# @abstract Subclass and override {#format} to implement your own formatter.
|
69
69
|
# @return [String] the formatted value
|
70
70
|
def format
|
71
|
-
msg
|
72
|
-
The only thing a formatter needs to do is implement the #format method.
|
73
|
-
If you see this error, you forgot to do that for the #{self.class.type} formatter.
|
74
|
-
MSG
|
71
|
+
msg = "The '#{self.class.type}' formatter needs to implement the #format method."
|
75
72
|
raise NotImplementedError.new(msg)
|
76
73
|
end
|
77
74
|
|
@@ -114,13 +111,18 @@ module View
|
|
114
111
|
end
|
115
112
|
end
|
116
113
|
|
117
|
-
#
|
118
|
-
# the formatted value. This doesn't (and shouldn't) do any formatting
|
119
|
-
# itself.
|
114
|
+
# The final result of the formatter, with the block captured if given.
|
120
115
|
#
|
121
|
-
#
|
122
|
-
|
123
|
-
|
116
|
+
# If a template is given, use it to capture the block, for maximum
|
117
|
+
# integration with ActionView.
|
118
|
+
#
|
119
|
+
# @return [String]
|
120
|
+
def format!
|
121
|
+
if block
|
122
|
+
captured_value
|
123
|
+
else
|
124
|
+
to_s
|
125
|
+
end
|
124
126
|
end
|
125
127
|
|
126
128
|
# @return All options, unfiltered.
|
@@ -129,6 +131,12 @@ module View
|
|
129
131
|
@options
|
130
132
|
end
|
131
133
|
|
134
|
+
# A hook for formatters to override so they can inject code between above
|
135
|
+
# the formatting, without all their inherited classes knowing about it.
|
136
|
+
def to_s
|
137
|
+
format
|
138
|
+
end
|
139
|
+
|
132
140
|
private
|
133
141
|
|
134
142
|
def self.skip_blank_formatter?
|
@@ -136,15 +144,7 @@ module View
|
|
136
144
|
end
|
137
145
|
|
138
146
|
def self.format(*args, &block)
|
139
|
-
new(*args, &block).send(:
|
140
|
-
end
|
141
|
-
|
142
|
-
def format!
|
143
|
-
if block
|
144
|
-
captured_value
|
145
|
-
else
|
146
|
-
formatted_value
|
147
|
-
end
|
147
|
+
new(*args, &block).send(:formatted_value)
|
148
148
|
end
|
149
149
|
|
150
150
|
def self.formatters
|
@@ -158,6 +158,11 @@ module View
|
|
158
158
|
@block = block
|
159
159
|
end
|
160
160
|
|
161
|
+
def formatted_value
|
162
|
+
formatter_not_found unless formatter
|
163
|
+
formatter.new(value, all_options, template, &block).format!
|
164
|
+
end
|
165
|
+
|
161
166
|
def template_can_capture?
|
162
167
|
template && template.respond_to?(:capture)
|
163
168
|
end
|
@@ -171,16 +176,11 @@ module View
|
|
171
176
|
end
|
172
177
|
|
173
178
|
def captured_value_by_template
|
174
|
-
template.capture(*block_arguments, &block)
|
179
|
+
template.capture(self, *block_arguments, &block)
|
175
180
|
end
|
176
181
|
|
177
182
|
def captured_return_value
|
178
|
-
block.call(*block_arguments)
|
179
|
-
end
|
180
|
-
|
181
|
-
def formatted_value
|
182
|
-
formatter_not_found unless formatter
|
183
|
-
formatter.new(value, all_options, template, &block).to_s
|
183
|
+
block.call(self, *block_arguments)
|
184
184
|
end
|
185
185
|
|
186
186
|
def formatter
|
@@ -205,7 +205,7 @@ module View
|
|
205
205
|
end
|
206
206
|
|
207
207
|
def block_arguments
|
208
|
-
all_options[:block_arguments] || [
|
208
|
+
all_options[:block_arguments] || []
|
209
209
|
end
|
210
210
|
|
211
211
|
def as
|
@@ -3,11 +3,14 @@ module View
|
|
3
3
|
# @abstract Subclass this for html safe lists with formatted each support.
|
4
4
|
class Array < Formatter
|
5
5
|
|
6
|
+
# The formatted value will be html safe if all the elements in the array
|
7
|
+
# are safe too.
|
6
8
|
def to_s
|
7
|
-
|
8
|
-
|
9
|
+
result = super
|
10
|
+
if result.respond_to?(:html_safe) && all_safe?
|
11
|
+
result.html_safe
|
9
12
|
else
|
10
|
-
|
13
|
+
result
|
11
14
|
end
|
12
15
|
end
|
13
16
|
|
@@ -19,11 +22,11 @@ module View
|
|
19
22
|
|
20
23
|
def formatted_values
|
21
24
|
@formatted_values ||= value.map do |element|
|
22
|
-
View.format(element,
|
25
|
+
View.format(element, each_options, template, &block)
|
23
26
|
end
|
24
27
|
end
|
25
28
|
|
26
|
-
def
|
29
|
+
def each_options
|
27
30
|
all_options[:each] || { :as => View.default_formatter }
|
28
31
|
end
|
29
32
|
|
data/lib/view/formatters/auto.rb
CHANGED
@@ -28,12 +28,12 @@ module View
|
|
28
28
|
auto_formatters.unshift(:formatter => formatter_name, :block => block)
|
29
29
|
end
|
30
30
|
|
31
|
-
private
|
32
|
-
|
33
31
|
def format
|
34
|
-
|
32
|
+
formatted_value
|
35
33
|
end
|
36
34
|
|
35
|
+
private
|
36
|
+
|
37
37
|
def self.auto_formatters
|
38
38
|
@auto_formatters ||= []
|
39
39
|
end
|
@@ -43,6 +43,10 @@ module View
|
|
43
43
|
as ? as[:formatter] : :guess
|
44
44
|
end
|
45
45
|
|
46
|
+
add View.default_list_formatter do
|
47
|
+
value.respond_to?(:each)
|
48
|
+
end
|
49
|
+
|
46
50
|
add :datetime do
|
47
51
|
value.respond_to?(:strftime)
|
48
52
|
end
|
@@ -51,10 +55,6 @@ module View
|
|
51
55
|
View.file_methods.any? { |method| value.respond_to?(method) }
|
52
56
|
end
|
53
57
|
|
54
|
-
add View.default_list_formatter do
|
55
|
-
value.respond_to?(:each)
|
56
|
-
end
|
57
|
-
|
58
58
|
add :link do
|
59
59
|
all_options.has_key?(:to)
|
60
60
|
end
|
@@ -1,8 +1,10 @@
|
|
1
1
|
module View
|
2
2
|
|
3
|
+
# Delegate rendering to the number_to_currency helper from Rails.
|
4
|
+
# @see http://rubydoc.info/docs/rails/3.0.0/ActionView/Helpers/NumberHelper:number_to_currency
|
3
5
|
class Currency < Formatter
|
4
6
|
|
5
|
-
self.allowed_options = [ :precision, :unit, :separator, :delimiter, :format ]
|
7
|
+
self.allowed_options = [ :precision, :unit, :separator, :delimiter, :format, :locale ]
|
6
8
|
|
7
9
|
def format
|
8
10
|
template.number_to_currency(value, options)
|
@@ -1,8 +1,10 @@
|
|
1
1
|
module View
|
2
2
|
|
3
|
+
# Delegate rendering to the number_with_delimiter helper from Rails.
|
4
|
+
# @see http://rubydoc.info/docs/rails/3.0.0/ActionView/Helpers/NumberHelper:number_with_delimiter
|
3
5
|
class Delimited < Formatter
|
4
6
|
|
5
|
-
self.allowed_options = [ :separator, :delimiter ]
|
7
|
+
self.allowed_options = [ :separator, :delimiter, :locale ]
|
6
8
|
|
7
9
|
def format
|
8
10
|
template.number_with_delimiter(value, options)
|
@@ -1,5 +1,20 @@
|
|
1
1
|
module View
|
2
2
|
|
3
|
+
# Handy for paperclip attachment objects, this formatter will find out what
|
4
|
+
# the url is and make a link to that.
|
5
|
+
#
|
6
|
+
# Controll the link with the :text or :text_method option.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# <%= view @user.avatar %>
|
11
|
+
# <%= view @user.avatar, :text => "Click for the avatar" %>
|
12
|
+
# <%= view @user.avatar, :text_method => :original_file_name %>
|
13
|
+
#
|
14
|
+
# By default, all links have the target _blank, so they won't screw up your
|
15
|
+
# site. Turn it off globally by putting:
|
16
|
+
#
|
17
|
+
# View::FileLink.default_options = {}
|
3
18
|
class FileLink < Formatter
|
4
19
|
|
5
20
|
self.reserved_options = [ :text, :text_method ]
|
@@ -17,11 +32,15 @@ module View
|
|
17
32
|
end
|
18
33
|
|
19
34
|
def text
|
20
|
-
all_options[:text] ||
|
35
|
+
all_options[:text] || formatted_text
|
36
|
+
end
|
37
|
+
|
38
|
+
def formatted_text
|
39
|
+
View.format((text_via_method || value), {:as => :guess}, template, &block)
|
21
40
|
end
|
22
41
|
|
23
42
|
def text_via_method
|
24
|
-
send(text_method) if text_method
|
43
|
+
value.send(text_method) if text_method
|
25
44
|
end
|
26
45
|
|
27
46
|
def text_method
|
data/lib/view/railtie.rb
CHANGED
data/lib/view/version.rb
CHANGED
data/lib/view.rb
CHANGED
@@ -61,15 +61,14 @@ module View
|
|
61
61
|
# gem should be done through this method (notwithstanding custom formatters).
|
62
62
|
#
|
63
63
|
# In every day usage you will access it through the +view+ helper method,
|
64
|
-
# because most formatters require a view to render links or images.
|
65
|
-
# helper method should also point to this method.
|
64
|
+
# because most formatters require a view to render links or images.
|
66
65
|
#
|
67
66
|
# @see View::Helper#view
|
68
67
|
#
|
69
68
|
# @example Rendering a link
|
70
69
|
# module PostsHelper
|
71
70
|
# def link_to_post
|
72
|
-
# View.format @post, :as => :link, self
|
71
|
+
# View.format @post, { :as => :link }, self
|
73
72
|
# end
|
74
73
|
# end
|
75
74
|
#
|
@@ -77,17 +76,15 @@ module View
|
|
77
76
|
# @param [Hash] options Any extra options
|
78
77
|
#
|
79
78
|
# @option options [Symbol] :as (:auto) The name of the formatter
|
80
|
-
# @option options [Array] :block_arguments (
|
79
|
+
# @option options [::Array] :block_arguments ([]) Adds arguments passed to
|
81
80
|
# the block of this method.
|
82
81
|
#
|
83
82
|
# @param [ActionView::Template] Template the view instance on which to call
|
84
83
|
# helper methods like +link_to+ and +image_tag+.
|
85
84
|
# You need this for many formatters.
|
86
85
|
#
|
87
|
-
# @yield [
|
88
|
-
# possible)
|
89
|
-
# Consider it as a final modifier after the view formatter has done its
|
90
|
-
# work.
|
86
|
+
# @yield [formatter] The block will be captured (using the template if
|
87
|
+
# possible) so you can use the formatter in more interesting ways.
|
91
88
|
#
|
92
89
|
# @return [String] the object formatted to a string
|
93
90
|
def self.format(value, options = {}, template = nil, &block)
|
@@ -95,3 +92,5 @@ module View
|
|
95
92
|
end
|
96
93
|
|
97
94
|
end
|
95
|
+
|
96
|
+
require 'view/railtie'
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require "rubygems"
|
2
2
|
require "bundler/setup"
|
3
3
|
Bundler.setup :default
|
4
|
-
require '
|
5
|
-
require '
|
4
|
+
require 'rails'
|
5
|
+
require 'action_view'
|
6
6
|
require 'rspec'
|
7
7
|
require 'view'
|
8
8
|
|
@@ -17,6 +17,24 @@ module WithTranslation
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
+
|
21
|
+
module TemplateHelper
|
22
|
+
|
23
|
+
# make it act like a helper spec
|
24
|
+
def helper
|
25
|
+
@helper ||= Template.new
|
26
|
+
end
|
27
|
+
|
28
|
+
class Template
|
29
|
+
|
30
|
+
include ActionView::Helpers
|
31
|
+
include View::Helper
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
20
37
|
RSpec.configure do |config|
|
21
38
|
config.include(WithTranslation)
|
39
|
+
config.include(TemplateHelper)
|
22
40
|
end
|
data/spec/view/formatter_spec.rb
CHANGED
@@ -2,12 +2,12 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "View::Formatter" do
|
4
4
|
|
5
|
-
it "
|
5
|
+
it "works" do
|
6
6
|
View.format("bar").should == "bar"
|
7
7
|
end
|
8
8
|
|
9
9
|
it "parses the block" do
|
10
|
-
View.format("bar") { |
|
10
|
+
View.format("bar") { |formatter| formatter.to_s.upcase }.should == "BAR"
|
11
11
|
end
|
12
12
|
|
13
13
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe "Currency formatter" do
|
5
|
+
|
6
|
+
before do
|
7
|
+
helper.stub(:number_to_currency).and_return("called")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "calls number_to_currency" do
|
11
|
+
helper.view(19.99, :as => :currency).should == "called"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "allowes no other options" do
|
15
|
+
helper.should_receive(:number_to_currency).with(19.99, :unit => "£")
|
16
|
+
helper.view(19.99, :as => :currency, :unit => "£", :foo => "bar")
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Delimited formatter" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
helper.stub(:number_with_delimiter).and_return("called")
|
7
|
+
end
|
8
|
+
|
9
|
+
it "calls number_with_delimiter" do
|
10
|
+
helper.view(19.99, :as => :delimited).should == "called"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "allowes no other options" do
|
14
|
+
helper.should_receive(:number_with_delimiter).with(19.99, :delimiter => ";")
|
15
|
+
helper.view(19.99, :as => :delimited, :delimiter => ";", :foo => "bar")
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "FileLink formatter" do
|
4
|
+
|
5
|
+
let :object do
|
6
|
+
Struct.new(:to_label, :url) {
|
7
|
+
def file?
|
8
|
+
true
|
9
|
+
end
|
10
|
+
def foo_bar
|
11
|
+
"foo is bar"
|
12
|
+
end
|
13
|
+
}.new("label", "url")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "calls link_to" do
|
17
|
+
helper.should_receive(:link_to).with("label", "url", {:target => "_blank"}).and_return("called")
|
18
|
+
helper.view(object, :as => :file_link).should == "called"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "uses the file_link automatically for paperclip like objects" do
|
22
|
+
helper.should_receive(:link_to).and_return("called")
|
23
|
+
helper.view(object).should == "called"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "uses the :text option for link text" do
|
27
|
+
subject = helper.view(object, :as => :file_link, :text => "foo")
|
28
|
+
subject.should == %|<a href="url" target="_blank">foo</a>|
|
29
|
+
end
|
30
|
+
|
31
|
+
it "uses the :text_method option for link text" do
|
32
|
+
subject = helper.view(object, :as => :file_link, :text_method => :foo_bar)
|
33
|
+
subject.should == %|<a href="url" target="_blank">foo is bar</a>|
|
34
|
+
end
|
35
|
+
|
36
|
+
it "guesses the name of the link" do
|
37
|
+
subject = helper.view(object, :as => :file_link)
|
38
|
+
subject.should == %|<a href="url" target="_blank">label</a>|
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
metadata
CHANGED
@@ -7,8 +7,8 @@ version: !ruby/object:Gem::Version
|
|
7
7
|
- 0
|
8
8
|
- 0
|
9
9
|
- alpha
|
10
|
-
-
|
11
|
-
version: 1.0.0.alpha.
|
10
|
+
- 3
|
11
|
+
version: 1.0.0.alpha.3
|
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-
|
19
|
+
date: 2010-10-16 00:00:00 +02:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -76,8 +76,11 @@ files:
|
|
76
76
|
- spec/view/formatters/auto_spec.rb
|
77
77
|
- spec/view/formatters/blank_spec.rb
|
78
78
|
- spec/view/formatters/boolean_spec.rb
|
79
|
+
- spec/view/formatters/currency_spec.rb
|
79
80
|
- spec/view/formatters/date_spec.rb
|
80
81
|
- spec/view/formatters/datetime_spec.rb
|
82
|
+
- spec/view/formatters/delimited_spec.rb
|
83
|
+
- spec/view/formatters/file_link_spec.rb
|
81
84
|
- spec/view/formatters/guess_spec.rb
|
82
85
|
- spec/view/formatters/self_spec.rb
|
83
86
|
- spec/view/formatters/sentence_spec.rb
|
@@ -122,8 +125,11 @@ test_files:
|
|
122
125
|
- spec/view/formatters/auto_spec.rb
|
123
126
|
- spec/view/formatters/blank_spec.rb
|
124
127
|
- spec/view/formatters/boolean_spec.rb
|
128
|
+
- spec/view/formatters/currency_spec.rb
|
125
129
|
- spec/view/formatters/date_spec.rb
|
126
130
|
- spec/view/formatters/datetime_spec.rb
|
131
|
+
- spec/view/formatters/delimited_spec.rb
|
132
|
+
- spec/view/formatters/file_link_spec.rb
|
127
133
|
- spec/view/formatters/guess_spec.rb
|
128
134
|
- spec/view/formatters/self_spec.rb
|
129
135
|
- spec/view/formatters/sentence_spec.rb
|