samlown-carrierwave 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. data/Generators +4 -0
  2. data/History.txt +125 -0
  3. data/Manifest.txt +110 -0
  4. data/README.rdoc +524 -0
  5. data/Rakefile +39 -0
  6. data/carrierwave.gemspec +85 -0
  7. data/cucumber.yml +2 -0
  8. data/features/caching.feature +28 -0
  9. data/features/download.feature +20 -0
  10. data/features/file_storage.feature +37 -0
  11. data/features/file_storage_overridden_filename.feature +38 -0
  12. data/features/file_storage_overridden_store_dir.feature +38 -0
  13. data/features/file_storage_reversing_processor.feature +43 -0
  14. data/features/fixtures/bork.txt +1 -0
  15. data/features/fixtures/monkey.txt +1 -0
  16. data/features/grid_fs_storage.feature +32 -0
  17. data/features/mount_activerecord.feature +46 -0
  18. data/features/mount_datamapper.feature +46 -0
  19. data/features/step_definitions/activerecord_steps.rb +22 -0
  20. data/features/step_definitions/caching_steps.rb +14 -0
  21. data/features/step_definitions/datamapper_steps.rb +29 -0
  22. data/features/step_definitions/download_steps.rb +4 -0
  23. data/features/step_definitions/file_steps.rb +53 -0
  24. data/features/step_definitions/general_steps.rb +85 -0
  25. data/features/step_definitions/mount_steps.rb +19 -0
  26. data/features/step_definitions/store_steps.rb +18 -0
  27. data/features/support/activerecord.rb +30 -0
  28. data/features/support/datamapper.rb +7 -0
  29. data/features/support/env.rb +22 -0
  30. data/features/versions_basics.feature +50 -0
  31. data/features/versions_nested_versions.feature +70 -0
  32. data/features/versions_overridden_filename.feature +51 -0
  33. data/features/versions_overriden_store_dir.feature +41 -0
  34. data/lib/carrierwave.rb +98 -0
  35. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  36. data/lib/carrierwave/core_ext/blank.rb +46 -0
  37. data/lib/carrierwave/core_ext/file.rb +11 -0
  38. data/lib/carrierwave/core_ext/inheritable_attributes.rb +108 -0
  39. data/lib/carrierwave/core_ext/module_setup.rb +51 -0
  40. data/lib/carrierwave/mount.rb +359 -0
  41. data/lib/carrierwave/orm/activerecord.rb +73 -0
  42. data/lib/carrierwave/orm/datamapper.rb +27 -0
  43. data/lib/carrierwave/orm/mongoid.rb +23 -0
  44. data/lib/carrierwave/orm/mongomapper.rb +27 -0
  45. data/lib/carrierwave/orm/sequel.rb +45 -0
  46. data/lib/carrierwave/processing/image_science.rb +101 -0
  47. data/lib/carrierwave/processing/mini_magick.rb +265 -0
  48. data/lib/carrierwave/processing/rmagick.rb +282 -0
  49. data/lib/carrierwave/sanitized_file.rb +273 -0
  50. data/lib/carrierwave/storage/abstract.rb +30 -0
  51. data/lib/carrierwave/storage/cloud_files.rb +169 -0
  52. data/lib/carrierwave/storage/file.rb +48 -0
  53. data/lib/carrierwave/storage/grid_fs.rb +97 -0
  54. data/lib/carrierwave/storage/right_s3.rb +3 -0
  55. data/lib/carrierwave/storage/s3.rb +206 -0
  56. data/lib/carrierwave/test/matchers.rb +128 -0
  57. data/lib/carrierwave/uploader.rb +44 -0
  58. data/lib/carrierwave/uploader/cache.rb +145 -0
  59. data/lib/carrierwave/uploader/callbacks.rb +42 -0
  60. data/lib/carrierwave/uploader/configuration.rb +132 -0
  61. data/lib/carrierwave/uploader/default_url.rb +19 -0
  62. data/lib/carrierwave/uploader/download.rb +59 -0
  63. data/lib/carrierwave/uploader/extension_whitelist.rb +37 -0
  64. data/lib/carrierwave/uploader/mountable.rb +39 -0
  65. data/lib/carrierwave/uploader/processing.rb +83 -0
  66. data/lib/carrierwave/uploader/proxy.rb +62 -0
  67. data/lib/carrierwave/uploader/remove.rb +22 -0
  68. data/lib/carrierwave/uploader/store.rb +89 -0
  69. data/lib/carrierwave/uploader/url.rb +33 -0
  70. data/lib/carrierwave/uploader/versions.rb +146 -0
  71. data/merb_generators/uploader_generator.rb +22 -0
  72. data/rails_generators/uploader/USAGE +2 -0
  73. data/rails_generators/uploader/templates/uploader.rb +47 -0
  74. data/rails_generators/uploader/uploader_generator.rb +21 -0
  75. data/script/console +10 -0
  76. data/script/destroy +14 -0
  77. data/script/generate +14 -0
  78. data/spec/compatibility/paperclip_spec.rb +52 -0
  79. data/spec/fixtures/bork.txt +1 -0
  80. data/spec/fixtures/landscape.jpg +0 -0
  81. data/spec/fixtures/portrait.jpg +0 -0
  82. data/spec/fixtures/test.jpeg +1 -0
  83. data/spec/fixtures/test.jpg +1 -0
  84. data/spec/mount_spec.rb +538 -0
  85. data/spec/orm/activerecord_spec.rb +271 -0
  86. data/spec/orm/datamapper_spec.rb +168 -0
  87. data/spec/orm/mongoid_spec.rb +202 -0
  88. data/spec/orm/mongomapper_spec.rb +202 -0
  89. data/spec/orm/sequel_spec.rb +183 -0
  90. data/spec/processing/image_science_spec.rb +56 -0
  91. data/spec/processing/mini_magick_spec.rb +76 -0
  92. data/spec/processing/rmagick_spec.rb +75 -0
  93. data/spec/sanitized_file_spec.rb +623 -0
  94. data/spec/spec_helper.rb +92 -0
  95. data/spec/storage/cloudfiles_spec.rb +78 -0
  96. data/spec/storage/grid_fs_spec.rb +83 -0
  97. data/spec/storage/s3_spec.rb +118 -0
  98. data/spec/uploader/cache_spec.rb +209 -0
  99. data/spec/uploader/configuration_spec.rb +105 -0
  100. data/spec/uploader/default_url_spec.rb +85 -0
  101. data/spec/uploader/download_spec.rb +75 -0
  102. data/spec/uploader/extension_whitelist_spec.rb +44 -0
  103. data/spec/uploader/mountable_spec.rb +33 -0
  104. data/spec/uploader/paths_spec.rb +22 -0
  105. data/spec/uploader/processing_spec.rb +73 -0
  106. data/spec/uploader/proxy_spec.rb +54 -0
  107. data/spec/uploader/remove_spec.rb +70 -0
  108. data/spec/uploader/store_spec.rb +264 -0
  109. data/spec/uploader/url_spec.rb +102 -0
  110. data/spec/uploader/versions_spec.rb +298 -0
  111. metadata +433 -0
