├── README └── mongomapper_paperclip.rb /README: -------------------------------------------------------------------------------- 1 | This patch is based on mongoid-paperclip code. 2 | -------------------------------------------------------------------------------- /mongomapper_paperclip.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | 3 | begin 4 | require "paperclip" 5 | rescue LoadError 6 | puts "MongoMapper::Paperclip requires that you install the Paperclip gem." 7 | exit 8 | end 9 | 10 | module Paperclip 11 | class << self 12 | def logger 13 | MongoMapper.logger || Rails.logger 14 | end 15 | end 16 | end 17 | 18 | Paperclip.interpolates :id_partition do |attachment, style| 19 | attachment.instance.id.to_s.scan(/.{4}/).join("/") 20 | end 21 | 22 | module MongoMapper 23 | module Paperclip 24 | 25 | def self.included(base) 26 | base.extend(ClassMethods) 27 | end 28 | 29 | module ClassMethods 30 | 31 | def has_mm_attached_file(field, options = {}) 32 | 33 | unless self.ancestors.include?(::Paperclip) 34 | include ::Paperclip 35 | include ::Paperclip::Glue 36 | end 37 | 38 | has_attached_file(field, options) 39 | 40 | key :"#{field}_file_name", String 41 | key :"#{field}_content_type", String 42 | key :"#{field}_file_size", Integer 43 | key :"#{field}_updated_at", Time 44 | end 45 | 46 | end 47 | 48 | end 49 | end 50 | 51 | module MongoMapper 52 | module Plugins 53 | module EmbeddedCallbacks 54 | module InstanceMethods 55 | def run_callbacks(callback, opts = nil, &block) 56 | embedded_docs = [] 57 | 58 | embedded_associations.each do |association| 59 | embedded_docs += Array(get_proxy(association).send(:load_target)) 60 | end 61 | 62 | block = embedded_docs.inject(block) do |chain, doc| 63 | if doc.class.respond_to?("_#{callback}_callbacks") 64 | lambda { doc.run_callbacks(callback, &chain) } 65 | else 66 | chain 67 | end 68 | end 69 | super callback, &block 70 | end 71 | end 72 | end 73 | end 74 | end 75 | 76 | module Paperclip 77 | class Attachment 78 | def updated_at 79 | time = instance_read(:updated_at) 80 | time && time.to_time.to_f.to_i 81 | end 82 | end 83 | end 84 | --------------------------------------------------------------------------------