Skip to content

Commit

Permalink
Merge pull request #55 from dustMason/master
Browse files Browse the repository at this point in the history
Should not mutate options[:file_name]
  • Loading branch information
y9v authored May 22, 2017
2 parents 051c588 + 36e49dd commit 4007ece
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 15 deletions.
3 changes: 2 additions & 1 deletion carrierwave-base64.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding: utf-8

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Expand All @@ -23,7 +24,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'carrierwave', '>= 0.8.0'
spec.add_dependency 'mime-types', '~> 3.0'

spec.add_development_dependency 'rails', '>= 3.2.0'
spec.add_development_dependency 'rails', '~> 4'
spec.add_development_dependency 'sqlite3'
spec.add_development_dependency 'mongoid'
spec.add_development_dependency 'carrierwave-mongoid'
Expand Down
15 changes: 8 additions & 7 deletions lib/carrierwave/base64/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Adapter
# rubocop:disable Metrics/PerceivedComplexity
def mount_base64_uploader(attribute, uploader_class, options = {})
mount_uploader attribute, uploader_class, options
options[:file_name] ||= -> { attribute }

define_method "#{attribute}=" do |data|
return if data == send(attribute).to_s
Expand All @@ -17,13 +18,13 @@ def mount_base64_uploader(attribute, uploader_class, options = {})
return super(data) unless data.is_a?(String) &&
data.strip.start_with?('data')

options[:file_name] ||= -> { attribute }
if options[:file_name].is_a?(Proc) && options[:file_name].arity == 1
options[:file_name] = options[:file_name].curry[self]
end
super(Carrierwave::Base64::Base64StringIO.new(
data.strip, options[:file_name]
))
filename = if options[:file_name].respond_to?(:call)
options[:file_name].call(self)
else
options[:file_name].to_s
end

super Carrierwave::Base64::Base64StringIO.new(data.strip, filename)
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
# rubocop:enable Metrics/CyclomaticComplexity
Expand Down
21 changes: 20 additions & 1 deletion spec/adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
User.mount_base64_uploader(
:image, uploader, file_name: ->(u) { u.username }
)
User.new
User.new(username: 'batman')
end

let(:mongoid_model) do
Expand Down Expand Up @@ -66,6 +66,25 @@
mongoid_model.image = 'test.jpg'
end.not_to raise_error
end

context 'with additional instances of the mounting class' do
let(:another_subject) do
another_subject = User.new(username: 'robin')
another_subject.image = File.read(
file_path('fixtures', 'base64_image.fixture')
).strip
another_subject
end

it 'should invoke the file_name proc upon each upload' do
subject.save!
another_subject.save!
another_subject.reload
expect(
another_subject.image.current_path
).to eq file_path('../uploads', 'robin.jpeg')
end
end
end

context 'stored uploads exist for the field' do
Expand Down
22 changes: 19 additions & 3 deletions spec/base64_string_io_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RSpec.describe Carrierwave::Base64::Base64StringIO do
%w(application/vnd.openxmlformats-officedocument.wordprocessingml.document
image/jpeg application/pdf audio/mpeg).each do |content_type|
%w[application/vnd.openxmlformats-officedocument.wordprocessingml.document
image/jpeg application/pdf audio/mpeg].each do |content_type|
context "correct #{content_type} data" do
let(:data) do
"data:#{content_type};base64,/9j/4AAQSkZJRgABAQEASABKdhH//2Q=="
Expand All @@ -22,14 +22,30 @@

it 'calls a function that returns the file_name' do
method = ->(u) { u.username }
base64_string_io = described_class.new data, method.curry[User.new]
base64_string_io = described_class.new(
data, method.curry[User.new(username: 'batman')]
)
expect(base64_string_io.file_name).to eql('batman')
end

it 'accepts a string as the file name as well' do
model = described_class.new data, 'string-file-name'
expect(model.file_name).to eql('string-file-name')
end

it 'issues deprecation warning when string given for file name' do
str = ->(u) { u.username }.curry[User.new(username: 'batman')]
expect do
described_class.new(data, str).file_name
end.to warn('Deprecation')
end

it 'does NOT issue deprecation warning when Proc given for file name' do
prc = -> { 'String' }
expect do
described_class.new(data, prc).file_name
end.not_to warn('Deprecation')
end
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

load 'support/schema.rb'
require 'support/models'
require 'support/custom_expectations/warn_expectation'

def file_path(*paths)
File.expand_path(File.join(File.dirname(__FILE__), *paths))
Expand Down
30 changes: 30 additions & 0 deletions spec/support/custom_expectations/warn_expectation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
RSpec::Matchers.define :warn do |message|
match do |block|
fake_stderr(&block).include? message
end

description do
"warn \"#{message}\""
end

failure_message_for_should do
"expected to #{description}"
end

failure_message_for_should_not do
"expected to not #{description}"
end

def fake_stderr
original_stderr = $stderr
$stderr = StringIO.new
yield
$stderr.string
ensure
$stderr = original_stderr
end

def supports_block_expectations?
true
end
end
4 changes: 1 addition & 3 deletions spec/support/models.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
class User < ActiveRecord::Base
def username
'batman'
end
attr_accessor :username
end

class MongoidModel
Expand Down

0 comments on commit 4007ece

Please sign in to comment.