Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #81 from peteonrails/master

Fix to_json / serializable hash issue when using two uploaders on the same model.
  • Loading branch information...
commit 60d63a029b65a55b1c1a3dd6860019abdb6cee12 2 parents 4e8577d + f977b5d
@rmm5t rmm5t authored
Showing with 26 additions and 2 deletions.
  1. +2 −2 lib/carrierwave/mongoid.rb
  2. +24 −0 spec/mongoid_spec.rb
View
4 lib/carrierwave/mongoid.rb
@@ -64,8 +64,8 @@ def find_previous_model_for_#{column}
def serializable_hash(options=nil)
hash = {}
self.class.uploaders.each do |column, uploader|
- if (!options[:only] && !options[:except]) || (options[:only] && options[:only].include?(column)) || (options[:except] && !options[:except].include?(column))
- hash[column.to_s] = _mounter(:#{column}).uploader.serializable_hash
+ if !options || (!options[:only] && !options[:except]) || (options[:only] && options[:only].include?(column)) || (options[:except] && !options[:except].include?(column))
+ hash[column.to_s] = _mounter(column.to_sym).uploader.serializable_hash
end
end
super(options).merge(hash)
View
24 spec/mongoid_spec.rb
@@ -20,6 +20,7 @@ def define_mongo_class(class_name, &block)
end
class MongoUploader < CarrierWave::Uploader::Base; end
+class AnotherMongoUploader < CarrierWave::Uploader::Base; end
class IntegrityErrorUploader < CarrierWave::Uploader::Base
process :monkey
@@ -765,4 +766,27 @@ def filename
end
end
+
+ context "JSON serialization with multiple uploaders" do
+ before do
+ @class = reset_mongo_class
+ @class.send(:mount_uploader, :textfile,AnotherMongoUploader)
+ @event = @class.new
+ @event.image = stub_file('old.jpeg')
+ @event.textfile = stub_file('old.txt')
+ end
+
+ it "serializes the correct values" do
+ puts @event.serializable_hash
+ @event.serializable_hash["image"]["url"].should match(/old\.jpeg$/)
+ @event.serializable_hash["textfile"]["url"].should match(/old\.txt$/)
+ end
+
+ it "should have JSON for each uploader" do
+ parsed = JSON.parse(@event.to_json)
+ parsed["image"]["url"].should match(/old\.jpeg$/)
+ parsed["textfile"]["url"].should match(/old\.txt$/)
+ end
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.