Class: FileColumn::BaseUploadedFile
- Inherits:
-
Object
- Object
- FileColumn::BaseUploadedFile
show all
- Defined in:
- lib/file_column.rb,
lib/magick_file_column.rb
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
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_errors ⇒ Object
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_dir ⇒ Object
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_destroy ⇒ Object
103
104
|
# File 'lib/file_column.rb', line 103
def after_destroy
end
|
#after_save ⇒ Object
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
file.extend FileColumn::FileCompat
end
if file.nil?
delete
else
if file.size == 0
self
else
if file.is_a?(String)
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)
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
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
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
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
|
#options ⇒ Object
106
107
108
|
# File 'lib/file_column.rb', line 106
def options
@instance.send(@options_method)
end
|
#relative_dir ⇒ Object
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_path ⇒ Object
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
|
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
|