module Resizing::CarrierWave

Public Class Methods

included(base) click to toggle source
# File lib/resizing/carrier_wave.rb, line 23
def self.included(base)
  base.storage Resizing::CarrierWave::Storage::Remote
  base.extend ClassMethods
end
new(*args) click to toggle source
Calls superclass method
# File lib/resizing/carrier_wave.rb, line 28
def initialize(*args)
  @requested_format = nil
  @default_format = nil
  super
end

Public Instance Methods

build_url() click to toggle source
# File lib/resizing/carrier_wave.rb, line 50
def build_url
  "#{Resizing.configure.image_host}#{model.read_attribute(serialization_column)}"
end
cache!(new_file) click to toggle source
# File lib/resizing/carrier_wave.rb, line 86
def cache!(new_file)
  return if new_file.nil?

  file = storage.store!(new_file)
  # do not assign @cache_id, bacause resizing do not support cache
  # save to resizing directly
  # @cache_id = file.public_id

  @filename = file.public_id.to_s
  @file = file
end
default_format() click to toggle source
# File lib/resizing/carrier_wave.rb, line 131
def default_format
  @default_format
end
default_url() click to toggle source

need override this. if you want to return some url when target_column is nil

# File lib/resizing/carrier_wave.rb, line 55
def default_url
  nil
end
filename() click to toggle source
# File lib/resizing/carrier_wave.rb, line 98
def filename
  read_column
end
format() click to toggle source
# File lib/resizing/carrier_wave.rb, line 135
def format
  requested_format || default_format
end
read_column() click to toggle source
# File lib/resizing/carrier_wave.rb, line 46
def read_column
  model.read_attribute(serialization_column)
end
remove_versions!(*args) click to toggle source

store_versions! is called after delete Disable on Resizing, because transform the image when browser fetch the image URL github.com/carrierwaveuploader/carrierwave/blob/28190e99299a6131c0424a5d10205f471e39f3cd/lib/carrierwave/uploader/versions.rb#L18

# File lib/resizing/carrier_wave.rb, line 121
def remove_versions!(*args)
  # NOP
end
rename() click to toggle source
# File lib/resizing/carrier_wave.rb, line 67
def rename
  raise NotImplementedError, 'rename is not implemented'
end
requested_format() click to toggle source
# File lib/resizing/carrier_wave.rb, line 125
def requested_format
  # TODO
  # The request with uploading format parameter is not working on the Resizing until 2020/09/25
  @requested_format
end
resize_to_fill(*args) click to toggle source
# File lib/resizing/carrier_wave.rb, line 76
def resize_to_fill(*args)
  @transform ||= []
  @transform.push(:resize_to_fill, *args)
end
resize_to_fit(*args) click to toggle source
# File lib/resizing/carrier_wave.rb, line 81
def resize_to_fit(*args)
  @transform ||= []
  @transform.push(:resize_to_fit, *args)
end
resize_to_limit(*args) click to toggle source
# File lib/resizing/carrier_wave.rb, line 71
def resize_to_limit(*args)
  @transform ||= []
  @transform.push(:resize_to_limit, *args)
end
serialization_column() click to toggle source
# File lib/resizing/carrier_wave.rb, line 107
def serialization_column
  model.send(:_mounter, mounted_as).send(:serialization_column)
end
store!() click to toggle source
Calls superclass method
# File lib/resizing/carrier_wave.rb, line 102
def store!
  # DO NOTHING
  super
end
store_versions!(*args) click to toggle source

store_versions! is called after store! Disable on Resizing, because transform the image when browser fetch the image URL github.com/carrierwaveuploader/carrierwave/blob/28190e99299a6131c0424a5d10205f471e39f3cd/lib/carrierwave/uploader/versions.rb#L18

# File lib/resizing/carrier_wave.rb, line 114
def store_versions!(*args)
  # NOP
end
transform_string() click to toggle source
# File lib/resizing/carrier_wave.rb, line 59
def transform_string
  transforms = processors.map do |processor|
    transform_string_from processor
  end

  transforms.join('/')
end
url(*args) click to toggle source
# File lib/resizing/carrier_wave.rb, line 34
def url(*args)
  return default_url unless read_column.present?

  transforms = args.map do |version|
    version = version.intern
    raise "No version is found: #{version}, #{versions.keys} are exists." unless versions.has_key? version
    versions[version].transform_string
  end.compact

  "#{build_url}/#{transforms.join('/')}"
end

Private Instance Methods

transform_string_from(processor) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/resizing/carrier_wave.rb, line 145
def transform_string_from(processor)
  action = processor.first
  value = processor.second

  case action
  when :resize_to_fill, :resize_to_limit, :resize_to_fit
    name = action.to_s.gsub(/resize_to_/, '')
    { c: name, w: value.first, h: value.second }
  when :transformation
    result = {}
    result[:q] = value[:quality] if value[:quality]
    result[:f] = value[:fetch_format] if value[:fetch_format]
    result[:f] = value[:format] if value[:format]
    result
  else
    raise NotImplementedError, "#{action} is not supported. #{processor.inspect}"
  end.map do |key, value|
    next nil if value.nil?

    "#{key}_#{value}"
  end.compact.join(',')
end