Permalink
Browse files

Respect the :mount_on option.

* When the :mount_on option is specified, use the existing property as
  the uploader identifier column. Otherwise, define a new uploader
  property.
  • Loading branch information...
postmodern committed Feb 17, 2012
1 parent ea025da commit 5a004f10482eb2f213f641f7a12737229abfc108
Showing with 38 additions and 3 deletions.
  1. +7 −3 lib/carrierwave/datamapper.rb
  2. +31 −0 spec/unit/carrier_wave/data_mapper/mount_uploader_spec.rb
@@ -18,7 +18,11 @@ def mount_uploader(column, uploader, options={}, &block)
properties.delete(properties[column])
end
- uploader_property = property(column, Property::Uploader)
+ uploader_property = if options[:mount_on]
+ properties[options[:mount_on]]
+ else
+ property column, Property::Uploader
+ end
super
@@ -37,11 +41,11 @@ def mount_uploader(column, uploader, options={}, &block)
class_eval <<-RUBY
def remove_#{column}=(value)
_mounter(:#{column}).remove = value
- attribute_set(:#{column}, '') if _mounter(:#{column}).remove?
+ attribute_set(:#{uploader_property.name}, '') if _mounter(:#{column}).remove?
end
def #{column}=(value)
- attribute_set(:#{column}, value)
+ attribute_set(:#{uploader_property.name}, value)
super(value)
end
RUBY
@@ -46,4 +46,35 @@ def self.name; :spec_model; end
it { should respond_to("#{uploader_name}=") }
end
end
+
+ context "when using the :mount_on option" do
+ let(:uploader_column) { :image_file_name }
+
+ let(:described_class) do
+ Class.new do
+ include DataMapper::Resource
+
+ property :id, DataMapper::Property::Serial
+
+ property :image_file_name, String
+
+ def self.name; :spec_model; end
+ end
+ end
+
+ let!(:uploader_property) do
+ described_class.mount_uploader uploader_name, uploader,
+ :mount_on => :image_file_name
+ end
+
+ it "should not define a new uploader property" do
+ described_class.properties.should_not be_named(:image)
+ end
+
+ describe 'Uploader Property' do
+ subject { uploader_property }
+
+ its(:name) { should == uploader_column }
+ end
+ end
end

0 comments on commit 5a004f1

Please sign in to comment.