@@ -0,0 +1,359 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+
5
+ ##
6
+ # If a Class is extended with this module, it gains the mount_uploader
7
+ # method, which is used for mapping attributes to uploaders and allowing
8
+ # easy assignment.
9
+ #
10
+ # You can use mount_uploader with pretty much any class, however it is
11
+ # intended to be used with some kind of persistent storage, like an ORM.
12
+ # If you want to persist the uploaded files in a particular Class, it
13
+ # needs to implement a `read_uploader` and a `write_uploader` method.
14
+ #
15
+ module Mount
16
+
17
+ ##
18
+ # === Returns
19
+ #
20
+ # [Hash{Symbol => CarrierWave}] what uploaders are mounted on which columns
21
+ #
22
+ def uploaders
23
+ @uploaders ||= {}
24
+ @uploaders = superclass.uploaders.merge(@uploaders)
25
+ rescue NoMethodError
26
+ @uploaders
27
+ end
28
+
29
+ def uploader_options
30
+ @uploader_options ||= {}
31
+ @uploader_options = superclass.uploader_options.merge(@uploader_options)
32
+ rescue NoMethodError
33
+ @uploader_options
34
+ end
35
+
36
+ ##
37
+ # Return a particular option for a particular uploader
38
+ #
39
+ # === Parameters
40
+ #
41
+ # [column (Symbol)] The column the uploader is mounted at
42
+ # [option (Symbol)] The option, e.g. validate_integrity
43
+ #
44
+ # === Returns
45
+ #
46
+ # [Object] The option value
47
+ #
48
+ def uploader_option(column, option)
49
+ if uploader_options[column].has_key?(option)
50
+ uploader_options[column][option]
51
+ else
52
+ uploaders[column].send(option)
53
+ end
54
+ end
55
+
56
+ ##
57
+ # Mounts the given uploader on the given column. This means that assigning
58
+ # and reading from the column will upload and retrieve files. Supposing
59
+ # that a User class has an uploader mounted on image, you can assign and
60
+ # retrieve files like this:
61
+ #
62
+ # @user.image # => <Uploader>
63
+ # @user.image = some_file_object
64
+ #
65
+ # @user.store_image!
66
+ #
67
+ # @user.image.url # => '/some_url.png'
68
+ #
69
+ # It is also possible (but not recommended) to ommit the uploader, which
70
+ # will create an anonymous uploader class. Passing a block to this method
71
+ # makes it possible to customize it. This can be convenient for brevity,
72
+ # but if there is any significatnt logic in the uploader, you should do
73
+ # the right thing and have it in its own file.
74
+ #
75
+ # === Added instance methods
76
+ #
77
+ # Supposing a class has used +mount_uploader+ to mount an uploader on a column
78
+ # named +image+, in that case the following methods will be added to the class:
79
+ #
80
+ # [image] Returns an instance of the uploader only if anything has been uploaded
81
+ # [image=] Caches the given file
82
+ #
83
+ # [image_url] Returns the url to the uploaded file
84
+ #
85
+ # [image_cache] Returns a string that identifies the cache location of the file
86
+ # [image_cache=] Retrieves the file from the cache based on the given cache name
87
+ #
88
+ # [remote_image_url] Returns previously cached remote url
89
+ # [remote_image_url=] Retrieve the file from the remote url
90
+ #
91
+ # [remove_image] An attribute reader that can be used with a checkbox to mark a file for removal
92
+ # [remove_image=] An attribute writer that can be used with a checkbox to mark a file for removal
93
+ # [remove_image?] Whether the file should be removed when store_image! is called.
94
+ #
95
+ # [store_image!] Stores a file that has been assigned with +image=+
96
+ # [remove_image!] Removes the uploaded file from the filesystem.
97
+ #
98
+ # [image_integrity_error] Returns an error object if the last file to be assigned caused an integrity error
99
+ # [image_processing_error] Returns an error object if the last file to be assigned caused a processing error
100
+ #
101
+ # [write_image_identifier] Uses the write_uploader method to set the identifier.
102
+ #
103
+ # === Parameters
104
+ #
105
+ # [column (Symbol)] the attribute to mount this uploader on
106
+ # [uploader (CarrierWave::Uploader)] the uploader class to mount
107
+ # [options (Hash{Symbol => Object})] a set of options
108
+ # [&block (Proc)] customize anonymous uploaders
109
+ #
110
+ # === Options
111
+ #
112
+ # [:mount_on => Symbol] if the name of the column to be serialized to differs you can override it using this option
113
+ # [:ignore_integrity_errors => Boolean] if set to true, integrity errors will result in caching failing silently
114
+ # [:ignore_processing_errors => Boolean] if set to true, processing errors will result in caching failing silently
115
+ #
116
+ # === Examples
117
+ #
118
+ # Mounting uploaders on different columns.
119
+ #
120
+ # class Song
121
+ # mount_uploader :lyrics, LyricsUploader
122
+ # mount_uploader :alternative_lyrics, LyricsUploader
123
+ # mount_uploader :file, SongUploader
124
+ # end
125
+ #
126
+ # This will add an anonymous uploader with only the default settings:
127
+ #
128
+ # class Data
129
+ # mount_uploader :csv
130
+ # end
131
+ #
132
+ # this will add an anonymous uploader overriding the store_dir:
133
+ #
134
+ # class Product
135
+ # mount_uploader :blueprint do
136
+ # def store_dir
137
+ # 'blueprints'
138
+ # end
139
+ # end
140
+ # end
141
+ #
142
+ def mount_uploader(column, uploader=nil, options={}, &block)
143
+ unless uploader
144
+ uploader = Class.new(CarrierWave::Uploader::Base)
145
+ uploader.class_eval(&block)
146
+ end
147
+
148
+ uploaders[column.to_sym] = uploader
149
+ uploader_options[column.to_sym] = options
150
+
151
+ include CarrierWave::Mount::Extension
152
+
153
+ # Make sure to write over accessors directly defined on the class.
154
+ # Simply super to the included module below.
155
+ class_eval <<-RUBY, __FILE__, __LINE__+1
156
+ def #{column}; super; end
157
+ def #{column}=(new_file); super; end
158
+ RUBY
159
+
160
+ # Mixing this in as a Module instead of class_evaling directly, so we
161
+ # can maintain the ability to super to any of these methods from within
162
+ # the class.
163
+ mod = Module.new
164
+ include mod
165
+ mod.class_eval <<-RUBY, __FILE__, __LINE__+1
166
+
167
+ def #{column}
168
+ _mounter(:#{column}).uploader
169
+ end
170
+
171
+ def #{column}=(new_file)
172
+ _mounter(:#{column}).cache(new_file)
173
+ end
174
+
175
+ def #{column}?
176
+ !_mounter(:#{column}).blank?
177
+ end
178
+
179
+ def #{column}_url(*args)
180
+ _mounter(:#{column}).url(*args)
181
+ end
182
+
183
+ def #{column}_cache
184
+ _mounter(:#{column}).cache_name
185
+ end
186
+
187
+ def #{column}_cache=(cache_name)
188
+ _mounter(:#{column}).cache_name = cache_name
189
+ end
190
+
191
+ def remote_#{column}_url
192
+ _mounter(:#{column}).remote_url
193
+ end
194
+
195
+ def remote_#{column}_url=(url)
196
+ _mounter(:#{column}).remote_url = url
197
+ end
198
+
199
+ def remove_#{column}
200
+ _mounter(:#{column}).remove
201
+ end
202
+
203
+ def remove_#{column}!
204
+ _mounter(:#{column}).remove!
205
+ end
206
+
207
+ def remove_#{column}=(value)
208
+ _mounter(:#{column}).remove = value
209
+ end
210
+
211
+ def remove_#{column}?
212
+ _mounter(:#{column}).remove?
213
+ end
214
+
215
+ def store_#{column}!
216
+ _mounter(:#{column}).store!
217
+ end
218
+
219
+ def #{column}_integrity_error
220
+ _mounter(:#{column}).integrity_error
221
+ end
222
+
223
+ def #{column}_processing_error
224
+ _mounter(:#{column}).processing_error
225
+ end
226
+
227
+ def write_#{column}_identifier
228
+ _mounter(:#{column}).write_identifier
229
+ end
230
+
231
+ RUBY
232
+
233
+ end
234
+
235
+ module Extension
236
+
237
+ ##
238
+ # overwrite this to read from a serialized attribute
239
+ #
240
+ def read_uploader(column); end
241
+
242
+ ##
243
+ # overwrite this to write to a serialized attribute
244
+ #
245
+ def write_uploader(column, identifier); end
246
+
247
+ private
248
+
249
+ def _mounter(column)
250
+ # We cannot memoize in frozen objects :(
251
+ return Mounter.new(self, column) if frozen?
252
+ @_mounters ||= {}
253
+ @_mounters[column] ||= Mounter.new(self, column)
254
+ end
255
+
256
+ end # Extension
257
+
258
+ # this is an internal class, used by CarrierWave::Mount so that
259
+ # we don't pollute the model with a lot of methods.
260
+ class Mounter #:nodoc:
261
+
262
+ attr_reader :column, :record, :remote_url, :integrity_error, :processing_error
263
+ attr_accessor :remove
264
+
265
+ def initialize(record, column, options={})
266
+ @record = record
267
+ @column = column
268
+ @options = record.class.uploader_options[column]
269
+ end
270
+
271
+ def write_identifier
272
+ if remove?
273
+ record.write_uploader(serialization_column, '')
274
+ elsif not uploader.identifier.blank?
275
+ record.write_uploader(serialization_column, uploader.identifier)
276
+ end
277
+ end
278
+
279
+ def identifier
280
+ record.read_uploader(serialization_column)
281
+ end
282
+
283
+ def uploader
284
+ @uploader ||= record.class.uploaders[column].new(record, column)
285
+
286
+ if @uploader.blank? and not identifier.blank?
287
+ @uploader.retrieve_from_store!(identifier)
288
+ end
289
+ return @uploader
290
+ end
291
+
292
+ def cache(new_file)
293
+ uploader.cache!(new_file)
294
+ @integrity_error = nil
295
+ @processing_error = nil
296
+ rescue CarrierWave::IntegrityError => e
297
+ @integrity_error = e
298
+ raise e unless option(:ignore_integrity_errors)
299
+ rescue CarrierWave::ProcessingError => e
300
+ @processing_error = e
301
+ raise e unless option(:ignore_processing_errors)
302
+ end
303
+
304
+ def cache_name
305
+ uploader.cache_name
306
+ end
307
+
308
+ def cache_name=(cache_name)
309
+ uploader.retrieve_from_cache!(cache_name) unless uploader.cached?
310
+ rescue CarrierWave::InvalidParameter
311
+ end
312
+
313
+ def remote_url=(url)
314
+ unless uploader.cached?
315
+ @remote_url = url
316
+ uploader.download!(url)
317
+ end
318
+ end
319
+
320
+ def store!
321
+ unless uploader.blank?
322
+ if remove?
323
+ uploader.remove!
324
+ else
325
+ uploader.store!
326
+ end
327
+ end
328
+ end
329
+
330
+ def url(*args)
331
+ uploader.url(*args)
332
+ end
333
+
334
+ def blank?
335
+ uploader.blank?
336
+ end
337
+
338
+ def remove?
339
+ !remove.blank? and remove !~ /\A0|false$\z/
340
+ end
341
+
342
+ def remove!
343
+ uploader.remove!
344
+ end
345
+
346
+ private
347
+
348
+ def option(name)
349
+ record.class.uploader_option(column, name)
350
+ end
351
+
352
+ def serialization_column
353
+ option(:mount_on) || column
354
+ end
355
+
356
+ end # Mounter
357
+
358
+ end # Mount
359
+ end # CarrierWave
@@ -0,0 +1,73 @@
1
+ # encoding: utf-8
2
+
3
+ require 'active_record'
4
+
5
+ module CarrierWave
6
+ module ActiveRecord
7
+
8
+ include CarrierWave::Mount
9
+
10
+ ##
11
+ # See +CarrierWave::Mount#mount_uploader+ for documentation
12
+ #
13
+ def mount_uploader(column, uploader, options={}, &block)
14
+ super
15
+
16
+ alias_method :read_uploader, :read_attribute
17
+ alias_method :write_uploader, :write_attribute
18
+
19
+ validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
20
+ validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
21
+
22
+ after_save "store_#{column}!"
23
+ before_save "write_#{column}_identifier"
24
+ after_destroy "remove_#{column}!"
25
+ end
26
+
27
+ ##
28
+ # Makes the record invalid if the file couldn't be uploaded due to an integrity error
29
+ #
30
+ # Accepts the usual parameters for validations in Rails (:if, :unless, etc...)
31
+ #
32
+ # === Note
33
+ #
34
+ # Set this key in your translations file for I18n:
35
+ #
36
+ # carrierwave:
37
+ # errors:
38
+ # integrity: 'Here be an error message'
39
+ #
40
+ def validates_integrity_of(*attrs)
41
+ options = attrs.last.is_a?(Hash) ? attrs.last : {}
42
+ options[:message] ||= I18n.t('carrierwave.errors.integrity', :default => 'is not an allowed type of file.')
43
+ validates_each(*attrs) do |record, attr, value|
44
+ record.errors.add attr, options[:message] if record.send("#{attr}_integrity_error")
45
+ end
46
+ end
47
+
48
+ ##
49
+ # Makes the record invalid if the file couldn't be processed (assuming the process failed
50
+ # with a CarrierWave::ProcessingError)
51
+ #
52
+ # Accepts the usual parameters for validations in Rails (:if, :unless, etc...)
53
+ #
54
+ # === Note
55
+ #
56
+ # Set this key in your translations file for I18n:
57
+ #
58
+ # carrierwave:
59
+ # errors:
60
+ # processing: 'Here be an error message'
61
+ #
62
+ def validates_processing_of(*attrs)
63
+ options = attrs.last.is_a?(Hash) ? attrs.last : {}
64
+ options[:message] ||= I18n.t('carrierwave.errors.processing', :default => 'failed to be processed.')
65
+ validates_each(*attrs) do |record, attr, value|
66
+ record.errors.add attr, options[:message] if record.send("#{attr}_processing_error")
67
+ end
68
+ end
69
+
70
+ end # ActiveRecord
71
+ end # CarrierWave
72
+
73
+ ActiveRecord::Base.send(:extend, CarrierWave::ActiveRecord)
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'dm-core'
4
+
5
+ module CarrierWave
6
+ module DataMapper
7
+
8
+ include CarrierWave::Mount
9
+
10
+ ##
11
+ # See +CarrierWave::Mount#mount_uploader+ for documentation
12
+ #
13
+ def mount_uploader(column, uploader, options={}, &block)
14
+ super
15
+
16
+ alias_method :read_uploader, :attribute_get
17
+ alias_method :write_uploader, :attribute_set
18
+ after :save, "store_#{column}!".to_sym
19
+ pre_hook = ::DataMapper.const_defined?(:Validate) ? :valid? : :save
20
+ before pre_hook, "write_#{column}_identifier".to_sym
21
+ after :destroy, "remove_#{column}!".to_sym
22
+ end
23
+
24
+ end # DataMapper
25
+ end # CarrierWave
26
+
27
+ DataMapper::Model.send(:include, CarrierWave::DataMapper)