Module: CarrierWave::DataMapper

Includes:
Mount
Defined in:
lib/carrierwave/orm/datamapper.rb

Instance Method Summary collapse

Methods included from Mount

#uploader_option, #uploader_options, #uploaders

Instance Method Details

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

See CarrierWave::Mount#mount_uploader for documentation



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/carrierwave/orm/datamapper.rb', line 13

def mount_uploader(column, uploader, options={}, &block)
  super

  alias_method :read_uploader, :attribute_get
  alias_method :write_uploader, :attribute_set
  after :save, "store_#{column}!".to_sym
  pre_hook = ::DataMapper.const_defined?(:Validate) ? :valid? : :save
  before pre_hook, "write_#{column}_identifier".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_image=(value)
      _mounter(:#{column}).remove = value
      attribute_set(:#{column}, '') if _mounter(:#{column}).remove?
    end
  RUBY
end