Files
nairobi/app/models/concerns/has_attachment.rb
2022-02-23 18:43:48 +01:00

20 lines
442 B
Ruby

module HasAttachment
extend ActiveSupport::Concern
class_methods do
def has_attachment(attribute)
has_one_attached attribute
define_method :"#{attribute}=" do |file|
if file.is_a?(IO)
send(attribute).attach(io: file, filename: File.basename(file.path))
elsif file.nil?
send(attribute).detach
else
send(attribute).attach(file)
end
end
end
end
end