Module: CarrierWave::DataMapper

Includes:
Mount
Defined in:
lib/carrierwave/datamapper.rb,
lib/carrierwave/datamapper/property/uploader.rb

Defined Under Namespace

Modules: Property

Instance Method Summary collapse

Instance Method Details

#mount_uploader(column, uploader, options = {}, &block) ⇒ Object

See CarrierWave::Mount#mount_uploader for documentation



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/carrierwave/datamapper.rb', line 15

def mount_uploader(column, uploader, options={}, &block)
  if properties.named?(column)
    warn "Defining property for an uploader is deprecated at #{caller[2]}"
    properties.delete(properties[column])
  end

  uploader_property = if options[:mount_on]
                        properties[options[:mount_on]]
                      else
                        property column, Property::Uploader
                      end

  super

  alias_method :read_uploader,  :attribute_get
  alias_method :write_uploader, :attribute_set

  pre_hook = ::DataMapper.const_defined?(:Validations) ? :valid? : :save

  before pre_hook, "write_#{column}_identifier".to_sym
  after  :save,    "store_#{column}!".to_sym
  after  :destroy, "remove_#{column}!".to_sym

  # FIXME: Hack to work around Datamapper not triggering callbacks
  # for objects that are not dirty. By explicitly calling
  # attribute_set we are marking the record as dirty.
  class_eval <<-RUBY
    def remove_#{column}=(value)
      _mounter(:#{column}).remove = value
      attribute_set(:#{uploader_property.name}, '') if _mounter(:#{column}).remove?
    end

    def #{column}=(value)
      attribute_set(:#{uploader_property.name}, value)
      super(value)
    end
  RUBY

  uploader_property
end