Class: Udongo::Assets::Resizer

Inherits:
Object
  • Object
show all
Defined in:
lib/udongo/assets/resizer.rb

Instance Method Summary collapse

Constructor Details

#initialize(asset) ⇒ Resizer

Returns a new instance of Resizer.



6
7
8
# File 'lib/udongo/assets/resizer.rb', line 6

def initialize(asset)
  @asset = asset
end

Instance Method Details

#actual_path(calculated_filename) ⇒ Object



54
55
56
# File 'lib/udongo/assets/resizer.rb', line 54

def actual_path(calculated_filename)
  "#{Rails.root}/public/uploads/assets/_cache/#{main_dir}/#{second_dir}/#{calculated_filename}"
end

#actual_url(calculated_filename) ⇒ Object



45
46
47
# File 'lib/udongo/assets/resizer.rb', line 45

def actual_url(calculated_filename)
  "/uploads/assets/_cache/#{main_dir}/#{second_dir}/#{calculated_filename}"
end

#filename(width = nil, height = nil, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/udongo/assets/resizer.rb', line 10

def filename(width = nil, height = nil, options = {})
  action = options.key?(:action) ? options[:action] : :resize_to_limit
  quality = options[:quality]
  gravity = options[:gravity].to_s.underscore.split('_').map { |s| s[0,1] }.join
  background = options[:background].to_s.parameterize

  str = action.to_s.split('_').last
  str << "-q#{quality}" if quality.present?
  str << "-g#{gravity}" if gravity.present?
  str << "-b#{background}" if background.present?
  str << "-#{width}x#{height}-#{@asset.actual_filename}"
end

#path(width = nil, height = nil, options = {}) ⇒ Object



49
50
51
52
# File 'lib/udongo/assets/resizer.rb', line 49

def path(width = nil, height = nil, options = {})
  url(width, height, options) # Trigger the actual resize (if needed)
  actual_path(filename(width, height, options))
end

#url(width = nil, height = nil, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/udongo/assets/resizer.rb', line 23

def url(width = nil, height = nil, options = {})
  options[:action] = :resize_to_limit unless options.key?(:action)

  if width.nil? && height.nil?
    return "/uploads/assets/#{main_dir}/#{second_dir}/#{@asset.actual_filename}"
  end

  name = filename(width, height, options)

  unless File.exists?(actual_path(name))
    FileUtils.mkpath(File.dirname(actual_path(name)))

    unless resize_action_allowed? options[:action]
      raise "No such resize action '#{options[:action].to_s}'. Available are: resize_to_limit, resize_to_fit, resize_to_fill and resize_and_pad."
    end

    trigger_resize(width, height, options)
  end

  actual_url(name)
end