Module: CarrierWave::ActiveRecord

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

Instance Method Summary collapse

Methods included from Mount

#uploader_options, #uploaders

Instance Method Details

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

See CarrierWave::Mount#mount_uploader for documentation



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

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_options[column.to_sym][:validate_integrity]
  validates_processing_of column if uploader_options[column.to_sym][:validate_processing]

  before_save do |record|
    record.send("store_#{column}!")
  end
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'


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

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'


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

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