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

Instance Method Summary collapse

Methods included from Callbacks

#with_callbacks

Instance Method Details

#recreate_versions!(*names) ⇒ Object

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


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/carrierwave/uploader/versions.rb', line 236

def recreate_versions!(*names)
  # Some files could possibly not be stored on the local disk. This
  # doesn't play nicely with processing. Make sure that we're only
  # processing a cached file
  #
  # The call to store! will trigger the necessary callbacks to both
  # process this version and all sub-versions

  if names.any?
    set_versions_to_cache_and_store(names)
    store!(file)
    reset_versions_to_cache_and_store
  else
    cache! if !cached?
    store!
  end
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


220
221
222
223
224
225
226
227
228
229
230
# File 'lib/carrierwave/uploader/versions.rb', line 220

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_exists?(name) ⇒ Boolean

Parameters

name (#to_sym)

name of the version

Returns

Boolean

True when the version exists according to its :if or :unless condition

Returns:

  • (Boolean)

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/carrierwave/uploader/versions.rb', line 170

def version_exists?(name)
  name = name.to_sym

  return false unless self.class.versions.has_key?(name)

  condition = self.class.versions[name].version_options[:if]
  if_condition = self.class.versions[name].version_options[:if]
  unless_condition = self.class.versions[name].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


156
157
158
# File 'lib/carrierwave/uploader/versions.rb', line 156

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


141
142
143
144
145
146
147
148
149
# File 'lib/carrierwave/uploader/versions.rb', line 141

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