From 004ffc8389aed040ba6b19f78ed937cfafa18dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janko=20Marohni=C4=87?= Date: Fri, 13 Jan 2023 08:43:34 +0100 Subject: [PATCH] Fix removing attachments with boolean values on Ruby 3.2 Passing boolean values to the remove attachment attribute accidentally worked due to Object#=~ being defined. However, that is removed in Ruby 3.2, so we make the implementation explicitly work for boolean values. Closes #620 --- CHANGELOG.md | 2 ++ lib/shrine/plugins/remove_attachment.rb | 2 ++ test/plugin/remove_attachment_test.rb | 11 +++++++++++ 3 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8c2a1eb1..a106f8582 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## HEAD +* `remove_attachment` – Fix passing boolean values being broken in Ruby 3.2 (@janko) + * `model` – When duplicating a record, make the duplicated attacher reference the duplicated record (@janko) ## 3.4.0 (2021-06-14) diff --git a/lib/shrine/plugins/remove_attachment.rb b/lib/shrine/plugins/remove_attachment.rb index 8125377f1..186cd8d00 100644 --- a/lib/shrine/plugins/remove_attachment.rb +++ b/lib/shrine/plugins/remove_attachment.rb @@ -39,6 +39,8 @@ def change?(file) # Rails sends "0" or "false" if the checkbox hasn't been ticked. def remove? + return remove if [true, false].include?(remove) + remove && remove != "" && remove !~ /\A(0|false)\z/ end end diff --git a/test/plugin/remove_attachment_test.rb b/test/plugin/remove_attachment_test.rb index 91cc5a982..b7d060e07 100644 --- a/test/plugin/remove_attachment_test.rb +++ b/test/plugin/remove_attachment_test.rb @@ -62,6 +62,14 @@ assert @attacher.changed? end + it "deassigns the attached file on true" do + @attacher.file = @attacher.upload(fakeio) + @attacher.remove = true + + assert_nil @attacher.file + assert @attacher.changed? + end + it "keeps the file on falsy value" do @attacher.file = @attacher.upload(fakeio) @@ -73,6 +81,9 @@ @attacher.remove = "false" refute_nil @attacher.file + + @attacher.remove = false + refute_nil @attacher.file end end