Module: CarrierWave::Uploader::Versions::ClassMethods

Defined in:
lib/carrierwave/uploader/versions.rb

Instance Method Summary collapse

Instance Method Details

#recursively_apply_block_to_versions(&block) ⇒ Object



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

def recursively_apply_block_to_versions(&block)
  versions.each do |name, version|
    version[:uploader].class_eval(&block)
    version[:uploader].recursively_apply_block_to_versions(&block)
  end
end

#version(name, options = {}, &block) ⇒ Object

Adds a new version to this uploader

Parameters

name (#to_sym)

name of the version

options (Hash)

optional options hash

&block (Proc)

a block to eval on this version of the uploader

Examples

class MyUploader < CarrierWave::Uploader::Base

  version :thumb do
    process :scale => [200, 200]
  end

  version :preview, :if => :image? do
    process :scale => [200, 200]
  end

end


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/carrierwave/uploader/versions.rb', line 61

def version(name, options = {}, &block)
  name = name.to_sym
  unless versions[name]
    uploader = Class.new(self)
    uploader.versions = {}

    # Define the enable_processing method for versions so they get the
    # value from the parent class unless explicitly overwritten
    uploader.class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def self.enable_processing(value=nil)
        self.enable_processing = value if value
        if !@enable_processing.nil?
          @enable_processing
        else
          superclass.enable_processing
        end
      end
    RUBY

    # Add the current version hash to class attribute :versions
    current_version = {}
    current_version[name] = {
      :uploader => uploader,
      :options  => options
    }
    self.versions = versions.merge(current_version)

    versions[name][:uploader].version_names += [name]

    class_eval <<-RUBY
      def #{name}
        versions[:#{name}]
      end
    RUBY
    # as the processors get the output from the previous processors as their
    # input we must not stack the processors here
    versions[name][:uploader].processors = versions[name][:uploader].processors.dup
    versions[name][:uploader].processors.clear
  end
  versions[name][:uploader].class_eval(&block) if block
  versions[name]
end