Skip to content

Commit

Permalink
Fix removing attachments with boolean values on Ruby 3.2
Browse files Browse the repository at this point in the history
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
  • Loading branch information
janko committed Jan 13, 2023
1 parent 723d1de commit 004ffc8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/shrine/plugins/remove_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/plugin/remove_attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -73,6 +81,9 @@

@attacher.remove = "false"
refute_nil @attacher.file

@attacher.remove = false
refute_nil @attacher.file
end
end

Expand Down

0 comments on commit 004ffc8

Please sign in to comment.