Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix to_json / serializable hash issue when using two uploaders on the same model. #81

Merged
merged 1 commit into from

2 participants

@peteonrails
  1. Added two failing tests that illustrate the problem.
  2. Fixed issue by changing serializable hash to use column.to_sym instead of string interpolation (which overwrites the method definition at the time mount_uploader is called)
  3. Fix an unrelated issue that caused my tests to fail: check that options are defined before sending :
@peteonrails peteonrails Correctly serialize JSON for two separate uplaoders on the same model.
1. Added two failing tests that illustrate the problem.
2. Fixed issue by changing serializable hash to use column.to_sym instead of string interpolation (which overwrites the method definition at the time mount_uploader is called)
3. Fix an unrelated issue that caused my tests to fail: check that options are defined before sending :[] (which nil doesn't repond to)
f977b5d
@rmm5t rmm5t merged commit 60d63a0 into carrierwaveuploader:master
@rmm5t
Collaborator

Thanks Pete!

@rmm5t
Collaborator

This fix was just released in v0.3.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 15, 2013
  1. @peteonrails

    Correctly serialize JSON for two separate uplaoders on the same model.

    peteonrails authored
    1. Added two failing tests that illustrate the problem.
    2. Fixed issue by changing serializable hash to use column.to_sym instead of string interpolation (which overwrites the method definition at the time mount_uploader is called)
    3. Fix an unrelated issue that caused my tests to fail: check that options are defined before sending :[] (which nil doesn't repond to)
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.