Skip to content

Instantly share code, notes, and snippets.

@cheald
Created August 18, 2010 15:02
Show Gist options
  • Save cheald/535026 to your computer and use it in GitHub Desktop.
Save cheald/535026 to your computer and use it in GitHub Desktop.
require 'mongo_mapper'
module CarrierWave
module MongoMapper
include CarrierWave::Mount
##
# See +CarrierWave::Mount#mount_uploader+ for documentation
#
def mount_uploader(column, uploader, options={}, &block)
# We need to set the mount_on column (or key in MongoMapper's case)
# since MongoMapper will attempt to set the filename on
# the uploader instead of the file on a Document's initialization.
options[:mount_on] ||= "#{column}_filename"
key options[:mount_on]
super
alias_method :read_uploader, :[]
alias_method :write_uploader, :[]=
after_save "store_#{column}!".to_sym
before_save "write_#{column}_identifier".to_sym
after_destroy "remove_#{column}!".to_sym
end
end # MongoMapper
end # CarrierWave
MongoMapper::Document.append_extensions(CarrierWave::MongoMapper)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment