jnicklas-carrierwave 0.2.2 → 0.2.3

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 (46) hide show
  1. data/README.rdoc +35 -20
  2. data/Rakefile +1 -1
  3. data/lib/carrierwave/compatibility/paperclip.rb +91 -0
  4. data/lib/carrierwave/core_ext/inheritable_attributes.rb +102 -0
  5. data/lib/carrierwave/core_ext/module_setup.rb +49 -0
  6. data/lib/carrierwave/mount.rb +119 -103
  7. data/lib/carrierwave/orm/activerecord.rb +6 -1
  8. data/lib/carrierwave/orm/sequel.rb +15 -2
  9. data/lib/carrierwave/processing/rmagick.rb +8 -7
  10. data/lib/carrierwave/storage/abstract.rb +16 -1
  11. data/lib/carrierwave/storage/file.rb +20 -1
  12. data/lib/carrierwave/uploader/cache.rb +114 -0
  13. data/lib/carrierwave/uploader/callbacks.rb +40 -0
  14. data/lib/carrierwave/uploader/default_path.rb +21 -0
  15. data/lib/carrierwave/uploader/extension_whitelist.rb +35 -0
  16. data/lib/carrierwave/uploader/mountable.rb +37 -0
  17. data/lib/carrierwave/uploader/paths.rb +25 -0
  18. data/lib/carrierwave/uploader/processing.rb +79 -0
  19. data/lib/carrierwave/uploader/proxy.rb +60 -0
  20. data/lib/carrierwave/uploader/remove.rb +21 -0
  21. data/lib/carrierwave/uploader/store.rb +154 -0
  22. data/lib/carrierwave/uploader/url.rb +22 -0
  23. data/lib/carrierwave/uploader/versions.rb +145 -0
  24. data/lib/carrierwave/uploader.rb +31 -593
  25. data/lib/carrierwave.rb +55 -7
  26. data/lib/generators/uploader_generator.rb +1 -1
  27. data/rails_generators/uploader/templates/uploader.rb +12 -8
  28. data/spec/compatibility/paperclip_spec.rb +41 -0
  29. data/spec/mount_spec.rb +88 -25
  30. data/spec/orm/activerecord_spec.rb +7 -9
  31. data/spec/orm/datamapper_spec.rb +7 -9
  32. data/spec/orm/sequel_spec.rb +47 -32
  33. data/spec/spec_helper.rb +13 -0
  34. data/spec/uploader/cache_spec.rb +194 -0
  35. data/spec/uploader/default_path_spec.rb +66 -0
  36. data/spec/uploader/extension_whitelist_spec.rb +42 -0
  37. data/spec/uploader/mountable_spec.rb +31 -0
  38. data/spec/uploader/paths_spec.rb +20 -0
  39. data/spec/uploader/processing_spec.rb +60 -0
  40. data/spec/uploader/proxy_spec.rb +52 -0
  41. data/spec/uploader/remove_spec.rb +65 -0
  42. data/spec/uploader/store_spec.rb +260 -0
  43. data/spec/uploader/url_spec.rb +85 -0
  44. data/spec/uploader/versions_spec.rb +275 -0
  45. metadata +34 -3
  46. data/spec/uploader_spec.rb +0 -887
