Skip to content

Instantly share code, notes, and snippets.

@hamin
Forked from cheald/gist:535026
Created August 18, 2010 15:04
Show Gist options
  • Save hamin/535041 to your computer and use it in GitHub Desktop.
Save hamin/535041 to your computer and use it in GitHub Desktop.
MongoMapper CarrierWave adapter
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