Permalink
Browse files

Added more mount features (datamapper)

  • Loading branch information...
1 parent 504ad02 commit 12ec2bce5cd149ad337927d15959397bd87fd6ad @jnicklas jnicklas committed Jun 11, 2009
@@ -19,10 +19,28 @@ Feature: Mount an Uploader on ActiveRecord class
And I save the active record
Then there should be a file at 'public/uploads/bork.txt'
And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
+ And the url for the column 'avatar' should be '/uploads/bork.txt'
Scenario: assign a file and retrieve it from cache
When I assign the file 'fixtures/bork.txt' to the 'avatar' column
And I retrieve the file later from the cache name for the column 'avatar'
And I save the active record
Then there should be a file at 'public/uploads/bork.txt'
- And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
+ And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
+ And the url for the column 'avatar' should be '/uploads/bork.txt'
+
+ Scenario: store a file and retrieve it later
+ When I assign the file 'fixtures/bork.txt' to the 'avatar' column
+ And I retrieve the file later from the cache name for the column 'avatar'
+ And I save the active record
+ Then there should be a file at 'public/uploads/bork.txt'
+ When I reload the active record
+ Then the url for the column 'avatar' should be '/uploads/bork.txt'
+
+ Scenario: store a file and delete the record
+ When I assign the file 'fixtures/bork.txt' to the 'avatar' column
+ And I retrieve the file later from the cache name for the column 'avatar'
+ And I save the active record
+ Then there should be a file at 'public/uploads/bork.txt'
+ When I delete the active record
+ Then there should not be a file at 'public/uploads/bork.txt'
@@ -0,0 +1,46 @@
+Feature: Mount an Uploader on ActiveRecord class
+ In order to easily attach files to a form
+ As a web developer using CarrierWave
+ I want to mount an uploader on an ActiveRecord class
+
+ Background:
+ Given an uploader class that uses the 'file' storage
+ And a datamapper class that has a 'avatar' column
+ And the uploader class is mounted on the 'avatar' column
+ And an instance of the datamapper class
+
+ Scenario: assign a file
+ When I assign the file 'fixtures/bork.txt' to the 'avatar' column
+ Then there should be a file called 'bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
+ And the file called 'bork.txt' in a subdirectory of 'public/uploads/tmp' should be identical to the file at 'fixtures/bork.txt'
+
+ Scenario: assign a file and save the record
+ When I assign the file 'fixtures/bork.txt' to the 'avatar' column
+ And I save the datamapper record
+ Then there should be a file at 'public/uploads/bork.txt'
+ And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
+ And the url for the column 'avatar' should be '/uploads/bork.txt'
+
+ Scenario: assign a file and retrieve it from cache
+ When I assign the file 'fixtures/bork.txt' to the 'avatar' column
+ And I retrieve the file later from the cache name for the column 'avatar'
+ And I save the datamapper record
+ Then there should be a file at 'public/uploads/bork.txt'
+ And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
+ And the url for the column 'avatar' should be '/uploads/bork.txt'
+
+ # Scenario: store a file and retrieve it later
+ # When I assign the file 'fixtures/bork.txt' to the 'avatar' column
+ # And I retrieve the file later from the cache name for the column 'avatar'
+ # And I save the datamapper record
+ # Then there should be a file at 'public/uploads/bork.txt'
+ # When I reload the datamapper record
+ # Then the url for the column 'avatar' should be '/uploads/bork.txt'
+ #
+ # Scenario: store a file and delete the record
+ # When I assign the file 'fixtures/bork.txt' to the 'avatar' column
+ # And I retrieve the file later from the cache name for the column 'avatar'
+ # And I save the datamapper record
+ # Then there should be a file at 'public/uploads/bork.txt'
+ # When I delete the datamapper record
+ # Then there should not be a file at 'public/uploads/bork.txt'
@@ -10,3 +10,11 @@
When /^I save the active record$/ do
@instance.save!
end
+
+When /^I reload the active record$/ do
+ @instance = @instance.class.find(@instance.id)
+end
+
+When /^I delete the active record$/ do
+ @instance.destroy
+end
@@ -0,0 +1,27 @@
+Given /^a datamapper class that has a '([^\']*)' column$/ do |column|
+ @mountee_klass = Class.new do
+ include DataMapper::Resource
+
+ storage_names[:default] = 'users'
+
+ property :id, Integer, :key => true
+ property column.to_sym, String
+ end
+ @mountee_klass.auto_migrate!
+end
+
+Given /^an instance of the datamapper class$/ do
+ @instance = @mountee_klass.new
+end
+
+When /^I save the datamapper record$/ do
+ @instance.save
+end
+
+When /^I reload the datamapper record$/ do
+ @instance = @instance.class.first(:id => @instance.key)
+end
+
+When /^I delete the datamapper record$/ do
+ @instance.destroy
+end
@@ -10,4 +10,8 @@
new_instance = @instance.class.new
new_instance.send("#{column}_cache=", @instance.send("#{column}_cache"))
@instance = new_instance
-end
+end
+
+Then /^the url for the column '([^\']*)' should be '([^\']*)'$/ do |column, url|
+ @instance.send("#{column}_url").should == url
+end
@@ -0,0 +1,5 @@
+require 'dm-core'
+require 'carrierwave/mount'
+require 'carrierwave/orm/datamapper'
+
+DataMapper.setup(:default, 'sqlite3::memory:')
View
@@ -3,6 +3,7 @@
require 'rubygems'
require File.join(File.dirname(__FILE__), 'activerecord')
+require File.join(File.dirname(__FILE__), 'datamapper')
if ENV["AS"]
puts "--> using ActiveSupport"
@@ -17,6 +17,10 @@ def mount_uploader(column, uploader, options={}, &block)
before :save do
send("store_#{column}!")
end
+
+ after :destroy do
+ send("remove_#{column}!")
+ end
end
end # DataMapper
@@ -101,14 +101,14 @@
@event.image.current_path.should == public_path('uploads/test.jpeg')
end
- it "should do nothing when a validation fails" do
- pending "how do we test with and without dm-validations?"
- @class.validate { |r| r.errors.add :textfile, "FAIL!" }
- @event.image = stub_file('test.jpeg')
- @event.save
- @event.image.should be_an_instance_of(@uploader)
- @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
- end
+ # it "should do nothing when a validation fails" do
+ # pending "how do we test with and without dm-validations?"
+ # @class.validate { |r| r.errors.add :textfile, "FAIL!" }
+ # @event.image = stub_file('test.jpeg')
+ # @event.save
+ # @event.image.should be_an_instance_of(@uploader)
+ # @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
+ # end
it "should assign the filename to the database" do
@event.image = stub_file('test.jpeg')
@@ -117,14 +117,14 @@
@event.attribute_get(:image).should == 'test.jpeg'
end
- it "should assign the filename before validation" do
- pending "how do we test with and without dm-validations?"
- @class.validate { |r| r.errors.add_to_base "FAIL!" if r[:image].nil? }
- @event.image = stub_file('test.jpeg')
- @event.save
- @event.reload
- @event.attribute_get(:image).should == 'test.jpeg'
- end
+ # it "should assign the filename before validation" do
+ # pending "how do we test with and without dm-validations?"
+ # @class.validate { |r| r.errors.add_to_base "FAIL!" if r[:image].nil? }
+ # @event.image = stub_file('test.jpeg')
+ # @event.save
+ # @event.reload
+ # @event.attribute_get(:image).should == 'test.jpeg'
+ # end
it "should remove the image if remove_image? returns true" do
@event.image = stub_file('test.jpeg')
@@ -137,5 +137,23 @@
end
end
+
+ describe '#destroy' do
+
+ it "should do nothing when no file has been assigned" do
+ @event.destroy
+ end
+
+ it "should remove the file from the filesystem" do
+ @event.image = stub_file('test.jpeg')
+ @event.save.should be_true
+ File.exist?(public_path('uploads/test.jpeg')).should be_true
+ @event.image.should be_an_instance_of(@uploader)
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
+ @event.destroy
+ File.exist?(public_path('uploads/test.jpeg')).should be_false
+ end
+
+ end
end

0 comments on commit 12ec2bc

Please sign in to comment.