Module: Paperclip

Defined in:
lib/paperclip.rb,
lib/paperclip/style.rb,
lib/paperclip/upfile.rb,
lib/paperclip/railtie.rb,
lib/paperclip/storage.rb,
lib/paperclip/version.rb,
lib/paperclip/geometry.rb,
lib/paperclip/matchers.rb,
lib/paperclip/processor.rb,
lib/paperclip/thumbnail.rb,
lib/paperclip/attachment.rb,
lib/paperclip/interpolations.rb,
lib/paperclip/callback_compatability.rb,
lib/paperclip/matchers/have_attached_file_matcher.rb,
lib/paperclip/matchers/validate_attachment_size_matcher.rb,
lib/paperclip/matchers/validate_attachment_presence_matcher.rb,
lib/paperclip/matchers/validate_attachment_content_type_matcher.rb

Overview

The base module that gets included in ActiveRecord::Base. See the documentation for Paperclip::ClassMethods for more useful information.

Defined Under Namespace

Modules: CallbackCompatability, ClassMethods, InstanceMethods, Interpolations, Shoulda, Storage, Upfile Classes: Attachment, CommandNotFoundError, Geometry, InfiniteInterpolationError, NotIdentifiedByImageMagickError, PaperclipCommandLineError, PaperclipError, Processor, Railtie, Style, Tempfile, Thumbnail

Constant Summary collapse

VERSION =
"2.3.3"

Class Method Summary collapse

Class Method Details

.bit_bucketObject

:nodoc:



138
139
140
# File 'lib/paperclip.rb', line 138

def bit_bucket #:nodoc:
  File.exists?("/dev/null") ? "/dev/null" : "NUL"
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Paperclip)

    the object that the method was called on



74
75
76
# File 'lib/paperclip.rb', line 74

def configure
  yield(self) if block_given?
end

.included(base) ⇒ Object

:nodoc:



142
143
144
145
146
147
148
149
# File 'lib/paperclip.rb', line 142

def included base #:nodoc:
  base.extend ClassMethods
  if base.respond_to?("set_callback")
    base.send :include, Paperclip::CallbackCompatability::Rails3
  else
    base.send :include, Paperclip::CallbackCompatability::Rails21
  end
end

.interpolates(key, &block) ⇒ Object



86
87
88
# File 'lib/paperclip.rb', line 86

def interpolates key, &block
  Paperclip::Interpolations[key] = block
end

.log(message) ⇒ Object

Log a paperclip-specific line. Uses ActiveRecord::Base.logger by default. Set Paperclip.options to false to turn off.



162
163
164
# File 'lib/paperclip.rb', line 162

def log message
  logger.info("[paperclip] #{message}") if logging?
end

.loggerObject

:nodoc:



166
167
168
# File 'lib/paperclip.rb', line 166

def logger #:nodoc:
  ActiveRecord::Base.logger
end

.logging?Boolean

:nodoc:

Returns:

  • (Boolean)


170
171
172
# File 'lib/paperclip.rb', line 170

def logging? #:nodoc:
  options[:log]
end

.optionsObject

Provides configurability to Paperclip. There are a number of options available, such as:

  • whiny: Will raise an error if Paperclip cannot process thumbnails of an uploaded image. Defaults to true.

  • log: Logs progress to the Rails log. Uses ActiveRecord’s logger, so honors log levels, etc. Defaults to true.

  • command_path: Defines the path at which to find the command line programs if they are not visible to Rails the system’s search path. Defaults to nil, which uses the first executable found in the user’s search path.

  • image_magick_path: Deprecated alias of command_path.



63
64
65
66
67
68
69
70
71
72
# File 'lib/paperclip.rb', line 63

def options
  @options ||= {
    :whiny             => true,
    :image_magick_path => nil,
    :command_path      => nil,
    :log               => true,
    :log_command       => true,
    :swallow_stderr    => true
  }
end

.path_for_command(command) ⇒ Object

:nodoc:



78
79
80
81
82
83
84
# File 'lib/paperclip.rb', line 78

def path_for_command command #:nodoc:
  if options[:image_magick_path]
    warn("[DEPRECATION] :image_magick_path is deprecated and will be removed. Use :command_path instead")
  end
  path = [options[:command_path] || options[:image_magick_path], command].compact
  File.join(*path)
end

.processor(name) ⇒ Object

:nodoc:



151
152
153
154
155
156
157
158
# File 'lib/paperclip.rb', line 151

def processor name #:nodoc:
  name = name.to_s.camelize
  processor = Paperclip.const_get(name)
  unless processor.ancestors.include?(Paperclip::Processor)
    raise PaperclipError.new("Processor #{name} was not found")
  end
  processor
end

.quote_command_options(*options) ⇒ Object



132
133
134
135
136
# File 'lib/paperclip.rb', line 132

def quote_command_options(*options)
  options.map do |option|
    option.split("'").map{|m| "'#{m}'" }.join("\\'")
  end
end

.run(cmd, *params) ⇒ Object

The run method takes a command to execute and an array of parameters that get passed to it. The command is prefixed with the :command_path option from Paperclip.options. If you have many commands to run and they are in different paths, the suggested course of action is to symlink them so they are all in the same directory.

If the command returns with a result code that is not one of the expected_outcodes, a PaperclipCommandLineError will be raised. Generally a code of 0 is expected, but a list of codes may be passed if necessary. These codes should be passed as a hash as the last argument, like so:

Paperclip.run("echo", "something", :expected_outcodes => [0,1,2,3])

This method can log the command being run when Paperclip.options is set to true (defaults to false). This will only log if logging in general is set to true as well.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/paperclip.rb', line 106

def run cmd, *params
  options           = params.last.is_a?(Hash) ? params.pop : {}
  expected_outcodes = options[:expected_outcodes] || [0]
  params            = quote_command_options(*params).join(" ")

  command = %Q[#{path_for_command(cmd)} #{params}]
  command = "#{command} 2>#{bit_bucket}" if Paperclip.options[:swallow_stderr]
  Paperclip.log(command) if Paperclip.options[:log_command]

  begin
    output = `#{command}`

    raise CommandNotFoundError if $?.exitstatus == 127

    unless expected_outcodes.include?($?.exitstatus)
      raise PaperclipCommandLineError,
        "Error while running #{cmd}. Expected return code to be #{expected_outcodes.join(", ")} but was #{$?.exitstatus}",
        output
    end
  rescue Errno::ENOENT => e
    raise CommandNotFoundError
  end

  output
end