ss-attachment_fu 3.2.17

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,99 @@
1
+ * Jan 27, 2015 *
2
+ * Pull in changes from bacrossland to fix test suite *
3
+
4
+ * Sep 23, 2014 *
5
+ * Pull in changes from corlissc to Refactor DbFileBackend for Rails 4 AREL conformity *
6
+
7
+ * May 13, 2014 *
8
+ * Pull in changes from bacrossland to fix thumbnails for S3 storage
9
+
10
+ * Apr 17, 2014 *
11
+ * Pull in changes from jasonbuehler to fix Rails 4 errors
12
+
13
+ * Jan 7, 2014 *
14
+ * Pull in changes from twelvelabs to fix gem use in Rails 2
15
+
16
+ * Aug 22, 2013 *
17
+ * Added cache-control header option and Ruby 1.9 fix for S3 from tricycle
18
+
19
+ * Aug 21, 2013 *
20
+ * Added S3 :encrypted_storage option support from cschulte22
21
+
22
+ * Jul 5, 2013
23
+ * Pull in changes from lchimonji10 to reformat README to RDoc format
24
+
25
+ * Apr 10, 2013 *
26
+ * Ruby 2 compatibility fix
27
+ * Removed some lingering occurrences of RAILS_ROOT and RAILS_ENV
28
+ * clean up of gemspec file
29
+ * no longer package unnecessary files like the test files
30
+
31
+ * Mar 12, 2013 *
32
+ * Pull in changes from https://github.com/aalong/attachment_fu to use Ruby 1.9.2 Tempfile naming strategy to fix conflicts with Sprockets (see https://github.com/aalong/attachment_fu/commit/938ec3b8597fbf82b1de8c98de12c4688463450a)
33
+
34
+ * Feb 19, 2013 *
35
+ * Pull in changes from https://github.com/itkin/attachment_fu fork to fix mini magick
36
+
37
+ * Nov 15, 2012 *
38
+ * Removed 'crop:' option as already included when prefixing geometry with a 'c'. Add note to docs.
39
+ * Added option to sharpen resized images ':sharpen_on_resize'
40
+
41
+ * Oct 26, 2012 *
42
+ * Merged with https://github.com/tdd/attachment_fu fork to include GEM support for use in Rails 3.2, and bumped version to 3.2.x to indicate Rails 3.2 support.
43
+
44
+ * Oct 25, 2012 *
45
+ * Fix to support Ruby 1.9.3
46
+ * Image cropping support (append 'crop:' to desired file size when resizing)
47
+
48
+ * Mar 15, 2010 *
49
+ * Added a symbol syntax for parent-type-based size spec: calls the corresponding method on the current asset instance to
50
+ get a Hash of required thumbnails. Lets us dynamically specify what would otherwise be hard-coded, which is useful
51
+ when the set of thumbnails for a given parent type varies depending on the usage context.
52
+
53
+ * Aug 6, 2009 *
54
+ * JPEG quality control finalized across processors (although CoreImage applies it in a rather fuzzy-logic way), with tests.
55
+
56
+ * Aug 4, 2009 *
57
+ * Supports the :aspect/'!' geometry flag in all processors
58
+ (thanks to http://www.deepcalm.com/writing/cropped-thumbnails-in-attachment_fu-using-imagescience)
59
+ * JPEG quality control on thumbnailing/resizing (still buggy on Rmagick/MiniMagick though).
60
+ * Moves from GIF to PNG regardless of the source file extension's case (used to require lowercase)
61
+ * Auto-orients image (if EXIF suggests it) prior to processing with RMagickProcessor
62
+ * Fixes non-image upload tests (both regular files and Merb files)
63
+ * Fixes obsolete failures on RMagick tests (aspect_ratio tested but not initialized anymore due to new callback architecture
64
+
65
+ * Apr 17 2008 *
66
+ * amazon_s3.yml is now passed through ERB before being passed to AWS::S3 [François Beausoleil]
67
+
68
+ * Mar 22 2008 *
69
+ * Some tweaks to support Rails 2.0 and Rails 2.1 due to ActiveSupport::Callback changes.
70
+ Thanks to http://blog.methodmissing.com/2008/1/19/edge-callback-refactorings-attachment_fu/
71
+
72
+ * Feb. 26, 2008 *
73
+ * remove breakpoint from test_helper, makes test suite crazy (at least Rails 2+) [Rob Sanheim]
74
+ * make S3 test really optional [Rob Sanheim]
75
+
76
+ * Nov 27, 2007 *
77
+ * Handle properly ImageScience thumbnails resized from a gif file [Matt Aimonetti]
78
+ * Save thumbnails file size properly when using ImageScience [Matt Aimonetti]
79
+ * fixed s3 config file loading with latest versions of Rails [Matt Aimonetti]
80
+
81
+ * April 2, 2007 *
82
+
83
+ * don't copy the #full_filename to the default #temp_paths array if it doesn't exist
84
+ * add default ID partitioning for attachments
85
+ * add #binmode call to Tempfile (note: ruby should be doing this!) [Eric Beland]
86
+ * Check for current type of :thumbnails option.
87
+ * allow customization of the S3 configuration file path with the :s3_config_path option.
88
+ * Don't try to remove thumbnails if there aren't any. Closes #3 [ben stiglitz]
89
+
90
+ * BC * (before changelog)
91
+
92
+ * add default #temp_paths entry [mattly]
93
+ * add MiniMagick support to attachment_fu [Isacc]
94
+ * update #destroy_file to clear out any empty directories too [carlivar]
95
+ * fix references to S3Backend module [Hunter Hillegas]
96
+ * make #current_data public with db_file and s3 backends [ebryn]
97
+ * oops, actually svn add the files for s3 backend. [Jeffrey Hardy]
98
+ * experimental s3 support, egad, no tests.... [Jeffrey Hardy]
99
+ * doh, fix a few bad references to ActsAsAttachment [sixty4bit]
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 rick olson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,358 @@
1
+ = attachment-fu
2
+
3
+ {<img src="https://badge.fury.io/rb/pothoven-attachment_fu.svg" alt="Gem Version" />}[http://badge.fury.io/rb/pothoven-attachment_fu]
4
+
5
+ attachment_fu is a plugin by Rick Olson (aka technoweenie
6
+ http://techno-weenie.net) and is the successor to acts_as_attachment. To get a
7
+ basic run-through of its capabilities, check out {Mike Clark's
8
+ tutorial}[https://github.com/pothoven/attachment_fu/blob/master/TUTORIAL.rdoc].
9
+
10
+ = patch
11
+ * Directly save to AWS S3 based on Zoo configuration
12
+ * Parameter resize_to can receive a symbol which is can be an instance method of class
13
+
14
+ = attachment_fu functionality
15
+
16
+ attachment_fu facilitates file uploads in Ruby on Rails. There are a few
17
+ storage options for the actual file data, but the plugin always at a minimum
18
+ stores metadata for each file in the database.
19
+
20
+ There are four storage options for files uploaded through attachment_fu:
21
+
22
+ * File system
23
+ * Database file
24
+ * Amazon S3
25
+ * Rackspace (Mosso) Cloud Files
26
+
27
+ Each method of storage many options associated with it that will be covered in
28
+ the following section. Something to note, however, is that the Amazon S3 storage
29
+ requires you to modify +config/amazon_s3.yml+, the Rackspace Cloud Files storage
30
+ requires you to modify +config/rackspace_cloudfiles.yml+, and the Database file
31
+ storage requires an extra table.
32
+
33
+ = attachment_fu models
34
+
35
+ For all three of these storage options a table of metadata is required. This
36
+ table will contain information about the file (hence the 'meta') and its
37
+ location. This table has no restrictions on naming, unlike the extra table
38
+ required for database storage, which must have a table name of +db_files+ (and
39
+ by convention a model of +DbFile+).
40
+
41
+ Two methods are available to models: +has_attachment+ and
42
+ +validates_as_attachment+.
43
+
44
+ == has_attachment(options = {})
45
+
46
+ This method accepts the options in a hash:
47
+
48
+ [:content_type]
49
+ Allowed content types.
50
+
51
+ By default, all content types are allowed. Use +:image+ to allow all
52
+ standard image types.
53
+
54
+ [:min_size]
55
+ Minimum file size.
56
+
57
+ By default, set to +1.byte+.
58
+
59
+ [:max_size]
60
+ Maximum file size.
61
+
62
+ By default, set to +1.megabyte+.
63
+
64
+ [:size]
65
+ Minimum and maximum file size.
66
+
67
+ By default, set to +1..1.megabyte+. Overrides +:min_size+ and
68
+ +:max_size+.
69
+
70
+ [:resize_to]
71
+ Used by RMagick.
72
+
73
+ Tells RMagick how to resize images. Pass either an array specifying
74
+ width and height or a geometry string. Prefixing the geometry string
75
+ with a 'c' will crop the image to the specified size.
76
+
77
+ [:sharpen_on_resize]
78
+ Used by RMagick.
79
+
80
+ If set to true, images are sharpened after being resized.
81
+
82
+ [:thumbnails]
83
+ A set of thumbnails to generate.
84
+
85
+ This accepts a hash of filename suffixes and RMagick resizing options. This
86
+ option need only be included if you want thumbnailing.
87
+
88
+ If you have a polymorphic parent relationship, you can provide
89
+ parent-type-specific thumbnail settings by using a pair with the type string
90
+ as key and a Hash of thumbnail definitions, or a method symbol, as value.
91
+ The method symbol will call the named method in order to get a
92
+ dynamically-built Hash of thumbnail definitions, which gives you full
93
+ flexibility. AttachmentFu automatically detects your first polymorphic
94
+ +belongs_to+ relationship.
95
+
96
+ [:thumbnail_class]
97
+ Which model class to use for thumbnails.
98
+
99
+ By default, the current attachment class is used.
100
+
101
+ [:jpeg_quality]
102
+ JPEG quality settings for thumbnail resizes.
103
+
104
+ Arguments can be in multiple formats:
105
+
106
+ * Integer from 0 (basically crap) to 100 (basically lossless, fat files).
107
+
108
+ * When relying on tdd-image_science, you can also use one of its +JPEG_xxx+
109
+ constants for predefined ratios/settings.
110
+
111
+ * You can also use a Hash, with keys being either thumbnail symbols (I
112
+ repeat: _symbols_) or surface boundaries. A surface boundary is a string
113
+ starting with either '<' or '>=', followed by a number of pixels. This
114
+ lets you specify per-thumbnail or per-general-thumbnail-"size" JPEG
115
+ qualities. (which can be useful when you have a _lot_ of thumbnail
116
+ options). Surface example: <code>{'<2000' => 90, '>=2000' => 75}</code>.
117
+
118
+ Defaults vary depending on the processor (ImageScience: 100%,
119
+ Rmagick/MiniMagick/Gd2: 75%, CoreImage: auto-adjust). Note that only
120
+ tdd-image_science (available from GitHub) currently supports explicit JPEG
121
+ quality; the default image_science currently forces 100%.
122
+
123
+ [:path_prefix]
124
+ Path to store the uploaded files in. Uses <code>public/#{table_name}</code>
125
+ by default for the filesystem, and just <code>#{table_name}</code> for the
126
+ S3 and Cloud Files backend. Setting this sets the +:storage+ to
127
+ +:file_system+.
128
+
129
+ [:partition]
130
+ Whether to partiton files in directories like +/0000/0001/image.jpg+.
131
+ Default is true. Only applicable to the +:file_system+ backend.
132
+
133
+ [:storage]
134
+ Specifies the storage system to use. Defaults to +:db_file+. Options are
135
+ +:file_system+, +:db_file+, +:s3+, and +:cloud_files+.
136
+
137
+ [:cloudfront]
138
+ If using S3 for storage, this option allows for serving the files via Amazon
139
+ CloudFront. Defaults to false.
140
+
141
+ [:processor]
142
+ Sets the image processor to use for resizing of the attached image. Options
143
+ include ImageScience, Rmagick, MiniMagick, Gd2 and CoreImage. Default is
144
+ whatever is installed.
145
+
146
+ [:uuid_primary_key]
147
+ If your model's primary key is a 128-bit UUID in hexadecimal format, then
148
+ set this to true.
149
+
150
+ [:association_options]
151
+ attachment_fu automatically defines associations with thumbnails with
152
+ +has_many+ and +belongs_to+. If there are any additional options that you
153
+ want to pass to these methods, then specify them here.
154
+
155
+ Examples:
156
+
157
+ has_attachment(content_type: 'application/pdf')
158
+ has_attachment(
159
+ content_type: ['application/pdf', 'application/msword', 'text/plain']
160
+ )
161
+ has_attachment(content_type: ['application/pdf', :image], resize_to: 'x50')
162
+ has_attachment(content_type: :image, resize_to: [50,50])
163
+ has_attachment(max_size: 1.kilobyte)
164
+ has_attachment(size: 1.megabyte..2.megabytes)
165
+ has_attachment(storage: :cloud_files)
166
+ has_attachment(storage: :file_system, path_prefix: 'public/files')
167
+ has_attachment(
168
+ storage: :file_system,
169
+ path_prefix: 'public/files',
170
+ content_type: :image,
171
+ resize_to: [50, 50],
172
+ partition: false
173
+ )
174
+ has_attachment(
175
+ storage: :file_system,
176
+ path_prefix: 'public/files',
177
+ thumbnails: {thumb: [50, 50], geometry: 'x50'}
178
+ )
179
+ has_attachment(storage: :s3)
180
+ has_attachment(store: :s3, cloudfront: true)
181
+ has_attachment(thumbnails: {thumb: [50, 50], geometry: 'x50'})
182
+
183
+ # Let's say we have a polymorphic belongs_to, e.g. called 'imageable', where
184
+ # imageable_type (or whatever the :foreign_type option was set to) can be,
185
+ # among other things, 'Product', 'User' or 'Editorial', each of which should
186
+ # have extra thumbnails:
187
+
188
+ has_attachment(thumbnails: {
189
+ editorials: {fullsize: '150x100>'},
190
+ geometry: 'x50',
191
+ products: {large_thumb: '169x169!', zoomed: '500x500>'},
192
+ thumb: [50, 50],
193
+ users: {avatar: '64x64!'}
194
+ })
195
+
196
+ # JPEG qualities…
197
+
198
+ has_attachment(jpeg_quality: 75)
199
+ has_attachment(jpeg_quality: 80 | ImageScience::JPEG_PROGRESSIVE)
200
+ has_attachment(
201
+ thumbnails: {thumb: [50, 50], geometry: 'x50'},
202
+ jpeg_quality: {'<2000' => 90, '>=2000' => 75}
203
+ )
204
+ has_attachment(
205
+ thumbnails: {thumb: [50, 50], geometry: 'x50'},
206
+ jpeg_quality: {nil => 75, thumb: 90, geometry: 90}
207
+ )
208
+
209
+ == validates_as_attachment
210
+
211
+ This method prevents files outside of the valid range (+:min_size+ to
212
+ +:max_size+, or the +:size+ range) from being saved. It does not however, halt
213
+ the upload of such files. They will be uploaded into memory regardless of size
214
+ before validation.
215
+
216
+ To perform this validation, simply add +validates_as_attachment+ to your model.
217
+
218
+ = attachment_fu migrations
219
+
220
+ Fields for attachment_fu metadata tables…
221
+
222
+ In general:
223
+
224
+ size, :integer # file size in bytes
225
+ content_type, :string # mime type, ex: application/mp3
226
+ filename, :string # sanitized filename
227
+
228
+ That reference images:
229
+
230
+ height, :integer # in pixels
231
+ width, :integer # in pixels
232
+
233
+ That reference images that will be thumbnailed:
234
+
235
+ parent_id, :integer # id of parent image (on the same table, a
236
+ # self-referencing foreign-key). Only populated if
237
+ # the current object is a thumbnail.
238
+ thumbnail, :string # The type of thumbnail this attachment record
239
+ # describes. Only populated if the current object is
240
+ # a thumbnail. Example:
241
+ #
242
+ # (In Model 'Avatar')
243
+ # has_attachment(
244
+ # :content_type => :image,
245
+ # :storage => :file_system,
246
+ # :max_size => 500.kilobytes,
247
+ # :resize_to => '320x200>',
248
+ # :thumbnails => {
249
+ # :small => '10x10>',
250
+ # :thumb => '100x100>'
251
+ # }
252
+ # )
253
+ #
254
+ # (Elsewhere)
255
+ # @user.avatar.thumbnails.first.thumbnail # => 'small'
256
+ #
257
+ db_file_id, :integer # ID of the file in the database (foreign key) that
258
+ # reference files stored in the database (:db_file).
259
+
260
+ Field for attachment_fu +db_files+ table:
261
+
262
+ data, :binary # binary file data, for use in database file storage
263
+
264
+ = attachment_fu views
265
+
266
+ There are two main views tasks that will be directly affected by attachment_fu:
267
+ upload forms and displaying uploaded images.
268
+
269
+ There are two parts of the upload form that differ from typical usage.
270
+
271
+ 1. Include <code>multipart: true</code> in the html options of the +form_for+
272
+ tag. Example:
273
+
274
+ <%=
275
+ form_for(
276
+ :attachment_metadata,
277
+ url: {action: "create"},
278
+ html: {multipart: true}
279
+ ) do |form|
280
+ %>
281
+
282
+ 2. Use the +file_field+ helper with +:uploaded_data+ as the field name. Example:
283
+
284
+ <%= form.file_field(:uploaded_data) %>
285
+
286
+ Displaying uploaded images is made easy by the +public_filename+ method of the
287
+ ActiveRecord attachment objects using file system, s3, and Cloud Files storage.
288
+
289
+ == public_filename(thumbnail = nil)
290
+
291
+ Returns the public path to the file. If a thumbnail prefix is specified it will
292
+ return the public file path to the corresponding thumbnail. Examples:
293
+
294
+ attachment_obj.public_filename #=> /attachments/2/file.jpg
295
+ attachment_obj.public_filename(:thumb) #=> /attachments/2/file_thumb.jpg
296
+ attachment_obj.public_filename(:small) #=> /attachments/2/file_small.jpg
297
+
298
+ When serving files from database storage, doing more than simply downloading the
299
+ file is beyond the scope of this document.
300
+
301
+ = attachment_fu controllers
302
+
303
+ There are two considerations to take into account when using attachment_fu in
304
+ controllers.
305
+
306
+ The first is when the files have no publicly accessible path and need to be
307
+ downloaded through an action. Example:
308
+
309
+ def readme
310
+ send_file(
311
+ '/path/to/readme.txt',
312
+ type: 'plain/text',
313
+ disposition: 'inline'
314
+ )
315
+ end
316
+
317
+ See the possible values for +send_file+ for reference.
318
+
319
+ The second is when saving the file when submitted from a form. Example:
320
+
321
+ In a view:
322
+
323
+ <%= form.file_field(:attachable, :uploaded_data) %>
324
+
325
+ In a controller:
326
+
327
+ def create
328
+ @attachable_file = AttachmentMetadataModel.new(params[:attachable])
329
+ if @attachable_file.save
330
+ flash[:notice] = 'Attachment was successfully created.'
331
+ redirect_to(attachable_url(@attachable_file))
332
+ else
333
+ redirect_to(action: 'new')
334
+ end
335
+ end
336
+
337
+ = attachment_fu scripting
338
+
339
+ You may wish to import a large number of images or attachments. The following
340
+ example shows how to upload a file from a script.
341
+
342
+ #!/usr/bin/env ./script/runner
343
+
344
+ # required to use ActionController::TestUploadedFile
345
+ require 'action_controller'
346
+ require 'action_controller/test_process.rb'
347
+
348
+ path = "./public/images/x.jpg"
349
+
350
+ # `mimetype` is a string like "image/jpeg". One way to get the mimetype for
351
+ # a given file on a UNIX system: mimetype = `file -ib #{path}`.gsub(/\n/,"")
352
+ mimetype = "image/jpeg"
353
+
354
+ # This will "upload" the file at path and create the new model.
355
+ @attachable = AttachmentMetadataModel.new(
356
+ uploaded_data: ActionController::TestUploadedFile.new(path, mimetype)
357
+ )
358
+ @attachable.save
data/amazon_s3.yml.tpl ADDED
@@ -0,0 +1,17 @@
1
+ development:
2
+ bucket_name: appname_development
3
+ access_key_id:
4
+ secret_access_key:
5
+ distribution_domain: XXXX.cloudfront.net
6
+
7
+ test:
8
+ bucket_name: appname_test
9
+ access_key_id:
10
+ secret_access_key:
11
+ distribution_domain: XXXX.cloudfront.net
12
+
13
+ production:
14
+ bucket_name: appname
15
+ access_key_id:
16
+ secret_access_key:
17
+ distribution_domain: XXXX.cloudfront.net
data/lib/geometry.rb ADDED
@@ -0,0 +1,96 @@
1
+ # This Geometry class was yanked from RMagick. However, it lets ImageMagick handle the actual change_geometry.
2
+ # Use #new_dimensions_for to get new dimensions
3
+ # Used so I can use spiffy RMagick geometry strings with ImageScience
4
+ class Geometry
5
+ # @ is removed until support for them is added
6
+ FLAGS = ['', '%', '<', '>', '!']#, '@']
7
+ RFLAGS = { '%' => :percent,
8
+ '!' => :aspect,
9
+ '<' => :>,
10
+ '>' => :<,
11
+ '@' => :area }
12
+
13
+ attr_accessor :width, :height, :x, :y, :flag
14
+
15
+ def initialize(width=nil, height=nil, x=nil, y=nil, flag=nil)
16
+ # Support floating-point width and height arguments so Geometry
17
+ # objects can be used to specify Image#density= arguments.
18
+ raise ArgumentError, "width must be >= 0: #{width}" if width < 0
19
+ raise ArgumentError, "height must be >= 0: #{height}" if height < 0
20
+ @width = width.to_f
21
+ @height = height.to_f
22
+ @x = x.to_i
23
+ @y = y.to_i
24
+ @flag = flag
25
+ end
26
+
27
+ # Construct an object from a geometry string
28
+ RE = /\A(\d*)(?:x(\d+)?)?([-+]\d+)?([-+]\d+)?([%!<>@]?)\Z/
29
+
30
+ def self.from_s(str)
31
+ raise(ArgumentError, "no geometry string specified") unless str
32
+
33
+ if m = RE.match(str)
34
+ new(m[1].to_i, m[2].to_i, m[3].to_i, m[4].to_i, RFLAGS[m[5]])
35
+ else
36
+ raise ArgumentError, "invalid geometry format"
37
+ end
38
+ end
39
+
40
+ # Convert object to a geometry string
41
+ def to_s
42
+ str = ''
43
+ str << "%g" % @width if @width > 0
44
+ str << 'x' if (@width > 0 || @height > 0)
45
+ str << "%g" % @height if @height > 0
46
+ str << "%+d%+d" % [@x, @y] if (@x != 0 || @y != 0)
47
+ str << FLAGS[@flag.to_i]
48
+ end
49
+
50
+ # attempts to get new dimensions for the current geometry string given these old dimensions.
51
+ # This doesn't implement the aspect flag (!) or the area flag (@). PDI
52
+ def new_dimensions_for(orig_width, orig_height)
53
+ new_width = orig_width
54
+ new_height = orig_height
55
+
56
+ case @flag
57
+ when :percent
58
+ scale_x = @width.zero? ? 100 : @width
59
+ scale_y = @height.zero? ? @width : @height
60
+ new_width = scale_x.to_f * (orig_width.to_f / 100.0)
61
+ new_height = scale_y.to_f * (orig_height.to_f / 100.0)
62
+ when :aspect
63
+ new_width = @width unless @width.nil?
64
+ new_height = @height unless @height.nil?
65
+ when :<, :>, nil
66
+ scale_factor =
67
+ if new_width.zero? || new_height.zero?
68
+ 1.0
69
+ else
70
+ if @width.nonzero? && @height.nonzero?
71
+ [@width.to_f / new_width.to_f, @height.to_f / new_height.to_f].min
72
+ else
73
+ @width.nonzero? ? (@width.to_f / new_width.to_f) : (@height.to_f / new_height.to_f)
74
+ end
75
+ end
76
+ new_width = scale_factor * new_width.to_f
77
+ new_height = scale_factor * new_height.to_f
78
+ new_width = orig_width if @flag && orig_width.send(@flag, new_width)
79
+ new_height = orig_height if @flag && orig_height.send(@flag, new_height)
80
+ end
81
+
82
+ [new_width, new_height].collect! { |v| [v.round, 1].max }
83
+ end
84
+ end
85
+
86
+ class Array
87
+ # allows you to get new dimensions for the current array of dimensions with a given geometry string
88
+ #
89
+ # [50, 64] / '40>' # => [40, 51]
90
+ def /(geometry)
91
+ raise ArgumentError, "Only works with a [width, height] pair" if size != 2
92
+ raise ArgumentError, "Must pass a valid geometry string or object" unless geometry.is_a?(String) || geometry.is_a?(Geometry)
93
+ geometry = Geometry.from_s(geometry) if geometry.is_a?(String)
94
+ geometry.new_dimensions_for first, last
95
+ end
96
+ end
@@ -0,0 +1,27 @@
1
+
2
+ if defined?(Rails::Engine)
3
+ # Rails >= 3
4
+ class Engine < Rails::Engine
5
+ # Mimic old vendored plugin behavior, attachment_fu/lib is autoloaded.
6
+ config.autoload_paths << File.expand_path("..", __FILE__)
7
+
8
+ initializer "attachment_fu" do
9
+ require 'geometry'
10
+
11
+ ActiveRecord::Base.send(:extend, Technoweenie::AttachmentFu::ActMethods)
12
+ Technoweenie::AttachmentFu.tempfile_path = ATTACHMENT_FU_TEMPFILE_PATH if Object.const_defined?(:ATTACHMENT_FU_TEMPFILE_PATH)
13
+ FileUtils.mkdir_p Technoweenie::AttachmentFu.tempfile_path
14
+ end
15
+ end
16
+ else
17
+ # Rails <= 2
18
+ require 'geometry'
19
+
20
+ ActiveSupport::Dependencies.autoload_paths << File.expand_path("..", __FILE__)
21
+
22
+ ActiveRecord::Base.send(:extend, Technoweenie::AttachmentFu::ActMethods)
23
+ Technoweenie::AttachmentFu.tempfile_path = ATTACHMENT_FU_TEMPFILE_PATH if Object.const_defined?(:ATTACHMENT_FU_TEMPFILE_PATH)
24
+ FileUtils.mkdir_p Technoweenie::AttachmentFu.tempfile_path
25
+
26
+ $:.unshift(File.expand_path("../vendor", __FILE__))
27
+ end