Module: FileUploadCache::CachedAttributes::ClassMethods

Defined in:
lib/file_upload_cache/cached_attributes.rb

Instance Method Summary collapse

Instance Method Details

#cached_file_for(field) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/file_upload_cache/cached_attributes.rb', line 7

def cached_file_for(field)
  attr_accessor :"#{field}_cache_id", :"cached_#{field}"
  define_method "#{field}_with_cache=" do |value|
    instance_variable_set("@#{field}_original", value)
    self.send("#{field}_without_cache=", value)
  end

  alias_method_chain :"#{field}=", :cache

  before_validation lambda { 
    original = self.instance_variable_get("@#{field}_original")
    original.rewind if original && original.respond_to?(:rewind)

    self.send("cached_#{field}=", CachedFile.store(original)) if original.respond_to?(:read)
    if( ! self.send("#{field}_cache_id").blank? && original.blank? )
      cached_file = CachedFile.find(self.send("#{field}_cache_id"))
      if cached_file
        FileUploadCache::Tempfile.for(cached_file.read, cached_file.original_filename) do |tf|
          self.send("#{field}=", tf)
          self.send("cached_#{field}=", cached_file)
        end
      end
    end
  }
end