Module: CarrierWave::Uploader::Versions

Extended by:
ActiveSupport::Concern
Includes:
Callbacks
Included in:
Base
Defined in:
lib/carrierwave/uploader/versions.rb

Defined Under Namespace

Modules: ClassMethods Classes: Builder

Instance Method Summary collapse

Methods included from Callbacks

#with_callbacks

Instance Method Details

#cache!(*args) ⇒ Object

Copies the parent’s cache_id when caching a version file. This behavior is not essential but it makes easier to understand that the cached files are generated by the single upload attempt.



233
234
235
236
237
# File 'lib/carrierwave/uploader/versions.rb', line 233

def cache!(*args)
  self.cache_id = parent_version.cache_id if parent_version

  super
end

#recreate_versions!(*names) ⇒ Object

Recreate versions and reprocess them. This can be used to recreate versions if their parameters somehow have changed.



279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/carrierwave/uploader/versions.rb', line 279

def recreate_versions!(*names)
  # As well as specified versions, we need to reprocess versions
  # that are the source of another version.

  self.cache_id = CarrierWave.generate_cache_id
  derived_versions.each_value do |v|
    v.cache!(file) if names.empty? || !(v.descendant_version_names & names).empty?
  end
  active_versions.each do |name, v|
    v.store! if names.empty? || names.include?(name)
  end
ensure
  @cache_id = nil
end

#url(*args) ⇒ Object

When given a version name as a parameter, will return the url for that version This also works with nested versions. When given a query hash as a parameter, will return the url with signature that contains query params Query hash only works with AWS (S3 storage).

Example

my_uploader.url                 # => /path/to/my/uploader.gif
my_uploader.url(:thumb)         # => /path/to/my/thumb_uploader.gif
my_uploader.url(:thumb, :small) # => /path/to/my/thumb_small_uploader.gif
my_uploader.url(:query => {"response-content-disposition" => "attachment"})
my_uploader.url(:version, :sub_version, :query => {"response-content-disposition" => "attachment"})

Parameters

*args (Symbol)

any number of versions

OR/AND

Hash

query params

Returns

String

the location where this file is accessible via a url



263
264
265
266
267
268
269
270
271
272
273
# File 'lib/carrierwave/uploader/versions.rb', line 263

def url(*args)
  if (version = args.first) && version.respond_to?(:to_sym)
    raise ArgumentError, "Version #{version} doesn't exist!" if versions[version.to_sym].nil?
    # recursively proxy to version
    versions[version.to_sym].url(*args[1..-1])
  elsif args.first
    super(args.first)
  else
    super
  end
end

#version_active?(name) ⇒ Boolean Also known as: version_exists?

Parameters

name (#to_sym)

name of the version

Returns

Boolean

True when the version satisfy its :if or :unless condition

Returns:

  • (Boolean)


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/carrierwave/uploader/versions.rb', line 201

def version_active?(name)
  name = name.to_sym

  return false unless versions.has_key?(name)

  if_condition = versions[name].class.version_options[:if]
  unless_condition = versions[name].class.version_options[:unless]

  if if_condition
    if if_condition.respond_to?(:call)
      if_condition.call(self, :version => name, :file => file)
    else
      send(if_condition, file)
    end
  elsif unless_condition
    if unless_condition.respond_to?(:call)
      !unless_condition.call(self, :version => name, :file => file)
    else
      !send(unless_condition, file)
    end
  else
    true
  end
end

#version_nameObject

Returns

String

the name of this version of the uploader



187
188
189
# File 'lib/carrierwave/uploader/versions.rb', line 187

def version_name
  self.class.version_names.join('_').to_sym unless self.class.version_names.blank?
end

#versionsObject

Returns a hash mapping the name of each version of the uploader to an instance of it

Returns

Hash=> CarrierWave::Uploader

a list of uploader instances



172
173
174
175
176
177
178
179
180
# File 'lib/carrierwave/uploader/versions.rb', line 172

def versions
  return @versions if @versions
  @versions = {}
  self.class.versions.each do |name, version|
    @versions[name] = version.build(self.class).new(model, mounted_as)
    @versions[name].parent_version = self
  end
  @versions
end