Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow remove_derivatives to receive string arg normalized to symbol #147

Merged
merged 4 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow remove_derivatives to receive string arg normalized to symbol
  • Loading branch information
jrochkind committed Mar 28, 2022
commit 3cba53ffb93ad81b19f9685c38af3bf9a26c4250
4 changes: 4 additions & 0 deletions lib/shrine/plugins/kithe_persisted_derivatives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ def create_persisted_derivatives(*args, storage: nil, allow_other_changes: false
def remove_persisted_derivatives(*paths, **options)
return if paths.empty?

# Shrine does weird things if we pass in Strings, let's save ourselves
# the terrible debugging on that mistake, and noramlize to symbols
paths = paths.collect(&:to_sym)

other_changes_allowed = !!options.delete(:allow_other_changes)
if record && !other_changes_allowed && record.changed?
raise TypeError.new("Can't safely add_persisted_derivatives on model with unsaved changes. Pass `allow_other_changes: true` to force.")
Expand Down
8 changes: 8 additions & 0 deletions spec/shrine/kithe_persisted_derivatives_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ def sample_deriv_file!(path = Kithe::Engine.root.join("spec/test_support/images/
expect(removed.first.exists?).to be(false)
end

it "can remove string argument normalized to symbol" do
removed = asset.file_attacher.remove_persisted_derivatives("sample1")

expect(asset.file_derivatives.keys).to eq([:sample2])
expect(removed.first).to be_kind_of(Shrine::UploadedFile)
expect(removed.first.exists?).to be false
end

describe "if someone else removed first" do
before do
another_copy = asset.class.find(asset.id)
Expand Down