forgeos_core 1.9.5.rc1 → 1.9.5.rc2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +6 -0
- data/README.textile +1 -0
- data/app/helpers/application_helper.rb +6 -5
- data/app/models/administrator.rb +1 -1
- data/app/models/attachment.rb +2 -1
- data/app/models/person.rb +18 -13
- data/app/views/layouts/admin.html.haml +2 -2
- data/config/forgeos_admin_menu.yml +51 -0
- data/config/initializers/forgeos_menu_hash.rb +10 -0
- data/db/migrate/20091103220541_migrate_from_restful_authentication_to_authlogic.rb +3 -25
- data/lib/forgeos.rb +2 -62
- data/lib/forgeos/core.rb +1 -6
- data/lib/forgeos/core/engine.rb +1 -0
- data/lib/forgeos/core/version.rb +1 -1
- data/test/before_travis.sh +9 -0
- data/test/dummy/config/application.rb +8 -1
- data/test/dummy/config/database.yml +1 -21
- data/test/dummy/config/initializers/in_memory_database.rb +8 -0
- data/test/dummy/config/initializers/session_store.rb +1 -1
- data/test/dummy/config/initializers/wrap_parameters.rb +1 -1
- data/test/dummy/db/schema.rb +256 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/RAILS_ENV=test.log +0 -0
- data/test/dummy/log/development.log +546 -0
- data/test/dummy/log/test.log +20047 -0
- data/test/dummy/test/fixtures/geo_zones.yml +1582 -0
- data/test/dummy/test/fixtures/people.yml +11 -0
- data/test/dummy/test/fixtures/settings.yml +70 -0
- data/test/dummy/vendor/plugins/attachment_fu/CHANGELOG +35 -0
- data/test/dummy/vendor/plugins/attachment_fu/LICENSE +20 -0
- data/test/dummy/vendor/plugins/attachment_fu/README +193 -0
- data/test/dummy/vendor/plugins/attachment_fu/Rakefile +22 -0
- data/test/dummy/vendor/plugins/attachment_fu/amazon_s3.yml.tpl +17 -0
- data/test/dummy/vendor/plugins/attachment_fu/init.rb +17 -0
- data/test/dummy/vendor/plugins/attachment_fu/install.rb +7 -0
- data/test/dummy/vendor/plugins/attachment_fu/lib/geometry.rb +93 -0
- data/test/dummy/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb +538 -0
- data/test/dummy/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/backends/cloud_file_backend.rb +211 -0
- data/test/dummy/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/backends/db_file_backend.rb +39 -0
- data/test/dummy/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/backends/file_system_backend.rb +126 -0
- data/test/dummy/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/backends/ftp_backend.rb +255 -0
- data/test/dummy/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/backends/s3_backend.rb +394 -0
- data/test/dummy/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/core_image_processor.rb +59 -0
- data/test/dummy/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb +54 -0
- data/test/dummy/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/image_science_processor.rb +61 -0
- data/test/dummy/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb +132 -0
- data/test/dummy/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/rmagick_processor.rb +57 -0
- data/test/dummy/vendor/plugins/attachment_fu/rackspace_cloudfiles.yml.tpl +14 -0
- data/test/dummy/vendor/plugins/attachment_fu/vendor/red_artisan/core_image/filters/color.rb +27 -0
- data/test/dummy/vendor/plugins/attachment_fu/vendor/red_artisan/core_image/filters/effects.rb +31 -0
- data/test/dummy/vendor/plugins/attachment_fu/vendor/red_artisan/core_image/filters/perspective.rb +25 -0
- data/test/dummy/vendor/plugins/attachment_fu/vendor/red_artisan/core_image/filters/quality.rb +25 -0
- data/test/dummy/vendor/plugins/attachment_fu/vendor/red_artisan/core_image/filters/scale.rb +47 -0
- data/test/dummy/vendor/plugins/attachment_fu/vendor/red_artisan/core_image/filters/watermark.rb +32 -0
- data/test/dummy/vendor/plugins/attachment_fu/vendor/red_artisan/core_image/processor.rb +123 -0
- data/test/dummy/vendor/plugins/open_flash_chart/MIT-LICENSE +20 -0
- data/test/dummy/vendor/plugins/open_flash_chart/README +54 -0
- data/test/dummy/vendor/plugins/open_flash_chart/Rakefile +22 -0
- data/test/dummy/vendor/plugins/open_flash_chart/assets/javascripts/swfobject.js +5 -0
- data/test/dummy/vendor/plugins/open_flash_chart/assets/open-flash-chart-bar-clicking.swf +0 -0
- data/test/dummy/vendor/plugins/open_flash_chart/assets/open-flash-chart.swf +0 -0
- data/test/dummy/vendor/plugins/open_flash_chart/init.rb +9 -0
- data/test/dummy/vendor/plugins/open_flash_chart/install.rb +17 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart.rb +54 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/area_base.rb +18 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/area_hollow.rb +10 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/area_line.rb +10 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/arrow.rb +13 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/background.rb +45 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/bar.rb +18 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/bar_3d.rb +17 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/bar_base.rb +7 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/bar_filled.rb +18 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/bar_glass.rb +67 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/bar_sketch.rb +13 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/bar_stack.rb +29 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/base.rb +123 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/candle.rb +22 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/chart.rb +13 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/dot_base.rb +55 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/h_bar.rb +26 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/legend.rb +7 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/line.rb +23 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/line_base.rb +17 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/line_dot.rb +17 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/line_hollow.rb +10 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/line_style.rb +12 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/linear_regression.rb +39 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/menu.rb +26 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/ofc_ajax.rb +116 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/open_flash_chart_object.rb +53 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/pie.rb +75 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/radar_axis.rb +10 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/radar_axis_labels.rb +10 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/radar_spoke_labels.rb +10 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/scatter.rb +22 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/scatter_line.rb +20 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/shape.rb +20 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/sugar.rb +27 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/tags.rb +62 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/title.rb +10 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/tooltip.rb +25 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/upload_image.rb +1 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/x_axis.rb +18 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/x_axis_label.rb +21 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/x_axis_labels.rb +9 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/x_legend.rb +10 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/y_axis.rb +3 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/y_axis_base.rb +7 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/y_axis_label.rb +12 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/y_axis_labels.rb +7 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/y_axis_right.rb +4 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/y_legend.rb +10 -0
- data/test/dummy/vendor/plugins/open_flash_chart/lib/open_flash_chart/y_legend_right.rb +6 -0
- data/test/dummy/vendor/plugins/open_flash_chart/tasks/open_flash_chart_tasks.rake +4 -0
- data/test/dummy/vendor/plugins/open_flash_chart/uninstall.rb +1 -0
- data/test/files/admin_menu.yml +1 -0
- data/test/files/database.yml.mysql +5 -0
- data/test/files/database.yml.postgres +4 -0
- data/test/files/database.yml.sqlite +5 -0
- data/test/fixtures/geo_zones.yml +1582 -0
- data/test/fixtures/people.yml +43 -0
- data/test/fixtures/settings.yml +70 -0
- data/test/forgeos_core_test.rb +30 -2
- data/test/test_helper.rb +8 -0
- data/test/unit/administrator_test.rb +30 -0
- data/test/unit/person_test.rb +103 -0
- data/test/unit/user_test.rb +16 -0
- metadata +251 -37
- data/db/migrate/20110310144114_add_perishable_token_to_people.rb +0 -10
@@ -0,0 +1,538 @@
|
|
1
|
+
IS_RAILS_3 = Rails::VERSION::MAJOR >= 3
|
2
|
+
module Technoweenie # :nodoc:
|
3
|
+
module AttachmentFu # :nodoc:
|
4
|
+
@@default_processors = %w(ImageScience Rmagick MiniMagick Gd2 CoreImage)
|
5
|
+
@@tempfile_path = Rails.root.join('tmp', 'attachment_fu').to_s
|
6
|
+
@@content_types = [
|
7
|
+
'image/jpeg',
|
8
|
+
'image/pjpeg',
|
9
|
+
'image/jpg',
|
10
|
+
'image/gif',
|
11
|
+
'image/png',
|
12
|
+
'image/x-png',
|
13
|
+
'image/jpg',
|
14
|
+
'image/x-ms-bmp',
|
15
|
+
'image/bmp',
|
16
|
+
'image/x-bmp',
|
17
|
+
'image/x-bitmap',
|
18
|
+
'image/x-xbitmap',
|
19
|
+
'image/x-win-bitmap',
|
20
|
+
'image/x-windows-bmp',
|
21
|
+
'image/ms-bmp',
|
22
|
+
'application/bmp',
|
23
|
+
'application/x-bmp',
|
24
|
+
'application/x-win-bitmap',
|
25
|
+
'application/preview',
|
26
|
+
'image/jp_',
|
27
|
+
'application/jpg',
|
28
|
+
'application/x-jpg',
|
29
|
+
'image/pipeg',
|
30
|
+
'image/vnd.swiftview-jpeg',
|
31
|
+
'image/x-xbitmap',
|
32
|
+
'application/png',
|
33
|
+
'application/x-png',
|
34
|
+
'image/gi_',
|
35
|
+
'image/x-citrix-pjpeg'
|
36
|
+
]
|
37
|
+
mattr_reader :content_types, :tempfile_path, :default_processors
|
38
|
+
mattr_writer :tempfile_path
|
39
|
+
|
40
|
+
class ThumbnailError < StandardError; end
|
41
|
+
class AttachmentError < StandardError; end
|
42
|
+
|
43
|
+
module ActMethods
|
44
|
+
# Options:
|
45
|
+
# * <tt>:content_type</tt> - Allowed content types. Allows all by default. Use :image to allow all standard image types.
|
46
|
+
# * <tt>:min_size</tt> - Minimum size allowed. 1 byte is the default.
|
47
|
+
# * <tt>:max_size</tt> - Maximum size allowed. 1.megabyte is the default.
|
48
|
+
# * <tt>:size</tt> - Range of sizes allowed. (1..1.megabyte) is the default. This overrides the :min_size and :max_size options.
|
49
|
+
# * <tt>:resize_to</tt> - Used by RMagick to resize images. Pass either an array of width/height, or a geometry string.
|
50
|
+
# * <tt>:thumbnails</tt> - Specifies a set of thumbnails to generate. This accepts a hash of filename suffixes and RMagick resizing options.
|
51
|
+
# * <tt>:thumbnail_class</tt> - Set what class to use for thumbnails. This attachment class is used by default.
|
52
|
+
# * <tt>:path_prefix</tt> - path to store the uploaded files. Uses public/#{table_name} by default for the filesystem, and just #{table_name}
|
53
|
+
# for the S3 backend. Setting this sets the :storage to :file_system.
|
54
|
+
|
55
|
+
# * <tt>:storage</tt> - Use :file_system to specify the attachment data is stored with the file system. Defaults to :db_system.
|
56
|
+
# * <tt>:cloundfront</tt> - Set to true if you are using S3 storage and want to serve the files through CloudFront. You will need to
|
57
|
+
# set a distribution domain in the amazon_s3.yml config file. Defaults to false
|
58
|
+
# * <tt>:bucket_key</tt> - Use this to specify a different bucket key other than :bucket_name in the amazon_s3.yml file. This allows you to use
|
59
|
+
# different buckets for different models. An example setting would be :image_bucket and the you would need to define the name of the corresponding
|
60
|
+
# bucket in the amazon_s3.yml file.
|
61
|
+
|
62
|
+
# * <tt>:keep_profile</tt> By default image EXIF data will be stripped to minimize image size. For small thumbnails this proivides important savings. Picture quality is not affected. Set to false if you want to keep the image profile as is. ImageScience will allways keep EXIF data.
|
63
|
+
#
|
64
|
+
# Examples:
|
65
|
+
# has_attachment :max_size => 1.kilobyte
|
66
|
+
# has_attachment :size => 1.megabyte..2.megabytes
|
67
|
+
# has_attachment :content_type => 'application/pdf'
|
68
|
+
# has_attachment :content_type => ['application/pdf', 'application/msword', 'text/plain']
|
69
|
+
# has_attachment :content_type => :image, :resize_to => [50,50]
|
70
|
+
# has_attachment :content_type => ['application/pdf', :image], :resize_to => 'x50'
|
71
|
+
# has_attachment :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }
|
72
|
+
# has_attachment :storage => :file_system, :path_prefix => 'public/files'
|
73
|
+
# has_attachment :storage => :file_system, :path_prefix => 'public/files',
|
74
|
+
# :content_type => :image, :resize_to => [50,50]
|
75
|
+
# has_attachment :storage => :file_system, :path_prefix => 'public/files',
|
76
|
+
# :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }
|
77
|
+
# has_attachment :storage => :s3
|
78
|
+
def has_attachment(options = {})
|
79
|
+
# this allows you to redefine the acts' options for each subclass, however
|
80
|
+
options[:min_size] ||= 1
|
81
|
+
options[:max_size] ||= 1.megabyte
|
82
|
+
options[:size] ||= (options[:min_size]..options[:max_size])
|
83
|
+
options[:thumbnails] ||= {}
|
84
|
+
options[:thumbnail_class] ||= self
|
85
|
+
options[:s3_access] ||= :public_read
|
86
|
+
options[:cloudfront] ||= false
|
87
|
+
options[:content_type] = [options[:content_type]].flatten.collect! { |t| t == :image ? Technoweenie::AttachmentFu.content_types : t }.flatten unless options[:content_type].nil?
|
88
|
+
|
89
|
+
unless options[:thumbnails].is_a?(Hash)
|
90
|
+
raise ArgumentError, ":thumbnails option should be a hash: e.g. :thumbnails => { :foo => '50x50' }"
|
91
|
+
end
|
92
|
+
|
93
|
+
extend ClassMethods unless (class << self; included_modules; end).include?(ClassMethods)
|
94
|
+
include InstanceMethods unless included_modules.include?(InstanceMethods)
|
95
|
+
|
96
|
+
parent_options = attachment_options || {}
|
97
|
+
# doing these shenanigans so that #attachment_options is available to processors and backends
|
98
|
+
self.attachment_options = options
|
99
|
+
|
100
|
+
attr_accessor :thumbnail_resize_options
|
101
|
+
|
102
|
+
attachment_options[:storage] ||= (attachment_options[:file_system_path] || attachment_options[:path_prefix]) ? :file_system : :db_file
|
103
|
+
attachment_options[:storage] ||= parent_options[:storage]
|
104
|
+
attachment_options[:path_prefix] ||= attachment_options[:file_system_path]
|
105
|
+
if attachment_options[:path_prefix].nil?
|
106
|
+
attachment_options[:path_prefix] = case attachment_options[:storage]
|
107
|
+
when :s3 then table_name
|
108
|
+
when :cloud_files then table_name
|
109
|
+
else File.join("public", table_name)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
attachment_options[:path_prefix] = attachment_options[:path_prefix][1..-1] if options[:path_prefix].first == '/'
|
113
|
+
|
114
|
+
association_options = { :foreign_key => 'parent_id' }
|
115
|
+
if attachment_options[:association_options]
|
116
|
+
association_options.merge!(attachment_options[:association_options])
|
117
|
+
end
|
118
|
+
with_options(association_options) do |m|
|
119
|
+
m.has_many :thumbnails, :class_name => "::#{attachment_options[:thumbnail_class]}"
|
120
|
+
m.belongs_to :parent, :class_name => "::#{base_class}" unless options[:thumbnails].empty?
|
121
|
+
end
|
122
|
+
|
123
|
+
storage_mod = Technoweenie::AttachmentFu::Backends.const_get("#{options[:storage].to_s.classify}Backend")
|
124
|
+
include storage_mod unless included_modules.include?(storage_mod)
|
125
|
+
|
126
|
+
case attachment_options[:processor]
|
127
|
+
when :none, nil
|
128
|
+
processors = Technoweenie::AttachmentFu.default_processors.dup
|
129
|
+
begin
|
130
|
+
if processors.any?
|
131
|
+
attachment_options[:processor] = processors.first
|
132
|
+
processor_mod = Technoweenie::AttachmentFu::Processors.const_get("#{attachment_options[:processor].to_s.classify}Processor")
|
133
|
+
include processor_mod unless included_modules.include?(processor_mod)
|
134
|
+
end
|
135
|
+
rescue Object, Exception
|
136
|
+
raise unless load_related_exception?($!)
|
137
|
+
|
138
|
+
processors.shift
|
139
|
+
retry
|
140
|
+
end
|
141
|
+
else
|
142
|
+
begin
|
143
|
+
processor_mod = Technoweenie::AttachmentFu::Processors.const_get("#{attachment_options[:processor].to_s.classify}Processor")
|
144
|
+
include processor_mod unless included_modules.include?(processor_mod)
|
145
|
+
rescue Object, Exception
|
146
|
+
raise unless load_related_exception?($!)
|
147
|
+
|
148
|
+
puts "Problems loading #{options[:processor]}Processor: #{$!}"
|
149
|
+
end
|
150
|
+
end unless parent_options[:processor] # Don't let child override processor
|
151
|
+
end
|
152
|
+
|
153
|
+
def load_related_exception?(e) #:nodoc: implementation specific
|
154
|
+
case
|
155
|
+
when e.kind_of?(LoadError), e.kind_of?(MissingSourceFile), $!.class.name == "CompilationError"
|
156
|
+
# We can't rescue CompilationError directly, as it is part of the RubyInline library.
|
157
|
+
# We must instead rescue RuntimeError, and check the class' name.
|
158
|
+
true
|
159
|
+
else
|
160
|
+
false
|
161
|
+
end
|
162
|
+
end
|
163
|
+
private :load_related_exception?
|
164
|
+
end
|
165
|
+
|
166
|
+
module ClassMethods
|
167
|
+
delegate :content_types, :to => Technoweenie::AttachmentFu
|
168
|
+
|
169
|
+
# Performs common validations for attachment models.
|
170
|
+
def validates_as_attachment
|
171
|
+
validates_presence_of :size, :content_type, :filename
|
172
|
+
validate :attachment_attributes_valid?
|
173
|
+
end
|
174
|
+
|
175
|
+
# Returns true or false if the given content type is recognized as an image.
|
176
|
+
def image?(content_type)
|
177
|
+
content_types.include?(content_type)
|
178
|
+
end
|
179
|
+
|
180
|
+
def self.extended(base)
|
181
|
+
if IS_RAILS_3
|
182
|
+
base.class_attribute :attachment_options
|
183
|
+
else
|
184
|
+
base.class_inheritable_accessor :attachment_options
|
185
|
+
end
|
186
|
+
base.before_destroy :destroy_thumbnails
|
187
|
+
base.before_validation :set_size_from_temp_path
|
188
|
+
base.after_save :after_process_attachment
|
189
|
+
base.after_destroy :destroy_file
|
190
|
+
base.after_validation :process_attachment
|
191
|
+
#if defined?(::ActiveSupport::Callbacks)
|
192
|
+
# base.define_callbacks :after_resize, :after_attachment_saved, :before_thumbnail_saved
|
193
|
+
#end
|
194
|
+
end
|
195
|
+
|
196
|
+
unless defined?(::ActiveSupport::Callbacks)
|
197
|
+
# Callback after an image has been resized.
|
198
|
+
#
|
199
|
+
# class Foo < ActiveRecord::Base
|
200
|
+
# acts_as_attachment
|
201
|
+
# after_resize do |record, img|
|
202
|
+
# record.aspect_ratio = img.columns.to_f / img.rows.to_f
|
203
|
+
# end
|
204
|
+
# end
|
205
|
+
def after_resize(&block)
|
206
|
+
if IS_RAILS_3
|
207
|
+
self.after_resize = [block]
|
208
|
+
else
|
209
|
+
write_inheritable_array(:after_resize, [block])
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
# Callback after an attachment has been saved either to the file system or the DB.
|
214
|
+
# Only called if the file has been changed, not necessarily if the record is updated.
|
215
|
+
#
|
216
|
+
# class Foo < ActiveRecord::Base
|
217
|
+
# acts_as_attachment
|
218
|
+
# after_attachment_saved do |record|
|
219
|
+
# ...
|
220
|
+
# end
|
221
|
+
# end
|
222
|
+
def after_attachment_saved(&block)
|
223
|
+
if IS_RAILS_3
|
224
|
+
self.after_attachment_saved = [block]
|
225
|
+
else
|
226
|
+
write_inheritable_array(:after_attachment_saved, [block])
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
# Callback before a thumbnail is saved. Use this to pass any necessary extra attributes that may be required.
|
231
|
+
#
|
232
|
+
# class Foo < ActiveRecord::Base
|
233
|
+
# acts_as_attachment
|
234
|
+
# before_thumbnail_saved do |thumbnail|
|
235
|
+
# record = thumbnail.parent
|
236
|
+
# ...
|
237
|
+
# end
|
238
|
+
# end
|
239
|
+
def before_thumbnail_saved(&block)
|
240
|
+
if IS_RAILS_3
|
241
|
+
self.before_thumbnail_saved = [block]
|
242
|
+
else
|
243
|
+
write_inheritable_array(:before_thumbnail_saved, [block])
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
# Get the thumbnail class, which is the current attachment class by default.
|
249
|
+
# Configure this with the :thumbnail_class option.
|
250
|
+
def thumbnail_class
|
251
|
+
attachment_options[:thumbnail_class] = attachment_options[:thumbnail_class].constantize unless attachment_options[:thumbnail_class].is_a?(Class)
|
252
|
+
attachment_options[:thumbnail_class]
|
253
|
+
end
|
254
|
+
|
255
|
+
# Copies the given file path to a new tempfile, returning the closed tempfile.
|
256
|
+
def copy_to_temp_file(file, temp_base_name)
|
257
|
+
Tempfile.new(temp_base_name, Technoweenie::AttachmentFu.tempfile_path).tap do |tmp|
|
258
|
+
tmp.close
|
259
|
+
FileUtils.cp file, tmp.path
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
# Writes the given data to a new tempfile, returning the closed tempfile.
|
264
|
+
def write_to_temp_file(data, temp_base_name)
|
265
|
+
Tempfile.new(temp_base_name, Technoweenie::AttachmentFu.tempfile_path).tap do |tmp|
|
266
|
+
tmp.binmode
|
267
|
+
tmp.write data
|
268
|
+
tmp.close
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
module InstanceMethods
|
274
|
+
def self.included(base)
|
275
|
+
base.define_callbacks *[:after_resize, :after_attachment_saved, :before_thumbnail_saved] if base.respond_to?(:define_callbacks)
|
276
|
+
end
|
277
|
+
|
278
|
+
# Checks whether the attachment's content type is an image content type
|
279
|
+
def image?
|
280
|
+
self.class.image?(content_type)
|
281
|
+
end
|
282
|
+
|
283
|
+
# Returns true/false if an attachment is thumbnailable. A thumbnailable attachment has an image content type and the parent_id attribute.
|
284
|
+
def thumbnailable?
|
285
|
+
image? && respond_to?(:parent_id) && parent_id.nil?
|
286
|
+
end
|
287
|
+
|
288
|
+
# Returns the class used to create new thumbnails for this attachment.
|
289
|
+
def thumbnail_class
|
290
|
+
self.class.thumbnail_class
|
291
|
+
end
|
292
|
+
|
293
|
+
# Gets the thumbnail name for a filename. 'foo.jpg' becomes 'foo_thumbnail.jpg'
|
294
|
+
def thumbnail_name_for(thumbnail = nil)
|
295
|
+
return filename if thumbnail.blank?
|
296
|
+
ext = nil
|
297
|
+
basename = filename.gsub /\.\w+$/ do |s|
|
298
|
+
ext = s; ''
|
299
|
+
end
|
300
|
+
# ImageScience doesn't create gif thumbnails, only pngs
|
301
|
+
ext.sub!(/gif$/, 'png') if attachment_options[:processor] == "ImageScience"
|
302
|
+
"#{basename}_#{thumbnail}#{ext}"
|
303
|
+
end
|
304
|
+
|
305
|
+
# Creates or updates the thumbnail for the current attachment.
|
306
|
+
def create_or_update_thumbnail(temp_file, file_name_suffix, *size)
|
307
|
+
thumbnailable? || raise(ThumbnailError.new("Can't create a thumbnail if the content type is not an image or there is no parent_id column"))
|
308
|
+
find_or_initialize_thumbnail(file_name_suffix).tap do |thumb|
|
309
|
+
thumb.temp_paths.unshift temp_file
|
310
|
+
thumb.send(:'attributes=', {
|
311
|
+
:content_type => content_type,
|
312
|
+
:filename => thumbnail_name_for(file_name_suffix),
|
313
|
+
:thumbnail_resize_options => size
|
314
|
+
}, false)
|
315
|
+
callback_with_args :before_thumbnail_saved, thumb
|
316
|
+
thumb.save!
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
# Sets the content type.
|
321
|
+
def content_type=(new_type)
|
322
|
+
write_attribute :content_type, new_type.to_s.strip
|
323
|
+
end
|
324
|
+
|
325
|
+
# Sanitizes a filename.
|
326
|
+
def filename=(new_name)
|
327
|
+
write_attribute :filename, sanitize_filename(new_name)
|
328
|
+
end
|
329
|
+
|
330
|
+
# Returns the width/height in a suitable format for the image_tag helper: (100x100)
|
331
|
+
def image_size
|
332
|
+
[width.to_s, height.to_s] * 'x'
|
333
|
+
end
|
334
|
+
|
335
|
+
# Returns true if the attachment data will be written to the storage system on the next save
|
336
|
+
def save_attachment?
|
337
|
+
File.file?(temp_path.class == String ? temp_path : temp_path.to_filename)
|
338
|
+
end
|
339
|
+
|
340
|
+
# nil placeholder in case this field is used in a form.
|
341
|
+
def uploaded_data() nil; end
|
342
|
+
|
343
|
+
# This method handles the uploaded file object. If you set the field name to uploaded_data, you don't need
|
344
|
+
# any special code in your controller.
|
345
|
+
#
|
346
|
+
# <% form_for :attachment, :html => { :multipart => true } do |f| -%>
|
347
|
+
# <p><%= f.file_field :uploaded_data %></p>
|
348
|
+
# <p><%= submit_tag :Save %>
|
349
|
+
# <% end -%>
|
350
|
+
#
|
351
|
+
# @attachment = Attachment.create! params[:attachment]
|
352
|
+
#
|
353
|
+
# TODO: Allow it to work with Merb tempfiles too.
|
354
|
+
def uploaded_data=(file_data)
|
355
|
+
if file_data.respond_to?(:content_type)
|
356
|
+
return nil if file_data.size == 0
|
357
|
+
self.content_type = file_data.content_type
|
358
|
+
self.filename = file_data.original_filename if respond_to?(:filename)
|
359
|
+
else
|
360
|
+
return nil if file_data.blank? || file_data['size'] == 0
|
361
|
+
self.content_type = file_data['content_type']
|
362
|
+
self.filename = file_data['filename']
|
363
|
+
file_data = file_data['tempfile']
|
364
|
+
end
|
365
|
+
if file_data.is_a?(StringIO)
|
366
|
+
file_data.rewind
|
367
|
+
set_temp_data file_data.read
|
368
|
+
else
|
369
|
+
file_data.respond_to?(:tempfile) ? self.temp_paths.unshift( file_data.tempfile.path ) : self.temp_paths.unshift( file_data.path )
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
# Gets the latest temp path from the collection of temp paths. While working with an attachment,
|
374
|
+
# multiple Tempfile objects may be created for various processing purposes (resizing, for example).
|
375
|
+
# An array of all the tempfile objects is stored so that the Tempfile instance is held on to until
|
376
|
+
# it's not needed anymore. The collection is cleared after saving the attachment.
|
377
|
+
def temp_path
|
378
|
+
p = temp_paths.first
|
379
|
+
p.respond_to?(:path) ? p.path : p.to_s
|
380
|
+
end
|
381
|
+
|
382
|
+
# Gets an array of the currently used temp paths. Defaults to a copy of #full_filename.
|
383
|
+
def temp_paths
|
384
|
+
@temp_paths ||= (new_record? || !respond_to?(:full_filename) || !File.exist?(full_filename) ?
|
385
|
+
[] : [copy_to_temp_file(full_filename)])
|
386
|
+
end
|
387
|
+
|
388
|
+
# Gets the data from the latest temp file. This will read the file into memory.
|
389
|
+
def temp_data
|
390
|
+
save_attachment? ? File.read(temp_path) : nil
|
391
|
+
end
|
392
|
+
|
393
|
+
# Writes the given data to a Tempfile and adds it to the collection of temp files.
|
394
|
+
def set_temp_data(data)
|
395
|
+
temp_paths.unshift write_to_temp_file data unless data.nil?
|
396
|
+
end
|
397
|
+
|
398
|
+
# Copies the given file to a randomly named Tempfile.
|
399
|
+
def copy_to_temp_file(file)
|
400
|
+
self.class.copy_to_temp_file file, random_tempfile_filename
|
401
|
+
end
|
402
|
+
|
403
|
+
# Writes the given file to a randomly named Tempfile.
|
404
|
+
def write_to_temp_file(data)
|
405
|
+
self.class.write_to_temp_file data, random_tempfile_filename
|
406
|
+
end
|
407
|
+
|
408
|
+
# Stub for creating a temp file from the attachment data. This should be defined in the backend module.
|
409
|
+
def create_temp_file() end
|
410
|
+
|
411
|
+
# Allows you to work with a processed representation (RMagick, ImageScience, etc) of the attachment in a block.
|
412
|
+
#
|
413
|
+
# @attachment.with_image do |img|
|
414
|
+
# self.data = img.thumbnail(100, 100).to_blob
|
415
|
+
# end
|
416
|
+
#
|
417
|
+
def with_image(&block)
|
418
|
+
self.class.with_image(temp_path, &block)
|
419
|
+
end
|
420
|
+
|
421
|
+
protected
|
422
|
+
# Generates a unique filename for a Tempfile.
|
423
|
+
def random_tempfile_filename
|
424
|
+
"#{rand Time.now.to_i}#{filename || 'attachment'}"
|
425
|
+
end
|
426
|
+
|
427
|
+
def sanitize_filename(filename)
|
428
|
+
return unless filename
|
429
|
+
filename.strip.tap do |name|
|
430
|
+
# NOTE: File.basename doesn't work right with Windows paths on Unix
|
431
|
+
# get only the filename, not the whole path
|
432
|
+
name.gsub! /^.*(\\|\/)/, ''
|
433
|
+
|
434
|
+
# Finally, replace all non alphanumeric, underscore or periods with underscore
|
435
|
+
name.gsub! /[^A-Za-z0-9\.\-]/, '_'
|
436
|
+
end
|
437
|
+
end
|
438
|
+
|
439
|
+
# before_validation callback.
|
440
|
+
def set_size_from_temp_path
|
441
|
+
self.size = File.size(temp_path) if save_attachment?
|
442
|
+
end
|
443
|
+
|
444
|
+
# validates the size and content_type attributes according to the current model's options
|
445
|
+
def attachment_attributes_valid?
|
446
|
+
[:size, :content_type].each do |attr_name|
|
447
|
+
enum = attachment_options[attr_name]
|
448
|
+
if Object.const_defined?(:I18n) # Rails >= 2.2
|
449
|
+
errors.add attr_name, I18n.translate("activerecord.errors.messages.inclusion", attr_name => enum) unless enum.nil? || enum.include?(send(attr_name))
|
450
|
+
else
|
451
|
+
errors.add attr_name, ActiveRecord::Errors.default_error_messages[:inclusion] unless enum.nil? || enum.include?(send(attr_name))
|
452
|
+
end
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
# Initializes a new thumbnail with the given suffix.
|
457
|
+
def find_or_initialize_thumbnail(file_name_suffix)
|
458
|
+
respond_to?(:parent_id) ?
|
459
|
+
thumbnail_class.find_or_initialize_by_thumbnail_and_parent_id(file_name_suffix.to_s, id) :
|
460
|
+
thumbnail_class.find_or_initialize_by_thumbnail(file_name_suffix.to_s)
|
461
|
+
end
|
462
|
+
|
463
|
+
# Stub for a #process_attachment method in a processor
|
464
|
+
def process_attachment
|
465
|
+
@saved_attachment = save_attachment?
|
466
|
+
end
|
467
|
+
|
468
|
+
# Cleans up after processing. Thumbnails are created, the attachment is stored to the backend, and the temp_paths are cleared.
|
469
|
+
def after_process_attachment
|
470
|
+
if @saved_attachment
|
471
|
+
if respond_to?(:process_attachment_with_processing) && thumbnailable? && !attachment_options[:thumbnails].blank? && parent_id.nil?
|
472
|
+
temp_file = temp_path || create_temp_file
|
473
|
+
attachment_options[:thumbnails].each { |suffix, size| create_or_update_thumbnail(temp_file, suffix, *size) }
|
474
|
+
end
|
475
|
+
save_to_storage
|
476
|
+
@temp_paths.clear
|
477
|
+
@saved_attachment = nil
|
478
|
+
#callback :after_attachment_saved
|
479
|
+
callback_with_args :after_attachment_saved, nil
|
480
|
+
end
|
481
|
+
end
|
482
|
+
|
483
|
+
# Resizes the given processed img object with either the attachment resize options or the thumbnail resize options.
|
484
|
+
def resize_image_or_thumbnail!(img)
|
485
|
+
if (!respond_to?(:parent_id) || parent_id.nil?) && attachment_options[:resize_to] # parent image
|
486
|
+
resize_image(img, attachment_options[:resize_to])
|
487
|
+
elsif thumbnail_resize_options # thumbnail
|
488
|
+
resize_image(img, thumbnail_resize_options)
|
489
|
+
end
|
490
|
+
end
|
491
|
+
|
492
|
+
if defined?(Rails) && Rails::VERSION::MAJOR >= 3
|
493
|
+
def callback_with_args(method, arg = self)
|
494
|
+
if respond_to?(method)
|
495
|
+
send(method, arg)
|
496
|
+
end
|
497
|
+
end
|
498
|
+
# Yanked from ActiveRecord::Callbacks, modified so I can pass args to the callbacks besides self.
|
499
|
+
# Only accept blocks, however
|
500
|
+
elsif ActiveSupport.const_defined?(:Callbacks)
|
501
|
+
# Rails 2.1 and beyond!
|
502
|
+
def callback_with_args(method, arg = self)
|
503
|
+
notify(method)
|
504
|
+
|
505
|
+
result = run_callbacks(method, { :object => arg }) { |result, object| result == false }
|
506
|
+
|
507
|
+
if result != false && respond_to_without_attributes?(method)
|
508
|
+
result = send(method)
|
509
|
+
end
|
510
|
+
|
511
|
+
result
|
512
|
+
end
|
513
|
+
|
514
|
+
def run_callbacks(kind, options = {}, &block)
|
515
|
+
options.reverse_merge!( :object => self )
|
516
|
+
self.class.send("#{kind}_callback_chain").run(options[:object], options, &block)
|
517
|
+
end
|
518
|
+
else
|
519
|
+
# Rails 2.0
|
520
|
+
def callback_with_args(method, arg = self)
|
521
|
+
notify(method)
|
522
|
+
|
523
|
+
result = nil
|
524
|
+
callbacks_for(method).each do |callback|
|
525
|
+
result = callback.call(self, arg)
|
526
|
+
return false if result == false
|
527
|
+
end
|
528
|
+
result
|
529
|
+
end
|
530
|
+
end
|
531
|
+
|
532
|
+
# Removes the thumbnails for the attachment, if it has any
|
533
|
+
def destroy_thumbnails
|
534
|
+
self.thumbnails.each { |thumbnail| thumbnail.destroy } if thumbnailable?
|
535
|
+
end
|
536
|
+
end
|
537
|
+
end
|
538
|
+
end
|