From 5b20c4b315d45f85dbc22a82b46903a81e60d14c Mon Sep 17 00:00:00 2001 From: Matthew Rudy Jacobs Date: Tue, 3 Apr 2012 08:29:23 +0800 Subject: [PATCH] fix serializable_hash there was a bug with calling serializable hash with no options also make the :except and :only options function like ActiveRecord expects. --- lib/carrierwave/orm/activerecord.rb | 6 +++++- spec/orm/activerecord_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/carrierwave/orm/activerecord.rb b/lib/carrierwave/orm/activerecord.rb index b9b3bc1f8..2b8c07ddf 100644 --- a/lib/carrierwave/orm/activerecord.rb +++ b/lib/carrierwave/orm/activerecord.rb @@ -45,8 +45,12 @@ def remote_#{column}_url=(url) def serializable_hash(options=nil) hash = {} + + except = options && options[:except] && Array.wrap(options[:except]).map(&:to_s) + only = options && options[:only] && Array.wrap(options[:only]).map(&:to_s) + self.class.uploaders.each do |column, uploader| - if (!options[:only] && !options[:except]) || (options[:only] && options[:only].include?(column)) || (options[:except] && !options[:except].include?(column)) + if (!only && !except) || (only && only.include?(column.to_s)) || (except && !except.include?(column.to_s)) hash[column.to_s] = _mounter(:#{column}).uploader.serializable_hash end end diff --git a/spec/orm/activerecord_spec.rb b/spec/orm/activerecord_spec.rb index 458907f52..6148124d9 100644 --- a/spec/orm/activerecord_spec.rb +++ b/spec/orm/activerecord_spec.rb @@ -332,6 +332,28 @@ def monkey end + describe "#serializable_hash" do + + it "should include the image with url" do + @event.image = stub_file("test.jpg") + @event.serializable_hash["image"].should have_key("url") + end + + it "should include the other columns" do + ["id", "foo"].each do |key| + @event.serializable_hash.should have_key(key) + end + end + + it "should take an option to exclude the image column" do + @event.serializable_hash(:except => :image).should_not have_key("image") + end + + it "should take an option to only include the image column" do + @event.serializable_hash(:only => :image).should have_key("image") + end + end + describe '#destroy' do it "should do nothing when no file has been assigned" do