@@ -0,0 +1,154 @@
1
+ module CarrierWave
2
+ module Uploader
3
+ module Store
4
+
5
+ depends_on CarrierWave::Uploader::Paths
6
+ depends_on CarrierWave::Uploader::Callbacks
7
+ depends_on CarrierWave::Uploader::Cache
8
+
9
+ module ClassMethods
10
+
11
+ ##
12
+ # Sets the storage engine to be used when storing files with this uploader.
13
+ # Can be any class that implements a #store!(CarrierWave::SanitizedFile) and a #retrieve!
14
+ # method. See lib/carrierwave/storage/file.rb for an example. Storage engines should
15
+ # be added to CarrierWave.config[:storage_engines] so they can be referred
16
+ # to by a symbol, which should be more convenient
17
+ #
18
+ # If no argument is given, it will simply return the currently used storage engine.
19
+ #
20
+ # === Parameters
21
+ #
22
+ # [storage (Symbol, Class)] The storage engine to use for this uploader
23
+ #
24
+ # === Returns
25
+ #
26
+ # [Class] the storage engine to be used with this uploader
27
+ #
28
+ # === Examples
29
+ #
30
+ # storage :file
31
+ # storage CarrierWave::Storage::File
32
+ # storage MyCustomStorageEngine
33
+ #
34
+ def storage(storage = nil)
35
+ if storage.is_a?(Symbol)
36
+ @storage = get_storage_by_symbol(storage)
37
+ @storage.setup!
38
+ elsif storage
39
+ @storage = storage
40
+ @storage.setup!
41
+ elsif @storage.nil?
42
+ # Get the storage from the superclass if there is one
43
+ @storage = superclass.storage rescue nil
44
+ end
45
+ if @storage.nil?
46
+ # If we were not able to find a store any other way, setup the default store
47
+ @storage ||= get_storage_by_symbol(CarrierWave.config[:storage])
48
+ @storage.setup!
49
+ end
50
+ return @storage
51
+ end
52
+
53
+ alias_method :storage=, :storage
54
+
55
+ private
56
+
57
+ def get_storage_by_symbol(symbol)
58
+ eval(CarrierWave.config[:storage_engines][symbol])
59
+ end
60
+
61
+ end
62
+
63
+ ##
64
+ # Override this in your Uploader to change the filename.
65
+ #
66
+ # Be careful using record ids as filenames. If the filename is stored in the database
67
+ # the record id will be nil when the filename is set. Don't use record ids unless you
68
+ # understand this limitation.
69
+ #
70
+ # Do not use the version_name in the filename, as it will prevent versions from being
71
+ # loaded correctly.
72
+ #
73
+ # === Returns
74
+ #
75
+ # [String] a filename
76
+ #
77
+ def filename
78
+ @filename
79
+ end
80
+
81
+ ##
82
+ # Override this in your Uploader to change the directory where the file backend stores files.
83
+ #
84
+ # Other backends may or may not use this method, depending on their specific needs.
85
+ #
86
+ # === Returns
87
+ #
88
+ # [String] a directory
89
+ #
90
+ def store_dir
91
+ CarrierWave.config[:store_dir]
92
+ end
93
+
94
+ ##
95
+ # Calculates the path where the file should be stored. If +for_file+ is given, it will be
96
+ # used as the filename, otherwise +CarrierWave::Uploader#filename+ is assumed.
97
+ #
98
+ # === Parameters
99
+ #
100
+ # [for_file (String)] name of the file <optional>
101
+ #
102
+ # === Returns
103
+ #
104
+ # [String] the store path
105
+ #
106
+ def store_path(for_file=filename)
107
+ File.join(store_dir, full_filename(for_file))
108
+ end
109
+
110
+ ##
111
+ # Stores the file by passing it to this Uploader's storage engine.
112
+ #
113
+ # If new_file is omitted, a previously cached file will be stored.
114
+ #
115
+ # === Parameters
116
+ #
117
+ # [new_file (File, IOString, Tempfile)] any kind of file object
118
+ #
119
+ def store!(new_file=nil)
120
+ cache!(new_file) if new_file
121
+ if @file and @cache_id
122
+ with_callbacks(:store, new_file) do
123
+ @file = storage.store!(self, @file)
124
+ @cache_id = nil
125
+ end
126
+ end
127
+ end
128
+
129
+ ##
130
+ # Retrieves the file from the storage.
131
+ #
132
+ # === Parameters
133
+ #
134
+ # [identifier (String)] uniquely identifies the file to retrieve
135
+ #
136
+ def retrieve_from_store!(identifier)
137
+ with_callbacks(:retrieve_from_store, identifier) do
138
+ @file = storage.retrieve!(self, identifier)
139
+ end
140
+ end
141
+
142
+ private
143
+
144
+ def full_filename(for_file)
145
+ for_file
146
+ end
147
+
148
+ def storage
149
+ self.class.storage
150
+ end
151
+
152
+ end # Store
153
+ end # Uploader
154
+ end # CarrierWave
@@ -0,0 +1,22 @@
1
+ module CarrierWave
2
+ module Uploader
3
+ module Url
4
+
5
+ ##
6
+ # === Returns
7
+ #
8
+ # [String] the location where this file is accessible via a url
9
+ #
10
+ def url
11
+ if file.respond_to?(:url) and not file.url.blank?
12
+ file.url
13
+ elsif current_path
14
+ File.expand_path(current_path).gsub(File.expand_path(public), '')
15
+ end
16
+ end
17
+
18
+ alias_method :to_s, :url
19
+
20
+ end # Url
21
+ end # Uploader
22
+ end # CarrierWave
@@ -0,0 +1,145 @@
1
+ module CarrierWave
2
+ module Uploader
3
+ module Versions
4
+
5
+ setup do
6
+ after :cache, :cache_versions!
7
+ after :store, :store_versions!
8
+ after :remove, :remove_versions!
9
+ after :retrieve_from_cache, :retrieve_versions_from_cache!
10
+ after :retrieve_from_store, :retrieve_versions_from_store!
11
+ end
12
+
13
+ module ClassMethods
14
+
15
+ def version_names
16
+ @version_names ||= []
17
+ end
18
+
19
+ ##
20
+ # Adds a new version to this uploader
21
+ #
22
+ # === Parameters
23
+ #
24
+ # [name (#to_sym)] name of the version
25
+ # [&block (Proc)] a block to eval on this version of the uploader
26
+ #
27
+ def version(name, &block)
28
+ name = name.to_sym
29
+ unless versions[name]
30
+ versions[name] = Class.new(self)
31
+ versions[name].version_names.push(*version_names)
32
+ versions[name].version_names.push(name)
33
+ class_eval <<-RUBY
34
+ def #{name}
35
+ versions[:#{name}]
36
+ end
37
+ RUBY
38
+ end
39
+ versions[name].class_eval(&block) if block
40
+ versions[name]
41
+ end
42
+
43
+ ##
44
+ # === Returns
45
+ #
46
+ # [Hash{Symbol => Class}] a list of versions available for this uploader
47
+ #
48
+ def versions
49
+ @versions ||= {}
50
+ end
51
+
52
+ end # ClassMethods
53
+
54
+ ##
55
+ # Returns a hash mapping the name of each version of the uploader to an instance of it
56
+ #
57
+ # === Returns
58
+ #
59
+ # [Hash{Symbol => CarrierWave::Uploader}] a list of uploader instances
60
+ #
61
+ def versions
62
+ return @versions if @versions
63
+ @versions = {}
64
+ self.class.versions.each do |name, klass|
65
+ @versions[name] = klass.new(model, mounted_as)
66
+ end
67
+ @versions
68
+ end
69
+
70
+ ##
71
+ # === Returns
72
+ #
73
+ # [String] the name of this version of the uploader
74
+ #
75
+ def version_name
76
+ self.class.version_names.join('_').to_sym unless self.class.version_names.blank?
77
+ end
78
+
79
+ ##
80
+ # When given a version name as a parameter, will return the url for that version
81
+ # This also works with nested versions.
82
+ #
83
+ # === Example
84
+ #
85
+ # my_uploader.url # => /path/to/my/uploader.gif
86
+ # my_uploader.url(:thumb) # => /path/to/my/thumb_uploader.gif
87
+ # my_uploader.url(:thumb, :small) # => /path/to/my/thumb_small_uploader.gif
88
+ #
89
+ # === Parameters
90
+ #
91
+ # [*args (Symbol)] any number of versions
92
+ #
93
+ # === Returns
94
+ #
95
+ # [String] the location where this file is accessible via a url
96
+ #
97
+ def url(*args)
98
+ if(args.first)
99
+ raise ArgumentError, "Version #{args.first} doesn't exist!" if versions[args.first.to_sym].nil?
100
+ # recursively proxy to version
101
+ versions[args.first.to_sym].url(*args[1..-1])
102
+ else
103
+ super()
104
+ end
105
+ end
106
+
107
+ private
108
+
109
+ def full_filename(for_file)
110
+ [version_name, super(for_file)].compact.join('_')
111
+ end
112
+
113
+ def full_original_filename
114
+ [version_name, super].compact.join('_')
115
+ end
116
+
117
+ def cache_versions!(new_file)
118
+ versions.each do |name, v|
119
+ v.send(:cache_id=, cache_id)
120
+ v.cache!(new_file)
121
+ end
122
+ end
123
+
124
+ def store_versions!(new_file)
125
+ versions.each { |name, v| v.store!(new_file) }
126
+ end
127
+
128
+ def remove_versions!
129
+ versions.each do |name, v|
130
+ CarrierWave.logger.info "CarrierWave: removing file for version #{v.version_name}"
131
+ v.remove!
132
+ end
133
+ end
134
+
135
+ def retrieve_versions_from_cache!(cache_name)
136
+ versions.each { |name, v| v.retrieve_from_cache!(cache_name) }
137
+ end
138
+
139
+ def retrieve_versions_from_store!(identifier)
140
+ versions.each { |name, v| v.retrieve_from_store!(identifier) }
141
+ end
142
+
143
+ end # Versions
144
+ end # Uploader
145
+ end # CarrierWave