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, options = {}, &block) ⇒ Object

See CarrierWave::Mount#mount_uploader for documentation



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/carrierwave/orm/activerecord.rb', line 13

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

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

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

  after_save "store_#{column}!"
  before_save "write_#{column}_identifier"
  after_destroy "remove_#{column}!"
end

#validates_integrity_of(*attrs) ⇒ Object

Makes the record invalid if the file couldn’t be uploaded due to an integrity error

Accepts the usual parameters for validations in Rails (:if, :unless, etc…)

Note

Set this key in your translations file for I18n:

carrierwave:
  errors:
    integrity: 'Here be an error message'


40
41
42
43
44
45
46
# File 'lib/carrierwave/orm/activerecord.rb', line 40

def validates_integrity_of(*attrs)
  options = attrs.last.is_a?(Hash) ? attrs.last : {}
  options[:message] ||= I18n.t('carrierwave.errors.integrity', :default => 'is not an allowed type of file.')
  validates_each(*attrs) do |record, attr, value|
    record.errors.add attr, options[:message] if record.send("#{attr}_integrity_error")
  end
end

#validates_processing_of(*attrs) ⇒ Object

Makes the record invalid if the file couldn’t be processed (assuming the process failed with a CarrierWave::ProcessingError)

Accepts the usual parameters for validations in Rails (:if, :unless, etc…)

Note

Set this key in your translations file for I18n:

carrierwave:
  errors:
    processing: 'Here be an error message'


62
63
64
65
66
67
68
# File 'lib/carrierwave/orm/activerecord.rb', line 62

def validates_processing_of(*attrs)
  options = attrs.last.is_a?(Hash) ? attrs.last : {}
  options[:message] ||= I18n.t('carrierwave.errors.processing', :default => 'failed to be processed.')
  validates_each(*attrs) do |record, attr, value|
    record.errors.add attr, options[:message] if record.send("#{attr}_processing_error")
  end
end