Module: CarrierWave::Uploader::ClassMethods

Defined in:
lib/carrierwave/uploader.rb

Instance Method Summary collapse

Instance Method Details

#process(*args) ⇒ Object

Adds a processor callback which applies operations as a file is uploaded. The argument may be the name of any method of the uploader, expressed as a symbol, or a list of such methods, or a hash where the key is a method and the value is an array of arguments to call the method with

Parameters

args (*Symbol, Hash=> Array[])

Examples

class MyUploader
  include CarrierWave::Uploader

  process :sepiatone, :vignette
  process :scale => [200, 200]

  def sepiatone
    ...
  end

  def vignette
    ...
  end

  def scale(height, width)
    ...
  end
end


80
81
82
83
84
85
86
87
88
89
90
# File 'lib/carrierwave/uploader.rb', line 80

def process(*args)
  args.each do |arg|
    if arg.is_a?(Hash)
      arg.each do |method, args|
        processors.push([method, args])
      end
    else
      processors.push([arg, []])
    end
  end
end

#processorsObject

Lists processor callbacks declared

Returns

Array[Array[Symbol, Array]]

a list of processor callbacks which have been declared for this uploader



45
46
47
# File 'lib/carrierwave/uploader.rb', line 45

def processors
  @processors ||= []
end

#storage(storage = nil) ⇒ Object Also known as: storage=

Sets the storage engine to be used when storing files with this uploader. Can be any class that implements a #store!(CarrierWave::SanitizedFile) and a #retrieve! method. See lib/carrierwave/storage/file.rb for an example. Storage engines should be added to CarrierWave.config so they can be referred to by a symbol, which should be more convenient

If no argument is given, it will simply return the currently used storage engine.

Parameters

storage (Symbol, Class)

The storage engine to use for this uploader

Returns

Class

the storage engine to be used with this uploader

Examples

storage :file
storage CarrierWave::Storage::File
storage MyCustomStorageEngine


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

def storage(storage = nil)
  if storage.is_a?(Symbol)
    @storage = get_storage_by_symbol(storage)
    @storage.setup!
  elsif storage
    @storage = storage
    @storage.setup!
  elsif @storage.nil?
    # Get the storage from the superclass if there is one
    @storage = superclass.storage rescue nil
  end
  if @storage.nil?
    # If we were not able to find a store any other way, setup the default store
    @storage ||= get_storage_by_symbol(CarrierWave.config[:storage])
    @storage.setup!
  end
  return @storage
end

#version(name, &block) ⇒ Object

Adds a new version to this uploader

Parameters

name (#to_sym)

name of the version

&block (Proc)

a block to eval on this version of the uploader



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/carrierwave/uploader.rb', line 148

def version(name, &block)
  name = name.to_sym
  unless versions[name]
    versions[name] = Class.new(self)
    versions[name].version_names.push(*version_names)
    versions[name].version_names.push(name)
    class_eval <<-RUBY
      def #{name}
        versions[:#{name}]
      end
    RUBY
  end
  versions[name].class_eval(&block) if block
  versions[name]
end

#version_namesObject



136
137
138
# File 'lib/carrierwave/uploader.rb', line 136

def version_names
  @version_names ||= []
end

#versionsObject

Returns

Hash=> Class

a list of versions available for this uploader



169
170
171
# File 'lib/carrierwave/uploader.rb', line 169

def versions
  @versions ||= {}
end