Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add failing specs for JSON/XML serialization as it should work

  • Loading branch information...
commit 91cef965727c9f00dbc7044ffe4dfafdda22891a 1 parent e8fda5c
@bensie bensie authored
View
5 carrierwave.gemspec
@@ -22,10 +22,11 @@ Gem::Specification.new do |s|
s.rubygems_version = %q{1.3.5}
s.specification_version = 3
- s.add_dependency "activesupport", ">= 3.2.0.rc2"
+ s.add_dependency "activesupport", ">= 3.2.0"
+ s.add_dependency "activemodel", ">= 3.2.0"
s.add_development_dependency "mysql2"
- s.add_development_dependency "rails", ">= 3.2.0.rc2"
+ s.add_development_dependency "rails", ">= 3.2.0"
s.add_development_dependency "cucumber", "1.1.4"
s.add_development_dependency "json"
s.add_development_dependency "rspec", "~> 2.0"
View
4 lib/carrierwave/uploader/mountable.rb
@@ -7,7 +7,7 @@ module Mountable
attr_reader :model, :mounted_as
##
- # If a model is given as the first parameter, it will stored in the uploader, and
+ # If a model is given as the first parameter, it will be stored in the uploader, and
# available throught +#model+. Likewise, mounted_as stores the name of the column
# where this instance of the uploader is mounted. These values can then be used inside
# your uploader.
@@ -36,4 +36,4 @@ def initialize(model=nil, mounted_as=nil)
end # Mountable
end # Uploader
-end # CarrierWave
+end # CarrierWave
View
3  lib/carrierwave/uploader/url.rb
@@ -33,8 +33,7 @@ def to_s
# [Hash] the locations where this file and versions are accessible via a url
#
def as_json(options = nil)
- h = { :url => url }
- h.merge Hash[versions.map { |name, version| [name, { :url => version.url }] }]
+ {:url => url}.merge Hash[versions.map { |name, version| [name, { :url => version.url }] }]
end
end # Url
View
16 spec/orm/activerecord_spec.rb
@@ -83,8 +83,10 @@ class Event < ActiveRecord::Base; end # setup a basic AR class for testing
it "should return valid JSON when to_json is called when image is nil" do
@event[:image].should be_nil
-
- JSON.parse(@event.to_json)["event#{$arclass}"]["image"].should == {"url"=>nil}
+ hash = JSON.parse(@event.to_json)["event#{$arclass}"]
+ hash.keys.should include("image")
+ hash["image"].keys.should include("url")
+ hash["image"]["url"].should be_nil
end
it "should return valid JSON when to_json is called when image is present" do
@@ -92,14 +94,14 @@ class Event < ActiveRecord::Base; end # setup a basic AR class for testing
@event.save!
@event.reload
- JSON.parse(@event.to_json)["event#{$arclass}"]["image"].should == {"url"=>"/uploads/test.jpeg"}
+ JSON.parse(@event.to_json)["event#{$arclass}"]["image"].should == {"url" => "/uploads/test.jpeg"}
end
- # FIXME to_xml should work like to_json
it "should return valid XML when to_xml is called when image is nil" do
@event[:image].should be_nil
-
- Hash.from_xml(@event.to_xml)["event#{$arclass}"].except("id").should == {"textfile"=>nil, "foo"=>nil, "image"=>nil}
+ hash = Hash.from_xml(@event.to_xml)["event#{$arclass}"]
+ hash.keys.should include("image")
+ hash["image"].should be_nil
end
# FIXME to_xml should work like to_json
@@ -108,7 +110,7 @@ class Event < ActiveRecord::Base; end # setup a basic AR class for testing
@event.save!
@event.reload
- Hash.from_xml(@event.to_xml)["event#{$arclass}"]["image"].should == "/uploads/test.jpeg"
+ Hash.from_xml(@event.to_xml)["event#{$arclass}"]["image"].should == {"url" => "/uploads/test.jpeg"}
end
end
View
54 spec/uploader/url_spec.rb
@@ -6,12 +6,13 @@
describe CarrierWave::Uploader do
before do
- @uploader_class = Class.new(CarrierWave::Uploader::Base)
- @uploader = @uploader_class.new
+ class MyCoolUploader < CarrierWave::Uploader::Base; end
+ @uploader = MyCoolUploader.new
end
after do
FileUtils.rm_rf(public_path)
+ Object.send(:remove_const, "MyCoolUploader") if defined?(::MyCoolUploader)
end
describe '#url' do
@@ -38,7 +39,7 @@ module StorageX; class File; def url; true; end; end; end
end
it "should not raise ArgumentError when versions version exists" do
- @uploader_class.version(:thumb)
+ MyCoolUploader.version(:thumb)
lambda { @uploader.url(:thumb) }.should_not raise_error(ArgumentError)
end
@@ -48,13 +49,13 @@ module StorageX; class File; def url; true; end; end; end
end
it "should get the directory relative to public for a specific version" do
- @uploader_class.version(:thumb)
+ MyCoolUploader.version(:thumb)
@uploader.cache!(File.open(file_path('test.jpg')))
@uploader.url(:thumb).should == '/uploads/tmp/20071201-1234-345-2255/thumb_test.jpg'
end
it "should get the directory relative to public for a nested version" do
- @uploader_class.version(:thumb) do
+ MyCoolUploader.version(:thumb) do
version(:mini)
end
@uploader.cache!(File.open(file_path('test.jpg')))
@@ -62,7 +63,7 @@ module StorageX; class File; def url; true; end; end; end
end
it "should prepend the config option 'base_path', if set" do
- @uploader_class.version(:thumb)
+ MyCoolUploader.version(:thumb)
@uploader.class.configure do |config|
config.base_path = "/base_path"
end
@@ -88,19 +89,48 @@ module StorageX; class File; def url; true; end; end; end
CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
end
+ it "should return a hash with a nil URL" do
+ MyCoolUploader.version(:thumb)
+ hash = JSON.parse(@uploader.to_json)
+ hash.keys.should include("url")
+ hash.keys.should include("thumb")
+ hash["url"].should be_nil
+ hash["thumb"].keys.should include("url")
+ hash["thumb"]["url"].should be_nil
+ end
+
+ it "should return a hash including a cached URL" do
+ @uploader.cache!(File.open(file_path("test.jpg")))
+ JSON.parse(@uploader.to_json).should == {"url" => "/uploads/tmp/20071201-1234-345-2255/test.jpg"}
+ end
+
+ it "should return a hash including a cached URL of a version" do
+ MyCoolUploader.version(:thumb)
+ @uploader.cache!(File.open(file_path("test.jpg")))
+ hash = JSON.parse(@uploader.to_json)
+ hash.keys.should include "thumb"
+ hash["thumb"].should == {"url" => "/uploads/tmp/20071201-1234-345-2255/thumb_test.jpg"}
+ end
+ end
+
+ describe '#to_xml' do
+ before do
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
+ end
+
it "should return a hash with a blank URL" do
- JSON.parse(@uploader.to_json)['url'].should be_nil
+ Hash.from_xml(@uploader.to_xml).should == {"url" => nil}
end
it "should return a hash including a cached URL" do
- @uploader.cache!(File.open(file_path('test.jpg')))
- JSON.parse(@uploader.to_json)['url'].should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
+ @uploader.cache!(File.open(file_path("test.jpg")))
+ Hash.from_xml(@uploader.to_xml).should == {"url" => "/uploads/tmp/20071201-1234-345-2255/test.jpg"}
end
it "should return a hash including a cached URL of a version" do
- @uploader_class.version :thumb
- @uploader.cache!(File.open(file_path('test.jpg')))
- JSON.parse(@uploader.to_json)['thumb']['url'].should == '/uploads/tmp/20071201-1234-345-2255/thumb_test.jpg'
+ MyCoolUploader.version(:thumb)
+ @uploader.cache!(File.open(file_path("test.jpg")))
+ Hash.from_xml(@uploader.to_xml)["thumb"].should == {"url" => "/uploads/tmp/20071201-1234-345-2255/thumb_test.jpg"}
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.