Module: Locomotive::CarrierWave::Uploader::Asset

Extended by:
ActiveSupport::Concern
Included in:
Locomotive::ContentAssetUploader, ThemeAssetUploader
Defined in:
lib/locomotive/carrierwave/asset.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#apply_content_type_exception(value) ⇒ Object


77
78
79
80
# File 'lib/locomotive/carrierwave/asset.rb', line 77

def apply_content_type_exception(value)
  # by default, do nothing
  value
end

#find_content_type_of_model(content_type) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/locomotive/carrierwave/asset.rb', line 62

def find_content_type_of_model(content_type)
  value = :other

  self.class.content_types.each_pair do |type, rules|
    rules.each do |rule|
      case rule
      when String then value = type if content_type == rule
      when Regexp then value = type if (content_type =~ rule) == 0
      end
    end
  end

  apply_content_type_exception(value)
end

#image?(file) ⇒ Boolean

Returns:


58
59
60
# File 'lib/locomotive/carrierwave/asset.rb', line 58

def image?(file)
  model.image?
end

#set_content_type_of_model(*args) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/locomotive/carrierwave/asset.rb', line 31

def set_content_type_of_model(*args)
  content_type = file.content_type

  if content_type.blank? || ['application/octet-stream'].include?(content_type)
    content_type = File.mime_type?(original_filename)
  end

  value = find_content_type_of_model(content_type)

  model.content_type = value
end

#set_size(*args) ⇒ Object


43
44
45
# File 'lib/locomotive/carrierwave/asset.rb', line 43

def set_size(*args)
  model.size = file.size
end

#set_width_and_heightObject


47
48
49
50
51
52
53
54
55
56
# File 'lib/locomotive/carrierwave/asset.rb', line 47

def set_width_and_height
  if model.image?
    dragonfly_img = ::Dragonfly.app(:engine).fetch_file(current_path)
    begin
      model.width, model.height = dragonfly_img.width, dragonfly_img.height
    rescue Exception => e
      Rails.logger.error "[Dragonfly] Unable to get the width and the height, error: #{e.message}\n#{e.backtrace.join("\n")}"
    end
  end
end