5
5
require 'active_admin/view_helpers/method_or_proc_helper'
6
6
7
7
RSpec . describe ActiveAdmin ::ViewHelpers ::DisplayHelper do
8
- include ActiveAdmin :: ViewHelpers :: ActiveAdminApplicationHelper
9
- include ActiveAdmin :: ViewHelpers :: AutoLinkHelper
10
- include ActiveAdmin ::ViewHelpers ::DisplayHelper
11
- include MethodOrProcHelper
12
- include ActionView :: Helpers :: UrlHelper
13
- include ActionView :: Helpers :: TranslationHelper
14
- include ActionView ::Helpers ::SanitizeHelper
15
-
16
- def active_admin_namespace
17
- active_admin_application . namespaces [ :admin ]
8
+ let ( :view_klass ) do
9
+ Class . new ( ActionView :: Base ) do
10
+ include ActiveAdmin ::ViewHelpers ::ActiveAdminApplicationHelper
11
+ include ActiveAdmin :: ViewHelpers :: AutoLinkHelper
12
+ include ActiveAdmin :: ViewHelpers :: DisplayHelper
13
+ include MethodOrProcHelper
14
+ include ActionView ::Helpers ::UrlHelper
15
+ include ActionView :: Helpers :: TranslationHelper
16
+ include ActionView :: Helpers :: SanitizeHelper
17
+ end
18
18
end
19
19
20
- def authorized? ( *)
21
- true
22
- end
20
+ let ( :active_admin_namespace ) { view . active_admin_application . namespaces [ :admin ] }
23
21
24
- def url_options
25
- { locale : nil }
26
- end
22
+ let ( :view ) { mock_action_view ( view_klass ) }
27
23
28
- let ( :displayed_name ) { display_name ( resource ) }
24
+ let ( :displayed_name ) { view . display_name ( resource ) }
29
25
30
26
before do
27
+ allow ( view ) . to receive ( :authorized? ) . and_return ( true )
28
+ allow ( view ) . to receive ( :active_admin_namespace ) . and_return ( active_admin_namespace )
29
+ allow ( view ) . to receive ( :url_options ) . and_return ( locale : nil )
30
+
31
31
load_resources do
32
32
ActiveAdmin . register ( User )
33
33
ActiveAdmin . register ( Post ) { belongs_to :user , optional : true }
@@ -106,7 +106,7 @@ def url_options
106
106
it "should default to `to_s`" do
107
107
result = resource . to_s
108
108
109
- expect ( displayed_name ) . to eq sanitize ( result )
109
+ expect ( displayed_name ) . to eq view . sanitize ( result )
110
110
end
111
111
end
112
112
@@ -143,26 +143,26 @@ class ThisModel
143
143
144
144
describe '#format_attribute' do
145
145
it 'calls the provided block to format the value' do
146
- value = format_attribute double ( foo : 2 ) , -> r { r . foo + 1 }
146
+ value = view . format_attribute double ( foo : 2 ) , -> r { r . foo + 1 }
147
147
148
148
expect ( value ) . to eq '3'
149
149
end
150
150
151
151
it 'finds values as methods' do
152
- value = format_attribute double ( name : 'Joe' ) , :name
152
+ value = view . format_attribute double ( name : 'Joe' ) , :name
153
153
154
154
expect ( value ) . to eq 'Joe'
155
155
end
156
156
157
157
it 'finds values from hashes' do
158
- value = format_attribute ( { id : 100 } , :id )
158
+ value = view . format_attribute ( { id : 100 } , :id )
159
159
160
160
expect ( value ) . to eq '100'
161
161
end
162
162
163
163
[ 1 , 1.2 , :a_symbol ] . each do |val |
164
164
it "calls to_s to format the value of type #{ val . class } " do
165
- value = format_attribute double ( foo : val ) , :foo
165
+ value = view . format_attribute double ( foo : val ) , :foo
166
166
167
167
expect ( value ) . to eq val . to_s
168
168
end
@@ -171,39 +171,39 @@ class ThisModel
171
171
it 'localizes dates' do
172
172
date = Date . parse '2016/02/28'
173
173
174
- value = format_attribute double ( date : date ) , :date
174
+ value = view . format_attribute double ( date : date ) , :date
175
175
176
176
expect ( value ) . to eq 'February 28, 2016'
177
177
end
178
178
179
179
it 'localizes times' do
180
180
time = Time . parse '2016/02/28 9:34 PM'
181
181
182
- value = format_attribute double ( time : time ) , :time
182
+ value = view . format_attribute double ( time : time ) , :time
183
183
184
184
expect ( value ) . to eq 'February 28, 2016 21:34'
185
185
end
186
186
187
187
it 'uses a display_name method for arbitrary objects' do
188
188
object = double to_s : :wrong , display_name : :right
189
189
190
- value = format_attribute double ( object : object ) , :object
190
+ value = view . format_attribute double ( object : object ) , :object
191
191
192
192
expect ( value ) . to eq 'right'
193
193
end
194
194
195
195
it 'auto-links ActiveRecord records by association' do
196
196
post = Post . create! author : User . new
197
197
198
- value = format_attribute post , :author
198
+ value = view . format_attribute post , :author
199
199
200
200
expect ( value ) . to match /<a href="\/ admin\/ users\/ \d +"> <\/ a>/
201
201
end
202
202
203
203
it 'auto-links ActiveRecord records & uses a display_name method' do
204
204
post = Post . create! author : User . new ( first_name : 'A' , last_name : 'B' )
205
205
206
- value = format_attribute post , :author
206
+ value = view . format_attribute post , :author
207
207
208
208
expect ( value ) . to match /<a href="\/ admin\/ users\/ \d +">A B<\/ a>/
209
209
end
@@ -213,7 +213,7 @@ class ThisModel
213
213
it 'calls status_tag for boolean values' do
214
214
post = Post . new starred : true
215
215
216
- value = format_attribute post , :starred
216
+ value = view . format_attribute post , :starred
217
217
218
218
expect ( value . to_s ) . to eq "<span class=\" status_tag yes\" >Yes</span>\n "
219
219
end
@@ -226,9 +226,9 @@ class ThisModel
226
226
post . define_singleton_method ( :false_method ) do
227
227
false
228
228
end
229
- true_value = format_attribute post , :true_method
229
+ true_value = view . format_attribute post , :true_method
230
230
expect ( true_value . to_s ) . to eq "<span class=\" status_tag yes\" >Yes</span>\n "
231
- false_value = format_attribute post , :false_method
231
+ false_value = view . format_attribute post , :false_method
232
232
expect ( false_value . to_s ) . to eq "<span class=\" status_tag no\" >No</span>\n "
233
233
end
234
234
0 commit comments