Use prepend to correctly use the monketpatch's method

This commit is contained in:
Shinya Maeda 2018-05-25 14:31:11 +09:00
parent b358d263ce
commit 50b68fc2ea

View file

@ -5,36 +5,40 @@ module CarrierWave
module Storage
class Fog < Abstract
class File
def read
file_body = file.body
module MonkeyPatch
def read
file_body = file.body
return if file_body.nil?
return file_body unless file_body.is_a?(::File)
return if file_body.nil?
return file_body unless file_body.is_a?(::File)
begin
file_body = ::File.open(file_body.path) if file_body.closed? # Reopen if it's closed
file_body.read
ensure
file_body.close
begin
file_body = ::File.open(file_body.path) if file_body.closed? # Reopen if it's closed
file_body.read
ensure
file_body.close
end
end
def store(new_file)
if new_file.is_a?(self.class) # rubocop:disable Gitlab/LineBreakAroundConditionalBlock
new_file.copy_to(path)
else
fog_file = new_file.to_file
@content_type ||= new_file.content_type # rubocop:disable Gitlab/ModuleWithInstanceVariables
@file = directory.files.create({ # rubocop:disable Gitlab/ModuleWithInstanceVariables
:body => fog_file ? fog_file : new_file.read, # rubocop:disable Gitlab/HashSyntax
:content_type => @content_type, # rubocop:disable Gitlab/HashSyntax,Gitlab/ModuleWithInstanceVariables
:key => path, # rubocop:disable Gitlab/HashSyntax
:public => @uploader.fog_public # rubocop:disable Gitlab/HashSyntax,Gitlab/ModuleWithInstanceVariables
}.merge(@uploader.fog_attributes)) # rubocop:disable Gitlab/ModuleWithInstanceVariables
fog_file.close if fog_file && !fog_file.closed?
end
true
end
end
def store(new_file)
if new_file.is_a?(self.class) # rubocop:disable Gitlab/LineBreakAroundConditionalBlock
new_file.copy_to(path)
else
fog_file = new_file.to_file
@content_type ||= new_file.content_type
@file = directory.files.create({
:body => fog_file ? fog_file : new_file.read, # rubocop:disable Gitlab/HashSyntax
:content_type => @content_type, # rubocop:disable Gitlab/HashSyntax
:key => path, # rubocop:disable Gitlab/HashSyntax
:public => @uploader.fog_public # rubocop:disable Gitlab/HashSyntax
}.merge(@uploader.fog_attributes))
fog_file.close if fog_file && !fog_file.closed?
end
true
end
prepend MonkeyPatch
end
end
end