6
6
let ( :builder ) { ActiveAdmin ::CSVBuilder . new options , &block }
7
7
let ( :options ) { { } }
8
8
let ( :block ) { -> { } }
9
- before { |ex | builder . send :exec_columns unless ex . metadata [ :skip_exec ] }
9
+
10
+ let ( :view_context ) {
11
+ context = double
12
+ method = MethodOrProcHelper . instance_method ( :call_method_or_proc_on ) . bind ( context )
13
+ allow ( context ) . to receive ( :call_method_or_proc_on ) { |*args | method . call *args }
14
+ context
15
+ }
16
+ let ( :controller ) {
17
+ controller = double view_context : view_context , find_collection : collection
18
+ allow ( controller ) . to receive ( :apply_decorator ) { |r | r }
19
+ controller
20
+ }
21
+ let ( :collection ) { Post . all }
22
+
23
+ before { |ex | builder . send :exec_columns , view_context unless ex . metadata [ :skip_exec ] }
24
+
25
+ before :all do
26
+ Post . destroy_all
27
+ @post1 = Post . create! ( title : "Hello1" , published_date : Date . today - 2 . day )
28
+ @post2 = Post . create! ( title : "Hello2" , published_date : Date . today - 1 . day )
29
+ end
10
30
11
31
context 'when empty' do
12
32
it "has no columns" do
158
178
end
159
179
end
160
180
161
- context "with access to the controller" , skip_exec : true do
162
- let ( :dummy_view_context ) { double ( controller : dummy_controller ) }
163
- let ( :dummy_controller ) { double ( names : %w( title summary updated_at created_at ) ) }
181
+ context "with access to the controller" do
182
+ let ( :view_context ) { double controller : double ( names : %w( title summary updated_at created_at ) ) }
164
183
let ( :block ) {
165
184
-> {
166
185
column "id"
167
186
controller . names . each { |name | column name }
168
187
}
169
188
}
170
- before { builder . send :exec_columns , dummy_view_context }
171
189
172
190
it "builds columns provided by the controller" do
173
191
expect ( builder . columns . map ( &:data ) ) . to match_array ( [ :id , :title , :summary , :updated_at , :created_at ] )
174
192
end
175
193
end
176
194
177
- context "build csv using the supplied order" do
178
- before do
179
- @post1 = Post . create! ( title : "Hello1" , published_date : Date . today - 2 . day )
180
- @post2 = Post . create! ( title : "Hello2" , published_date : Date . today - 1 . day )
195
+ it "generates data ignoring pagination" do
196
+ expect ( controller ) . to receive ( :find_collection ) . with ( except : :pagination ) . once
197
+ expect ( builder ) . to receive ( :build_row ) . and_return ( [ ] ) . twice
198
+ builder . build controller , [ ]
199
+ end
200
+
201
+ describe "paginate_with: :per_page" do
202
+ it "works" do
203
+ expect ( collection ) . to receive ( :find_each ) . and_call_original
204
+ builder . build controller , [ ]
181
205
end
182
- let ( :dummy_controller ) {
183
- class DummyController
184
- def find_collection ( *)
185
- collection
186
- end
206
+ end
187
207
188
- def collection
189
- Post . order ( 'published_date DESC' )
190
- end
208
+ describe "paginate_with: <Proc>" do
209
+ let ( :options ) { { paginate_with : -> collection { collection } } }
191
210
192
- def apply_decorator ( resource )
193
- resource
194
- end
211
+ it "works" do
212
+ expect ( collection ) . to receive ( :each ) . and_call_original
213
+ builder . build controller , [ ]
214
+ end
215
+ end
195
216
196
- def view_context
197
- end
198
- end
199
- DummyController . new
200
- }
217
+ describe "paginate_with: :kaminari" do
218
+ let ( :options ) { { paginate_with : :kaminari } }
219
+ let ( :collection ) { Post . order published_date : :desc }
201
220
let ( :block ) {
202
221
-> {
203
222
column "id"
@@ -209,71 +228,43 @@ def view_context
209
228
it "generates data with the supplied order" do
210
229
expect ( builder ) . to receive ( :build_row ) . and_return ( [ ] ) . once . ordered { |post | expect ( post . id ) . to eq @post2 . id }
211
230
expect ( builder ) . to receive ( :build_row ) . and_return ( [ ] ) . once . ordered { |post | expect ( post . id ) . to eq @post1 . id }
212
- builder . build dummy_controller , [ ]
213
- end
214
-
215
- it "generates data ignoring pagination" do
216
- expect ( dummy_controller ) . to receive ( :find_collection ) .
217
- with ( except : :pagination ) . once .
218
- and_call_original
219
- expect ( builder ) . to receive ( :build_row ) . and_return ( [ ] ) . twice
220
- builder . build dummy_controller , [ ]
231
+ builder . build controller , [ ]
221
232
end
222
-
223
233
end
224
234
225
- context "build csv using specified encoding and encoding_options" do
226
- let ( :dummy_controller ) do
227
- class DummyController
228
- def find_collection ( *)
229
- collection
230
- end
231
-
232
- def collection
233
- Post
234
- end
235
-
236
- def apply_decorator ( resource )
237
- resource
238
- end
239
-
240
- def view_context
241
- end
242
- end
243
- DummyController . new
244
- end
235
+ describe ":encoding and :encoding_options" do
245
236
let ( :encoding ) { Encoding ::ASCII }
246
237
let ( :encoding_options ) { { } }
247
238
let ( :options ) { { encoding : encoding , encoding_options : encoding_options } }
248
239
let ( :block ) {
249
240
-> {
250
- column "おはようございます"
251
- column "title"
241
+ column ( "おはようございます" ) { |p | p . title }
252
242
}
253
243
}
254
244
255
- context "Shift-JIS with options " do
245
+ context "Shift-JIS" do
256
246
let ( :encoding ) { Encoding ::Shift_JIS }
257
247
let ( :encoding_options ) { { invalid : :replace , undef : :replace , replace : "?" } }
258
248
259
249
it "encodes the CSV" do
260
- receiver = [ ]
261
- builder . build dummy_controller , receiver
262
- line = receiver . last
263
- expect ( line . encoding ) . to eq ( encoding )
250
+ csv = [ ]
251
+ builder . build controller , csv
252
+
253
+ expect ( csv . map ( &:encoding ) . uniq ) . to eq [ encoding ]
254
+ expect ( csv ) . to include "おはようございます\n " . encode ( encoding , encoding_options )
264
255
end
265
256
end
266
257
267
- context "ASCII with options " do
258
+ context "ASCII" do
268
259
let ( :encoding ) { Encoding ::ASCII }
269
260
let ( :encoding_options ) { { invalid : :replace , undef : :replace , replace : "__REPLACED__" } }
270
261
271
262
it "encodes the CSV without errors" do
272
- receiver = [ ]
273
- builder . build dummy_controller , receiver
274
- line = receiver . last
275
- expect ( line . encoding ) . to eq ( encoding )
276
- expect ( line ) . to include ( "__REPLACED__" )
263
+ csv = [ ]
264
+ builder . build controller , csv
265
+
266
+ expect ( csv . map ( & : encoding) . uniq ) . to eq [ encoding ]
267
+ expect ( csv . first ) . to include "__REPLACED__"
277
268
end
278
269
end
279
270
end
0 commit comments