Mange-field_helpers 1.0.1
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/History.rdoc +7 -0
- data/README.rdoc +240 -0
- data/VERSION.yml +4 -0
- data/lib/field_helpers.rb +50 -0
- data/lib/field_helpers/base.rb +179 -0
- data/lib/field_helpers/helper.rb +203 -0
- data/lib/field_helpers/kinds.rb +255 -0
- data/spec/field_helpers/base_spec.rb +202 -0
- data/spec/field_helpers/helper_spec.rb +92 -0
- data/spec/field_helpers/kinds_spec.rb +336 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +51 -0
- metadata +67 -0
@@ -0,0 +1,202 @@
|
|
1
|
+
# Copyright (c) 2008
|
2
|
+
# * Magnus Bergmark
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#
|
23
|
+
|
24
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
25
|
+
|
26
|
+
describe FieldHelpers::Base do
|
27
|
+
|
28
|
+
describe "registering kind" do
|
29
|
+
describe "helpers" do
|
30
|
+
it "should allow the registering of a kind handler" do
|
31
|
+
lambda {
|
32
|
+
FieldHelpers::Base.register_kind(:kind_name, :helper_name)
|
33
|
+
}.should_not raise_error
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "discovery" do
|
38
|
+
it "should allow the registering with a proc" do
|
39
|
+
lambda {
|
40
|
+
FieldHelpers::Base.register_kind_discovery(:kind_name, lambda {})
|
41
|
+
}.should_not raise_error
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should allow the registering with a block" do
|
45
|
+
lambda {
|
46
|
+
FieldHelpers::Base.register_kind_discovery(:kind_name) { }
|
47
|
+
}.should_not raise_error
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should not allow the registering without any block or proc" do
|
51
|
+
lambda {
|
52
|
+
FieldHelpers::Base.register_kind_discovery(:kind_name)
|
53
|
+
}.should raise_error(ArgumentError)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "auto-discovering field kind" do
|
59
|
+
it "should default to string" do
|
60
|
+
FieldHelpers::Base.discover_kind(:field, nil).should == :string
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should try every kind" do
|
64
|
+
FieldHelpers::Base.kind_discoverers.each_pair do |kind, method|
|
65
|
+
method.should_receive(:call)
|
66
|
+
end
|
67
|
+
|
68
|
+
FieldHelpers::Base.discover_kind(:field, nil)
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "getting value for view" do
|
74
|
+
before(:each) do
|
75
|
+
@translation_proxy = mock("Model")
|
76
|
+
stub_translation('field', 'Field')
|
77
|
+
|
78
|
+
@obj = mock("Record object", :field => "Value", :class => @translation_proxy)
|
79
|
+
@template = stub("Template context")
|
80
|
+
end
|
81
|
+
|
82
|
+
def stub_field(value)
|
83
|
+
@obj.stub!(:field).and_return(value)
|
84
|
+
end
|
85
|
+
|
86
|
+
def stub_translation(key, value)
|
87
|
+
@translation_proxy.stub!(:human_attribute_name).with(key, anything).and_return(value)
|
88
|
+
end
|
89
|
+
|
90
|
+
def get_value(options = {})
|
91
|
+
FieldHelpers::Base.new(@template, @obj, :field, options).value_for_view
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should auto-discover kind" do
|
95
|
+
FieldHelpers::Base.should_receive(:discover_kind).with(:field, "Value").and_return(:string)
|
96
|
+
get_value
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should not auto-discover kind when given" do
|
100
|
+
FieldHelpers::Base.should_not_receive(:discover_kind)
|
101
|
+
get_value :kind => :string
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "with an unknown kind" do
|
105
|
+
it "should raise an argument error" do
|
106
|
+
lambda { get_value :kind => :unknown }.should raise_error(ArgumentError, /kind/)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe "with a custom format" do
|
111
|
+
it "should return the value in that format" do
|
112
|
+
get_value(:format => 'My %s').should == 'My Value'
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe "when value is nil" do
|
117
|
+
before(:each) do
|
118
|
+
stub_field(nil)
|
119
|
+
stub_translation('no_field', 'Empty')
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should return a translation" do
|
123
|
+
get_value.should == "Empty"
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should not return anything if the no_blanks option is set" do
|
127
|
+
get_value(:no_blanks => true).should == ""
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should return the default value if specified" do
|
131
|
+
get_value(:default => "Default").should == "Default"
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should return the default rather than nothing when both no_blanks and default is given" do
|
135
|
+
get_value(:default => "Default", :no_blanks => true).should == "Default"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe "when value is empty" do
|
140
|
+
before(:each) do
|
141
|
+
stub_field("")
|
142
|
+
stub_translation('no_field', 'Empty')
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should return a translation" do
|
146
|
+
get_value.should == "Empty"
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should not return anything if the no_blanks option is set" do
|
150
|
+
get_value(:no_blanks => true).should == ""
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should return the default value if specified" do
|
154
|
+
get_value(:default => "Default").should == "Default"
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should return the default rather than nothing when both no_blanks and default is given" do
|
158
|
+
get_value(:default => "Default", :no_blanks => true).should == "Default"
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
|
164
|
+
specify {
|
165
|
+
FieldHelpers::Base.should respond_to(:default_options)
|
166
|
+
FieldHelpers::Base.should respond_to(:default_options=)
|
167
|
+
}
|
168
|
+
|
169
|
+
describe "custom default options" do
|
170
|
+
before(:all) do
|
171
|
+
@old_default_options = FieldHelpers::Base.default_options
|
172
|
+
FieldHelpers::Base.default_options = { :test => true }
|
173
|
+
end
|
174
|
+
|
175
|
+
after(:all) do
|
176
|
+
FieldHelpers::Base.default_options = @old_default_options
|
177
|
+
end
|
178
|
+
|
179
|
+
def base_options(options=nil)
|
180
|
+
# We test for nil since we want to differentiate no options passed and an empty hash passed
|
181
|
+
if options
|
182
|
+
FieldHelpers::Base.new(nil, nil, nil, options).options
|
183
|
+
else
|
184
|
+
FieldHelpers::Base.new(nil, nil, nil).options
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
it "should become the options when no options has been passed in" do
|
189
|
+
base_options.should == {:test => true}
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should merge with the options passed in" do
|
193
|
+
base_options(:other => true).should == {:test => true, :other => true}
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should not overwrite passed in options" do
|
197
|
+
base_options(:test => false).should == {:test => false}
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
201
|
+
|
202
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# Copyright (c) 2008
|
2
|
+
# * Magnus Bergmark
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#
|
23
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
24
|
+
|
25
|
+
describe FieldHelpers::Helper, :type => :helper do
|
26
|
+
include FieldHelpers::Helper
|
27
|
+
|
28
|
+
describe "displaying field" do
|
29
|
+
before(:each) do
|
30
|
+
@time = Time.mktime(2008, 04, 01, 18, 0, 0)
|
31
|
+
@model = mock("Model", :human_attribute_name => '')
|
32
|
+
@record = mock("Record", :class => @model, :name => 'Test', :phone => '', :text => "Hello\nWorld\n\nParagraph")
|
33
|
+
@controller = mock("Controller", :class => "Controller", :action_name => 'show')
|
34
|
+
end
|
35
|
+
|
36
|
+
def stub_translation(attribute, value = 'Translated')
|
37
|
+
@model.stub!(:human_attribute_name).with(attribute.to_s).and_return(value)
|
38
|
+
@model.stub!(:human_attribute_name).with(attribute.to_s, anything).and_return(value)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should render inside a divider" do
|
42
|
+
field(@record, :name).should have_tag('div')
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should render even when the field is blank" do
|
46
|
+
field(@record, :phone).should_not be_blank
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should render nothing if value is blank and option :no_blanks is set to true" do
|
50
|
+
field(@record, :phone, :no_blanks => true).should be_blank
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should translate display a translated placeholder value if the real value is blank" do
|
54
|
+
stub_translation(:phone, "My Phone")
|
55
|
+
stub_translation(:no_phone, "No phone here")
|
56
|
+
|
57
|
+
result = field(@record, :phone)
|
58
|
+
result.should include('My Phone')
|
59
|
+
result.should include('No phone here')
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should use default value if field is blank" do
|
63
|
+
field(@record, :phone, :default => 'Default').should include('Default')
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should render the title in a strong" do
|
67
|
+
stub_translation(:name)
|
68
|
+
result = field(@record, :name)
|
69
|
+
result.should have_tag('div > strong')
|
70
|
+
result.should match(%r(<strong>\s*Translated\s*</strong>))
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should render the value" do
|
74
|
+
field(@record, :name).should include('Test')
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should be able to override the title" do
|
78
|
+
stub_translation(:name)
|
79
|
+
I18n.should_receive(:translate).with('overridden').and_return('Overridden!')
|
80
|
+
result = field(@record, :name, :title => 'overridden')
|
81
|
+
result.should_not include('Translated')
|
82
|
+
result.should_not include('Name')
|
83
|
+
result.should include('Overridden!')
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should do a simple markup on the value" do
|
87
|
+
should_receive(:simple_format).with("Hello\nWorld\n\nParagraph")
|
88
|
+
field(@record, :text)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
@@ -0,0 +1,336 @@
|
|
1
|
+
# Copyright (c) 2008
|
2
|
+
# * Magnus Bergmark
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#
|
23
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
24
|
+
|
25
|
+
describe FieldHelpers::Kinds, :type => :helper do
|
26
|
+
before(:each) do
|
27
|
+
@kinds = FieldHelpers::Kinds # Shortcut :-D
|
28
|
+
|
29
|
+
@template = mock("Template context")
|
30
|
+
|
31
|
+
@association = mock(ActiveRecord::Base,
|
32
|
+
:name => "Associated",
|
33
|
+
|
34
|
+
:inspect => "<Associated record>"
|
35
|
+
)
|
36
|
+
@association.stub!(:kind_of?).with(ActiveRecord::Base).and_return(true)
|
37
|
+
|
38
|
+
@record = mock("Record",
|
39
|
+
:string => 'String',
|
40
|
+
:number => 13,
|
41
|
+
:association => @association,
|
42
|
+
:true => true,
|
43
|
+
:false => false,
|
44
|
+
:nil => nil,
|
45
|
+
:date => Date.civil(2001, 2, 14),
|
46
|
+
:time => Time.mktime(2001, 2, 14, 22, 18, 32).in_time_zone,
|
47
|
+
|
48
|
+
:inspect => "<Record>" # Making errors easier to read
|
49
|
+
)
|
50
|
+
end
|
51
|
+
|
52
|
+
def new_base(field, options={})
|
53
|
+
base = FieldHelpers::Base.new(@template, @record, field, options)
|
54
|
+
base.stub!(:inspect => "<Base, #{field.inspect}, #{options.inspect}>") # Making errors easier to read
|
55
|
+
base
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "converting" do
|
59
|
+
describe "string" do
|
60
|
+
it "should just call to_s" do
|
61
|
+
@record.string.should_receive(:to_s)
|
62
|
+
@kinds.convert_to_string(new_base(:string))
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "boolean" do
|
67
|
+
it "should convert true to a translation of :yes" do
|
68
|
+
I18n.should_receive(:translate).with(:yes)
|
69
|
+
@kinds.convert_to_bool(new_base(:true))
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should convert false to a translation of :no" do
|
73
|
+
I18n.should_receive(:translate).with(:no)
|
74
|
+
@kinds.convert_to_bool(new_base(:false))
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should convert nil to a translation of :no" do
|
78
|
+
I18n.should_receive(:translate).with(:no)
|
79
|
+
@kinds.convert_to_bool(new_base(:nil))
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should convert non-false to a translation of :yes" do
|
83
|
+
I18n.should_receive(:translate).with(:yes)
|
84
|
+
@kinds.convert_to_bool(new_base(:string))
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "date" do
|
89
|
+
it "should cast to Date and then convert to string" do
|
90
|
+
@record.date.should_receive(:to_date).and_return(@record.date)
|
91
|
+
@record.date.should_receive(:to_s)
|
92
|
+
@kinds.convert_to_date(new_base(:date))
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "time" do
|
97
|
+
it "should cast to Time and then convert to string using :time format" do
|
98
|
+
@record.date.should_receive(:to_time).and_return(@record.date)
|
99
|
+
@record.date.should_receive(:to_s).with(:time)
|
100
|
+
@kinds.convert_to_time(new_base(:date))
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "datetime" do
|
105
|
+
it "should cast to Time and then convert to string" do
|
106
|
+
@record.time.should_receive(:to_time).and_return(@record.time)
|
107
|
+
@record.time.should_receive(:to_s)
|
108
|
+
@kinds.convert_to_datetime(new_base(:time))
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "money" do
|
113
|
+
it "should call the number_to_currency helper" do
|
114
|
+
@template.should_receive(:number_to_currency).with(13)
|
115
|
+
@kinds.convert_to_money(new_base(:number))
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "email" do
|
120
|
+
it "should call the mail_to helper" do
|
121
|
+
@template.should_receive(:mail_to).with("String")
|
122
|
+
@kinds.convert_to_email(new_base(:string))
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe "link" do
|
127
|
+
before(:each) do
|
128
|
+
@template.stub!(:link_to => '')
|
129
|
+
end
|
130
|
+
|
131
|
+
describe "with external target" do
|
132
|
+
before(:each) do
|
133
|
+
@base = new_base(:association)
|
134
|
+
@kinds.stub!(:get_link_text => '', :get_link_path => '')
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should get the link text" do
|
138
|
+
@kinds.should_receive(:get_link_text).with(@association, @base)
|
139
|
+
@kinds.convert_to_link(@base)
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should get the link path" do
|
143
|
+
@kinds.should_receive(:get_link_path).with(@association, @base)
|
144
|
+
@kinds.convert_to_link(@base)
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should call the link_to helper" do
|
148
|
+
@kinds.stub!(:get_link_path => 'url', :get_link_text => 'text')
|
149
|
+
@template.should_receive(:link_to).with('text', 'url', anything)
|
150
|
+
@kinds.convert_to_link(@base)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe "with self-target" do
|
155
|
+
before(:each) do
|
156
|
+
@base = new_base(:string)
|
157
|
+
@kinds.stub!(:get_link_text => '', :get_link_path => '')
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should get the link text" do
|
161
|
+
@kinds.should_receive(:get_link_text).with(@record, @base)
|
162
|
+
@kinds.convert_to_link(@base)
|
163
|
+
@base.options[:link_field].should == :string
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should get the link path" do
|
167
|
+
@kinds.should_receive(:get_link_path).with(@record, @base)
|
168
|
+
@kinds.convert_to_link(@base)
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should call the link_to helper" do
|
172
|
+
@kinds.stub!(:get_link_path => 'url', :get_link_text => 'text')
|
173
|
+
@template.should_receive(:link_to).with('text', 'url', anything)
|
174
|
+
@kinds.convert_to_link(@base)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should pass link_options to link_to helper" do
|
179
|
+
@template.should_receive(:link_to).with(anything, anything, :test => true)
|
180
|
+
@kinds.convert_to_link(new_base(:string, :link_options => {:test => true}))
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
describe "getting link" do
|
188
|
+
|
189
|
+
before(:each) do
|
190
|
+
@record = mock("Record", :foo => 'Foo')
|
191
|
+
@base = FieldHelpers::Base.new(@template, @record, :foo, {})
|
192
|
+
end
|
193
|
+
|
194
|
+
describe "text" do
|
195
|
+
|
196
|
+
it "should get the text specified in the link_text option" do
|
197
|
+
@base.options[:link_text] = "Correct"
|
198
|
+
@kinds.get_link_text(@record, @base).should == "Correct"
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should get the text of the field specified in the link_field option" do
|
202
|
+
@base.options[:link_field] = :field
|
203
|
+
@record.stub!(:field => 'Correct')
|
204
|
+
@kinds.get_link_text(@record, @base).should == "Correct"
|
205
|
+
end
|
206
|
+
|
207
|
+
it "should return the target converted to a string when no other option is available" do
|
208
|
+
@record.should_receive(:to_s).and_return('Correct')
|
209
|
+
@kinds.get_link_text(@record, @base).should == "Correct"
|
210
|
+
end
|
211
|
+
|
212
|
+
it "should return the specified text over the specified field's text when both are available" do
|
213
|
+
@base.options[:link_text] = "Correct"
|
214
|
+
@base.options[:link_field] = :field
|
215
|
+
@record.stub!(:field => 'Incorrect')
|
216
|
+
@kinds.get_link_text(@record, @base).should == "Correct"
|
217
|
+
end
|
218
|
+
|
219
|
+
describe "and having to auto-discover the field" do
|
220
|
+
it "should return the contents of the name field" do
|
221
|
+
@record.stub!(:name => "Name")
|
222
|
+
@kinds.get_link_text(@record, @base).should == "Name"
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should return the contents of the title field" do
|
226
|
+
@record.stub!(:title => "Title")
|
227
|
+
@kinds.get_link_text(@record, @base).should == "Title"
|
228
|
+
end
|
229
|
+
|
230
|
+
it "should return the contents of the label field" do
|
231
|
+
@record.stub!(:label => "Label")
|
232
|
+
@kinds.get_link_text(@record, @base).should == "Label"
|
233
|
+
end
|
234
|
+
|
235
|
+
it "should return the contents of the name field before the title field" do
|
236
|
+
@record.stub!(:name => 'Name', :title => 'Title')
|
237
|
+
@kinds.get_link_text(@record, @base).should == "Name"
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should return the contents of the title field before the label field" do
|
241
|
+
@record.stub!(:label => 'Label', :title => 'Title')
|
242
|
+
@kinds.get_link_text(@record, @base).should == "Title"
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
end
|
247
|
+
|
248
|
+
describe "path" do
|
249
|
+
|
250
|
+
it "should return the target is no better alternative exists" do
|
251
|
+
@kinds.get_link_path(@record, @base).should == @record
|
252
|
+
end
|
253
|
+
|
254
|
+
it "should return the specified link path if present" do
|
255
|
+
@base.options[:link_path] = 'url'
|
256
|
+
@kinds.get_link_path(@record, @base).should == 'url'
|
257
|
+
end
|
258
|
+
|
259
|
+
it "should evaluate the path if the link path is a proc" do
|
260
|
+
@base.options[:link_path] = lambda { 1+2 }
|
261
|
+
@kinds.get_link_path(@record, @base).should == 3
|
262
|
+
end
|
263
|
+
|
264
|
+
it "should evaluate the path if the link path is a proc and pass the target to it" do
|
265
|
+
@base.options[:link_path] = lambda { |t| t.foo }
|
266
|
+
@kinds.get_link_path(@record, @base).should == "Foo"
|
267
|
+
end
|
268
|
+
|
269
|
+
end
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
describe "discovering" do
|
274
|
+
|
275
|
+
describe "datetime" do
|
276
|
+
it "should be positive when field name ends with _at" do
|
277
|
+
@kinds.discover_datetime("ends_at", nil).should be_true
|
278
|
+
end
|
279
|
+
|
280
|
+
it "should be positive when the value is a Time object" do
|
281
|
+
@kinds.discover_datetime("field", Time.now).should be_true
|
282
|
+
end
|
283
|
+
|
284
|
+
it "should be positive when the value is a TimeWithZone object" do
|
285
|
+
@kinds.discover_datetime("field", Time.now.in_time_zone).should be_true
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
describe "date" do
|
290
|
+
it "should be positive when field name ends with _on" do
|
291
|
+
@kinds.discover_date("ends_on", nil).should be_true
|
292
|
+
end
|
293
|
+
|
294
|
+
it "should be positive when value is a Date object" do
|
295
|
+
@kinds.discover_date("field", Date.today).should be_true
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
describe "email" do
|
300
|
+
it "should be positive when field name contains 'email'" do
|
301
|
+
@kinds.discover_email("user_email_address", nil).should be_true
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
describe "boolean" do
|
306
|
+
it "should be positive when value is true" do
|
307
|
+
@kinds.discover_bool("field", true).should be_true
|
308
|
+
end
|
309
|
+
|
310
|
+
it "should be positive when value is false" do
|
311
|
+
@kinds.discover_bool("field", false).should be_true
|
312
|
+
end
|
313
|
+
|
314
|
+
it "should be positive when field name ends with a question mark" do
|
315
|
+
@kinds.discover_bool("active?", nil).should be_true
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
describe "money" do
|
320
|
+
it "should be positive when field name contains 'price'" do
|
321
|
+
@kinds.discover_money("field_price", nil).should be_true
|
322
|
+
end
|
323
|
+
|
324
|
+
it "should be positive when field name contains 'cost" do
|
325
|
+
@kinds.discover_money("field_cost_amount", nil).should be_true
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
describe "link" do
|
330
|
+
it "should be positive when value is an instance of ActiveRecord::Base" do
|
331
|
+
@kinds.discover_link("field", ActiveRecord::Base.new).should be_true
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
335
|
+
end
|
336
|
+
end
|