Module: RetinaRails::Strategies::CarrierWave::Uploader::ClassMethods

Defined in:
lib/retina_rails/strategies/carrierwave.rb

Instance Method Summary collapse

Instance Method Details

#optimize_retina!(name, options = {}) ⇒ Object

Optimize version for retina displays

Parameters

name (Sym)

name of the version



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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/retina_rails/strategies/carrierwave.rb', line 64

def optimize_retina!(name, options={})
  config = versions[name]
  options[:retina] = false

  processors = if config.respond_to?(:processors)
                 config.processors.dup
               else
                 config[:uploader].processors.dup
               end
  dimensions_processor = nil

  ## Check if there's a resize processor to get the dimensions
  processors.each do |p|
    dimensions_processor = processors.delete(p) if p[0].to_s.scan(/resize_to_fill|resize_to_limit|resize_to_fit|resize_and_pad/).any?
  end

  ## Define a retina version if processor is present
  if dimensions_processor
    dimensions = dimensions_processor[1].dup

    width  = dimensions[0] * 2
    height = dimensions[1] * 2

    2.times { dimensions.delete_at(0) }

    dimensions.insert(0, height)
    dimensions.insert(0, width)

    ## Reset the processors
    if config.respond_to?(:processors)
      config.processors = []
    else
      config[:uploader].processors = []
    end

    ## Override version with double height and width
    version name, options do
      process dimensions_processor[0] => dimensions

      quality_processor = nil

      ## Set other processors
      processors.each do |processor|
        process processor[0] => processor[1]

        quality_processor = true if processor[0] == :retina_quality
      end

      ## Set default quality if retina_quality is not defined
      process :retina_quality => 60 if quality_processor.nil?

      ## Store dimensions
      process :store_retina_dimensions
    end
  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

  retina!

  version :thumb do
    process :resize_to_fill => [30, 30]
    process :retina_quality => 25
  end

  version :thumb, :retina => false do
    process :resize_to_fill => [30, 30]
  end

end


51
52
53
54
55
# File 'lib/retina_rails/strategies/carrierwave.rb', line 51

def version(name, options={}, &block)
  super

  optimize_retina!(name, { :if => options[:if] }) unless options[:retina] == false
end