lib/carrierwave/orm/mongoid.rb in locomotive_carrierwave-0.5.0.1 vs lib/carrierwave/orm/mongoid.rb in locomotive_carrierwave-0.5.4.beta1
- old
+ new
@@ -4,15 +4,14 @@
require 'carrierwave/validations/active_model'
module CarrierWave
module Mongoid
include CarrierWave::Mount
-
##
# See +CarrierWave::Mount#mount_uploader+ for documentation
#
- def mount_uploader(column, uploader, options={}, &block)
+ def mount_uploader(column, uploader=nil, options={}, &block)
options[:mount_on] ||= "#{column}_filename"
field options[:mount_on]
super
@@ -22,15 +21,37 @@
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)
- before_save "check_stale_#{column}!".to_sym
- after_save "rename_#{column}!".to_sym
- after_save "store_#{column}!".to_sym
- before_save "write_#{column}_identifier".to_sym
- after_destroy "remove_#{column}!".to_sym
+ after_save :"store_#{column}!"
+ before_save :"write_#{column}_identifier"
+ after_destroy :"remove_#{column}!"
+ 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
+
+ # Note (Didier L.): equivalent of the <column>_will_change! ActiveModel method
+ begin
+ value = __send__(column)
+ value = value.duplicable? ? value.clone : value
+ rescue TypeError, NoMethodError
+ end
+ @modifications[column] = value
+
+ super
+ end
+
+ def #{column}_changed?
+ column = _mounter(:#{column}).serialization_column
+ send(:"\#{column}_changed?")
+ end
+ RUBY
+
end
end # Mongoid
end # CarrierWave
-Mongoid::Document::ClassMethods.send(:include, CarrierWave::Mongoid)
\ No newline at end of file
+Mongoid::Document::ClassMethods.send(:include, CarrierWave::Mongoid)