Skip to content

Commit 2d148f4

Browse files
authored
Merge pull request #3387 from AlchemyCMS/delete-pictures-in-background
Delete multiple pictures in background
2 parents 9c36bb5 + 09314c3 commit 2d148f4

File tree

6 files changed

+68
-78
lines changed

6 files changed

+68
-78
lines changed

app/controllers/alchemy/admin/pictures_controller.rb

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,32 +102,12 @@ def update_multiple
102102
end
103103

104104
def delete_multiple
105-
if request.delete? && params[:picture_ids].present?
106-
pictures = Picture.find(params[:picture_ids])
107-
names = []
108-
not_deletable = []
109-
pictures.each do |picture|
110-
if picture.deletable?
111-
names << picture.name
112-
picture.destroy
113-
else
114-
not_deletable << picture.name
115-
end
116-
end
117-
if not_deletable.any?
118-
flash[:warn] = Alchemy.t(
119-
"These pictures could not be deleted, because they were in use",
120-
names: not_deletable.to_sentence
121-
)
122-
else
123-
flash[:notice] = Alchemy.t("Pictures deleted successfully", names: names.to_sentence)
124-
end
105+
if params[:picture_ids].present?
106+
params[:picture_ids].each { DeletePictureJob.perform_later(_1) }
107+
flash[:notice] = Alchemy.t(:pictures_will_be_deleted_now)
125108
else
126109
flash[:warn] = Alchemy.t("Could not delete Pictures")
127110
end
128-
rescue => e
129-
flash[:error] = e.message
130-
ensure
131111
redirect_to_index
132112
end
133113

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Alchemy
2+
class DeletePictureJob < BaseJob
3+
queue_as :default
4+
5+
def perform(picture_id)
6+
picture = Alchemy::Picture.find_by(id: picture_id)
7+
return if picture.nil? || !picture.deletable?
8+
9+
picture.destroy
10+
end
11+
end
12+
end

config/locales/alchemy.en.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ en:
340340
"Size": "Size"
341341
"Successfully deleted element": "Successfully deleted %{element}"
342342
"Tags": "Tags"
343-
"These pictures could not be deleted, because they were in use": "These pictures could not be deleted, because they were in use: %{names}"
344343
"This page is locked": "This page is currently locked by %{name}"
345344
"Title": "Title"
346345
"Trash": "Trash"
@@ -568,6 +567,7 @@ en:
568567
password: "Password"
569568
paste: "paste"
570569
pictures_in_page: "%{page} in %{pictures}"
570+
pictures_will_be_deleted_now: Pictures will be deleted now
571571
place_link: "Add link"
572572
player_version: "Flash Player version"
573573
"please enter subject and mail address": "Please enter recipient and subject."

spec/controllers/alchemy/admin/pictures_controller_spec.rb

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ module Alchemy
309309
end
310310

311311
describe "#delete_multiple" do
312-
subject { delete :delete_multiple, params: {picture_ids: picture_ids} }
312+
subject do
313+
delete :delete_multiple, params: {picture_ids: picture_ids}
314+
end
313315

314316
it_behaves_like :redirecting_to_picture_library do
315317
let(:subject) do
@@ -319,12 +321,8 @@ module Alchemy
319321
end
320322
end
321323

322-
let(:deletable_picture) do
323-
mock_model("Picture", name: "pic of the pig", deletable?: true)
324-
end
325-
326-
let(:not_deletable_picture) do
327-
mock_model("Picture", name: "pic of the chick", deletable?: false)
324+
let(:picture) do
325+
build_stubbed(:alchemy_picture)
328326
end
329327

330328
context "no picture_ids given" do
@@ -337,49 +335,12 @@ module Alchemy
337335
end
338336

339337
context "picture_ids given" do
340-
context "all are deletable" do
341-
let(:picture_ids) { deletable_picture.id.to_s }
342-
343-
before do
344-
allow(Picture).to receive(:find).and_return([deletable_picture])
345-
end
346-
347-
it "should delete the pictures give a notice about deleting them" do
348-
subject
349-
expect(flash[:notice]).to match("successfully")
350-
end
351-
end
352-
353-
context "deletable and not deletable" do
354-
let(:picture_ids) { "#{deletable_picture.id},#{not_deletable_picture.id}" }
355-
356-
before do
357-
allow(Picture).to receive(:find).and_return([deletable_picture, not_deletable_picture])
358-
end
338+
let(:picture_ids) { [picture.id] }
359339

360-
it "should give a warning for the non deletable pictures and delete the others" do
361-
expect(deletable_picture).to receive(:destroy)
362-
subject
363-
expect(flash[:warn]).to match("could not be deleted")
364-
end
365-
end
366-
367-
context "with error happening" do
368-
let(:picture_ids) { deletable_picture.id.to_s }
369-
370-
before do
371-
expect(Picture).to receive(:find).and_raise("yada")
372-
end
373-
374-
it "sets error message" do
375-
subject
376-
expect(flash[:error]).not_to be_blank
377-
end
378-
379-
it "redirects to index" do
380-
subject
381-
expect(response).to redirect_to admin_pictures_path
382-
end
340+
it "enqueues the picture delete job" do
341+
subject
342+
expect(DeletePictureJob).to have_been_enqueued.with(picture.id.to_s)
343+
expect(flash[:notice]).to match("Pictures will be deleted now")
383344
end
384345
end
385346
end

spec/features/admin/picture_library_integration_spec.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,9 @@
102102
end
103103

104104
within "#flash_notices" do
105-
expect(page).to have_content("Pictures deleted successfully")
105+
expect(page).to have_content("Pictures will be deleted now")
106106
end
107107

108-
expect(page).to_not have_selector("#picture_#{picture_2.id}")
109-
expect(page).to_not have_selector("#picture_#{picture_1.id}")
110-
expect(page).to have_selector("#picture_#{picture_3.id}")
111-
112108
# Keeps existing params
113109
within "#filter_bar" do
114110
expect(page).to have_checked_field("Without tag")
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Alchemy::DeletePictureJob, type: :job do
4+
describe "#perform" do
5+
subject(:perform) { described_class.new.perform(5) }
6+
7+
context "when picture exists" do
8+
let(:picture) { build(:alchemy_picture) }
9+
10+
before do
11+
allow(Alchemy::Picture).to receive(:find_by).with(id: 5) { picture }
12+
end
13+
14+
context "when picture is deletable" do
15+
it "deletes the picture" do
16+
expect(picture).to receive(:deletable?) { true }
17+
expect(picture).to receive(:destroy)
18+
perform
19+
end
20+
end
21+
22+
context "when picture is not deletable" do
23+
it "does not delete the picture" do
24+
expect(picture).to receive(:deletable?) { false }
25+
expect(picture).to_not receive(:destroy)
26+
perform
27+
end
28+
end
29+
end
30+
31+
context "when picture does not exist" do
32+
before do
33+
allow(Alchemy::Picture).to receive(:find_by).with(id: 5) { nil }
34+
end
35+
36+
it "does nothing" do
37+
perform
38+
end
39+
end
40+
end
41+
end

0 commit comments

Comments
 (0)