Skip to content

Commit

Permalink
Merge pull request #681 from matthewrudy/fix-serializable-hash
Browse files Browse the repository at this point in the history
#serializable_hash broken with no options
  • Loading branch information
bensie committed Apr 3, 2012
2 parents df4b01a + 5b20c4b commit abdf3d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/carrierwave/orm/activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit abdf3d5

Please sign in to comment.