|
56 | 56 | context 'when only data is specified' do |
57 | 57 | it 'attaches an avatar to the user' do |
58 | 58 | user.avatar = base64_data |
| 59 | + user.save |
59 | 60 |
|
60 | 61 | expect(user.avatar.attached?).to be |
61 | 62 | end |
62 | 63 |
|
63 | 64 | it 'attached file matches attachment file' do |
64 | 65 | user.avatar = base64_data |
| 66 | + user.save |
65 | 67 |
|
66 | 68 | expect( |
67 | 69 | File.open(ActiveStorage::Blob.service.send(:path_for, user.avatar.key)).read |
|
72 | 74 | context 'when filename is specified along with data' do |
73 | 75 | it 'assigns the specified filename' do |
74 | 76 | user.avatar = data_with_filename |
| 77 | + user.save |
75 | 78 |
|
76 | 79 | expect(user.avatar.filename.to_s).to eq(filename) |
77 | 80 | end |
|
218 | 221 | context 'when only data is specified' do |
219 | 222 | it 'attaches a picture to the user' do |
220 | 223 | user.pictures = base64_data |
| 224 | + user.save |
221 | 225 |
|
222 | 226 | expect(user.pictures.attached?).to be |
223 | 227 | end |
224 | 228 |
|
225 | 229 | it 'attached file matches attachment file' do |
226 | 230 | user.pictures = base64_data |
| 231 | + user.save |
227 | 232 |
|
228 | 233 | expect( |
229 | 234 | File.open(ActiveStorage::Blob.service.send(:path_for, user.pictures.last.key)).read |
|
234 | 239 | context 'when filename is specified' do |
235 | 240 | it 'assigns the specified filename' do |
236 | 241 | user.pictures = data_with_filename |
| 242 | + user.save |
237 | 243 |
|
238 | 244 | expect(user.pictures.first.filename.to_s).to eq(filename) |
239 | 245 | end |
|
244 | 250 | context 'when only data is specified' do |
245 | 251 | it 'attaches an array of pictures to the user' do |
246 | 252 | user.pictures = pictures_attachments |
| 253 | + user.save |
247 | 254 |
|
248 | 255 | expect(user.pictures.count).to eq(2) |
249 | 256 | end |
250 | 257 |
|
251 | 258 | it 'attached file matches attachment file' do |
252 | 259 | user.pictures = pictures_attachments |
| 260 | + user.save |
253 | 261 |
|
254 | 262 | expect( |
255 | 263 | File.open(ActiveStorage::Blob.service.send(:path_for, user.pictures.last.key)).read |
256 | 264 | ).to match(second_file) |
257 | 265 | end |
258 | 266 |
|
259 | 267 | it 'attaches multiple individual pictures to the user' do |
260 | | - user.pictures = base64_data |
261 | | - user.pictures = second_base64_data |
| 268 | + user.pictures = [base64_data, second_base64_data] |
| 269 | + user.save |
262 | 270 |
|
263 | 271 | expect(user.pictures.count).to eq(2) |
264 | 272 | end |
|
267 | 275 | context 'when pictures have a specified filename' do |
268 | 276 | it 'assigns the specified filename for the array of pictures' do |
269 | 277 | user.pictures = attachments_with_filename |
| 278 | + user.save |
270 | 279 |
|
271 | 280 | expect(user.pictures.first.filename.to_s).to eq(filename) |
272 | 281 | expect(user.pictures.second.filename.to_s).to eq(second_filename) |
273 | 282 | end |
274 | 283 |
|
275 | 284 | it 'assigns the specified filename for each individual picture' do |
276 | | - user.pictures = data_with_filename |
277 | | - user.pictures = second_data_with_filename |
| 285 | + user.pictures = [data_with_filename, second_data_with_filename] |
| 286 | + user.save |
278 | 287 |
|
279 | 288 | expect(user.pictures.first.filename.to_s).to eq(filename) |
280 | 289 | expect(user.pictures.second.filename.to_s).to eq(second_filename) |
|
358 | 367 | expect(first_url).not_to eq(second_url) |
359 | 368 | end |
360 | 369 | end |
| 370 | + |
| 371 | + context 'when replacing on assign' do |
| 372 | + before do |
| 373 | + @previous = ActiveStorage.replace_on_assign_to_many |
| 374 | + ActiveStorage.replace_on_assign_to_many = true |
| 375 | + end |
| 376 | + |
| 377 | + after do |
| 378 | + ActiveStorage.replace_on_assign_to_many = @previous |
| 379 | + end |
| 380 | + |
| 381 | + it 'updates the existing record replacing attachments' do |
| 382 | + user.pictures = pictures_attachments |
| 383 | + user.save |
| 384 | + expect(user.pictures.count).to eq(2) |
| 385 | + end |
| 386 | + end |
| 387 | + |
| 388 | + context 'when appending on assign' do |
| 389 | + before do |
| 390 | + @previous = ActiveStorage.replace_on_assign_to_many |
| 391 | + ActiveStorage.replace_on_assign_to_many = false |
| 392 | + end |
| 393 | + |
| 394 | + after do |
| 395 | + ActiveStorage.replace_on_assign_to_many = @previous |
| 396 | + end |
| 397 | + |
| 398 | + it 'updates the existing record appending the new attachments' do |
| 399 | + user.pictures = pictures_attachments |
| 400 | + user.save |
| 401 | + expect(user.pictures.count).to eq(4) |
| 402 | + end |
| 403 | + end |
361 | 404 | end |
362 | 405 | end |
363 | 406 | end |
|
0 commit comments