Module: CarrierWave::ActiveRecord

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

Instance Method Summary collapse

Methods included from Mount

#uploader_option, #uploader_options, #uploaders

Instance Method Details

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

See CarrierWave::Mount#mount_uploader for documentation



14
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/carrierwave/orm/activerecord.rb', line 14

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

  alias_method :read_uploader, :read_attribute
  alias_method :write_uploader, :write_attribute
  public :read_uploader
  public :write_uploader

  include CarrierWave::Validations::ActiveModel

  validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
  validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
  validates_download_of column if uploader_option(column.to_sym, :validate_download)

  after_save :store_#{column}!"
  before_save :write_#{column}_identifier"
  after_commit :remove_#{column}!", :on => :destroy
  after_commit :mark_remove_#{column}_false", :on => :update
  before_update :store_previous_model_for_#{column}"
  after_save :remove_previously_stored_#{column}"

  class_eval <<-RUBY, __FILE__, __LINE__+1
    def #{column}=(new_file)
      column = _mounter(:#{column}).serialization_column
      send(:"\#{column}_will_change!")
      super
    end

    def remote_#{column}_url=(url)
      column = _mounter(:#{column}).serialization_column
      send(:"\#{column}_will_change!")
      super
    end

    def remove_#{column}=(value)
      send(:"#{column}_will_change!")
      super
    end

    def remove_#{column}!
      super
      _mounter(:#{column}).remove = true
      _mounter(:#{column}).write_identifier
    end

    def serializable_hash(options=nil)
      hash = {}

      except = options && options[:except] && Array.wrap(options[:except]).map(&:to_s)
      only   = options && options[:only]   && Array.wrap(options[:only]).map(&:to_s)

      self.class.uploaders.each do |column, uploader|
        if (!only && !except) || (only && only.include?(column.to_s)) || (!only && except && !except.include?(column.to_s))
          hash[column.to_s] = _mounter(column).uploader.serializable_hash
        end
      end
      super(options).merge(hash)
    end
  RUBY

end