Class: Paperclip::Caption

Inherits:
Processor
  • Object
show all
Defined in:
lib/caption_paperclip_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}, attachment = nil) ⇒ Caption

Returns a new instance of Caption.



6
7
8
9
10
11
12
13
# File 'lib/caption_paperclip_processor.rb', line 6

def initialize file, options = {}, attachment = nil
  super

  @file             = file
  @format           = options[:format]
  @current_format   = File.extname(@file.path)
  @basename         = File.basename(@file.path, @current_format)
end

Instance Method Details

#makeObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/caption_paperclip_processor.rb', line 15

def make
  src = @file
  dst = Tempfile.new([@basename, @format].compact.join("."))
  dst.binmode

  begin
    parameters = []
    parameters << caption_options
    parameters << caption
    parameters << ":source"
    parameters << "+swap"
    parameters << "-gravity center"
    parameters << "-composite"
    parameters << ":dest"

    parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")

    success = convert(parameters, source: "#{File.expand_path(src.path)}[0]", dest: File.expand_path(dst.path))
  rescue Cocaine::ExitStatusError => e
    raise Paperclip::Error, "There was an error processing caption for #{@basename}" if @whiny
  rescue Cocaine::CommandNotFoundError => e
    raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `convert` command. Please install ImageMagick.")
  end

  dst
end