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!Object

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



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/carrierwave/uploader/versions.rb', line 116

def recreate_versions!
  # Some files could possibly not be stored on the local disk. This
  # doesn't play nicely with processing. To fix this, we create a new
  # file with the same original filename and we call file.read to get the
  # data for the file and then store that.
  #
  # The call to store! will trigger the necessary callbacks to both
  # process this version and all sub-versions
  local_file = SanitizedFile.new :tempfile => StringIO.new(file.read),
    :filename => File.basename(path)

  store! local_file
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.

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

Parameters

*args (Symbol)

any number of versions

Returns

String

the location where this file is accessible via a url



102
103
104
105
106
107
108
109
110
# File 'lib/carrierwave/uploader/versions.rb', line 102

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

#version_nameObject

Returns

String

the name of this version of the uploader



80
81
82
# File 'lib/carrierwave/uploader/versions.rb', line 80

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



66
67
68
69
70
71
72
73
# File 'lib/carrierwave/uploader/versions.rb', line 66

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