Browse files

Respect options[:except] and options[:only] when passed to to_json

  • Loading branch information...
1 parent 27cf151 commit c2ed3be3e0f20acb730335c84639f2e24dd9efe6 @agerlic agerlic committed Jul 28, 2012
Showing with 17 additions and 1 deletion.
  1. +3 −1 lib/carrierwave/mongoid.rb
  2. +14 −0 spec/mongoid_spec.rb
View
4 lib/carrierwave/mongoid.rb
@@ -63,7 +63,9 @@ def find_previous_model_for_#{column}
def serializable_hash(options=nil)
hash = {}
self.class.uploaders.each do |column, uploader|
- hash[column.to_s] = _mounter(:#{column}).uploader.serializable_hash
+ 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
+ end
end
super(options).merge(hash)
end
View
14 spec/mongoid_spec.rb
@@ -121,6 +121,20 @@ def extension_white_list
JSON.parse({:data => @doc.image}.to_json).should == {"data"=>{"image"=>{"url"=>"/uploads/test.jpeg"}}}
end
+ it "should respect options[:only] when passed to to_json for the serializable hash" do
+ @doc[:image] = 'test.jpeg'
+ @doc.save!
+ @doc.reload
+ JSON.parse(@doc.to_json({:only => [:_id]})).should == {"_id" => @doc.id.to_s}
+ end
+
+ it "should respect options[:except] when passed to to_json for the serializable hash" do
+ @doc[:image] = 'test.jpeg'
+ @doc.save!
+ @doc.reload
+
+ JSON.parse(@doc.to_json({:except => [:_id, :image]})).should == {"folder" => ""}
+ end
end

0 comments on commit c2ed3be

Please sign in to comment.