Class: FileColumn::BaseUploadedFile

Inherits:
Object
  • Object
show all
Defined in:
lib/file_column.rb,
lib/magick_file_column.rb

Overview

:nodoc:

Direct Known Subclasses

NoUploadedFile, RealUploadedFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, attr) ⇒ BaseUploadedFile

Returns a new instance of BaseUploadedFile.



39
40
41
42
# File 'lib/file_column.rb', line 39

def initialize(instance,attr)
  @instance, @attr = instance, attr
  @options_method = "#{attr}_options".to_sym
end

Instance Attribute Details

#magick_errorsObject (readonly)

Returns the value of attribute magick_errors.



65
66
67
# File 'lib/magick_file_column.rb', line 65

def magick_errors
  @magick_errors
end

Instance Method Details

#absolute_dirObject



90
91
92
# File 'lib/file_column.rb', line 90

def absolute_dir
  if absolute_path then File.dirname(absolute_path) else nil end
end

#after_destroyObject



103
104
# File 'lib/file_column.rb', line 103

def after_destroy
end

#after_saveObject



98
99
100
101
# File 'lib/file_column.rb', line 98

def after_save
  @on_save.each { |blk| blk.call } if @on_save
  self
end

#assign(file) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/file_column.rb', line 45

def assign(file)
  if file.is_a? File
    # this did not come in via a CGI request. However,
    # assigning files directly may be useful, so we
    # make just this file object similar enough to an uploaded
    # file that we can handle it. 
    file.extend FileColumn::FileCompat
  end

  if file.nil?
    delete
  else
    if file.size == 0
      # user did not submit a file, so we
      # can simply ignore this
      self
    else
      if file.is_a?(String)
        # if file is a non-empty string it is most probably
        # the filename and the user forgot to set the encoding
        # to multipart/form-data. Since we would raise an exception
        # because of the missing "original_filename" method anyways,
        # we raise a more meaningful exception rightaway.
        raise TypeError.new("Do not know how to handle a string with value '#{file}' that was passed to a file_column. Check if the form's encoding has been set to 'multipart/form-data'.")
      end
      upload(file)
    end
  end
end

#create_magick_version_if_needed(version) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/magick_file_column.rb', line 32

def create_magick_version_if_needed(version)
  # RMagick might not have been loaded so far.
  # We do not want to require it on every call of this method
  # as this might be fairly expensive, so we just try if ::Magick
  # exists and require it if not.
  begin 
    ::Magick 
  rescue NameError
    require 'RMagick'
  end

  if version.is_a?(Symbol)
    version_options = options[:magick][:versions][version]
  else
    version_options = MagickExtension::process_options(version)
  end

  unless File.exists?(absolute_path(version_options[:name]))
    begin
      img = ::Magick::Image::read(absolute_path).first
    rescue ::Magick::ImageMagickError
      # we might be called directly from the view here
      # so we just return nil if we cannot load the image
      return nil
    end
    dirname = version_options[:name]
    FileUtils.mkdir File.join(@dir, dirname)
    transform_image(img, version_options, absolute_path(dirname))
  end

  version_options[:name]
end

#has_magick_errors?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/magick_file_column.rb', line 67

def has_magick_errors?
  @magick_errors and !@magick_errors.empty?
end

#just_uploaded?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/file_column.rb', line 75

def just_uploaded?
  @just_uploaded
end

#on_save(&blk) ⇒ Object



79
80
81
82
# File 'lib/file_column.rb', line 79

def on_save(&blk)
  @on_save ||= []
  @on_save << Proc.new
end

#optionsObject



106
107
108
# File 'lib/file_column.rb', line 106

def options
  @instance.send(@options_method)
end

#relative_dirObject



94
95
96
# File 'lib/file_column.rb', line 94

def relative_dir
  if relative_path then File.dirname(relative_path) else nil end
end

#temp_pathObject

the following methods are overriden by sub-classes if needed



86
87
88
# File 'lib/file_column.rb', line 86

def temp_path
  nil
end

#transform_with_magickObject



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

def transform_with_magick
  if needs_transform?
    begin
      img = ::Magick::Image::read(absolute_path).first
    rescue ::Magick::ImageMagickError
      if options[:magick][:image_required]
        @magick_errors ||= []
        @magick_errors << "invalid image"
      end
      return
    end
    
    if options[:magick][:versions]
      options[:magick][:versions].each_pair do |version, version_options|
        next if version_options[:lazy]
        dirname = version_options[:name]
        FileUtils.mkdir File.join(@dir, dirname)
        transform_image(img, version_options, absolute_path(dirname))
      end
    end
    if options[:magick][:size] or options[:magick][:crop] or options[:magick][:transformation] or options[:magick][:attributes]
      transform_image(img, options[:magick], absolute_path)
    end

    GC.start
  end
end