shrinerb / shrine

File Attachment toolkit for Ruby applications
https://shrinerb.com
MIT License
3.18k stars 275 forks source link

[Bug]: remove_attachment error with ruby 3.2 #620

Closed arrowcircle closed 1 year ago

arrowcircle commented 1 year ago

Report

When using remove_attachment plugin with ruby 3.2 this code fails

object.remove_file = true

with error:

NoMethodError:
       undefined method `=~' for true:TrueClass

Expected Behavior

It should work properly and remove file with true value

Actual Behavior

It works with ruby 3.2 only when passing:

object.remove_file = "true"

Steps to Reproduce the Problem

require 'bundler/inline'

gemfile do
  source "https://rubygems.org"
  gem "activerecord"
  gem "shrine"
  gem "sqlite3"
end

require "active_record"
require "shrine"
require "shrine/storage/memory"
require "down"

Shrine.storages = {
  cache: Shrine::Storage::Memory.new,
  store: Shrine::Storage::Memory.new,
}

Shrine.plugin :activerecord

class MyUploader < Shrine
  plugin :remove_attachment
end

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.connection.create_table(:posts) { |t| t.text :image_data }

class Post < ActiveRecord::Base
  include MyUploader::Attachment(:image)
end

post = Post.create(image: Down.download("https://picsum.photos/200/300"))
post.remove_image = true

Ruby Version

3.2.0

Shrine Version

3.4.0

Anything else?

No response

janko commented 1 year ago

Thanks for reporting. I honestly didn't intend for it to work with actual boolean values, but it accidentally does work, and you're right that it should continue working. The error happens because Ruby 3.2 removed Object#=~, which is what made it work before. I will push a fix shortly.

rince commented 1 year ago

@janko Thanks for fixing! Could you update the gem version? I can use this change if I specify the github master branch in the Gemfile, but I would prefer to avoid it if possible.