Class: Locomotive::Steam::Dragonfly

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/steam/monkey_patches/dragonfly.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enabledObject

Returns the value of attribute enabled.



5
6
7
# File 'lib/locomotive/steam/monkey_patches/dragonfly.rb', line 5

def enabled
  @enabled
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/locomotive/steam/monkey_patches/dragonfly.rb', line 5

def path
  @path
end

Class Method Details

.appObject



35
36
37
# File 'lib/locomotive/steam/monkey_patches/dragonfly.rb', line 35

def self.app
  ::Dragonfly[:images]
end

.instanceObject



40
41
42
# File 'lib/locomotive/steam/monkey_patches/dragonfly.rb', line 40

def self.instance
  @@instance ||= new
end

.setup!(path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/locomotive/steam/monkey_patches/dragonfly.rb', line 44

def self.setup!(path)
  self.instance.path    = path
  self.instance.enabled = false

  begin
    require 'rack/cache'
    require 'dragonfly'

    ## initialize Dragonfly ##
    app = ::Dragonfly[:images].configure_with(:imagemagick)

    ## configure it ##
    ::Dragonfly[:images].configure do |c|
      convert = `which convert`.strip.presence || '/usr/bin/env convert'
      c.convert_command  = convert
      c.identify_command = convert

      c.allow_fetch_url  = true
      c.allow_fetch_file = true

      c.url_format = '/images/dynamic/:job/:basename.:format'
    end

    self.instance.enabled = true
  rescue Exception => e
    
    Locomotive::Steam::Logger.warn %{
[Dragonfly] !disabled!
[Dragonfly] If you want to take full benefits of all the features in the LocomotiveSteam, we recommend you to install ImageMagick and RMagick. Check out the documentation here: http://doc.locomotivecms.com/editor/installation.
}
  end
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/locomotive/steam/monkey_patches/dragonfly.rb', line 7

def enabled?
  !!self.enabled
end

#resize_url(source, resize_string) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/locomotive/steam/monkey_patches/dragonfly.rb', line 11

def resize_url(source, resize_string)
  _source = (case source
  when String then source
  when Hash   then source['url'] || source[:url]
  else
    source.try(:url)
  end)

  if _source.blank?
    Locomotive::Steam::Logger.error "Unable to resize on the fly: #{source.inspect}"
    return
  end

  return _source unless self.enabled?

  if _source =~ /^http/
    file = self.class.app.fetch_url(_source)
  else
    file = self.class.app.fetch_file(File.join(self.path, 'public', _source))
  end

  file.process(:thumb, resize_string).url
end