Module: Paperclip

Defined in:
lib/dm-paperclip.rb,
lib/dm-paperclip/upfile.rb,
lib/dm-paperclip/storage.rb,
lib/dm-paperclip/geometry.rb,
lib/dm-paperclip/processor.rb,
lib/dm-paperclip/thumbnail.rb,
lib/dm-paperclip/attachment.rb,
lib/dm-paperclip/validations.rb,
lib/dm-paperclip/interpolations.rb,
lib/dm-paperclip/callback_compatability.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, Resource, Storage, Upfile, Validate Classes: Attachment, Configuration, Geometry, InfiniteInterpolationError, NotIdentifiedByImageMagickError, PaperclipCommandLineError, PaperclipError, Processor, Tempfile, Thumbnail

Constant Summary collapse

VERSION =
"2.6.2.1"

Class Method Summary collapse

Class Method Details

.bit_bucketObject

:nodoc:



161
162
163
# File 'lib/dm-paperclip.rb', line 161

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

.configObject



66
67
68
# File 'lib/dm-paperclip.rb', line 66

def self.config
  @config ||= Configuration.new
end

.config=(config) ⇒ Object



62
63
64
# File 'lib/dm-paperclip.rb', line 62

def self.config=(config)
  @config = config
end

.configure {|@config = Configuration.new| ... } ⇒ Object

To configure Paperclip, put this code in an initializer, Rake task, or wherever:

Paperclip.configure do |config|
  config.root               = Rails.root # the application root to anchor relative urls (defaults to Dir.pwd)
  config.env                = Rails.env  # server env support, defaults to ENV['RACK_ENV'] or 'development'
  config.use_dm_validations = true       # validate attachment sizes and such, defaults to false
  config.processors_path    = 'lib/pc'   # relative path to look for processors, defaults to 'lib/paperclip_processors'
end

Yields:



57
58
59
60
# File 'lib/dm-paperclip.rb', line 57

def self.configure
  yield @config = Configuration.new
  Paperclip.config = @config
end

.included(base) ⇒ Object

:nodoc:



165
166
167
168
169
170
# File 'lib/dm-paperclip.rb', line 165

def included base #:nodoc:
  base.extend ClassMethods
  unless base.respond_to?(:define_callbacks)
    base.send(:include, Paperclip::CallbackCompatability)
  end
end

.interpolates(key, &block) ⇒ Object



133
134
135
# File 'lib/dm-paperclip.rb', line 133

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.



183
184
185
# File 'lib/dm-paperclip.rb', line 183

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

.loggerObject

:nodoc:



187
188
189
# File 'lib/dm-paperclip.rb', line 187

def logger #:nodoc:
  DataMapper.logger
end

.logging?Boolean

:nodoc:

Returns:

  • (Boolean)


191
192
193
# File 'lib/dm-paperclip.rb', line 191

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.



114
115
116
117
118
119
120
121
122
123
# File 'lib/dm-paperclip.rb', line 114

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

.path_for_command(command) ⇒ Object

:nodoc:



125
126
127
128
129
130
131
# File 'lib/dm-paperclip.rb', line 125

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:



172
173
174
175
176
177
178
179
# File 'lib/dm-paperclip.rb', line 172

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

.require_processorsObject



70
71
72
73
74
75
76
# File 'lib/dm-paperclip.rb', line 70

def self.require_processors
  return if @processors_already_required
  Dir.glob(File.expand_path("#{Paperclip.config.processors_path}/*.rb")).sort.each do |processor|
    require processor
  end
  @processors_already_required = true
end

.run(cmd, params = "", expected_outcodes = 0) ⇒ Object

The run method takes a command to execute and a string 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.

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.



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

def run cmd, params = "", expected_outcodes = 0
  command = %Q<#{%Q[#{path_for_command(cmd)} #{params}].gsub(/\s+/, " ")}>
  command = "#{command} 2>#{bit_bucket}" if Paperclip.options[:swallow_stderr]
  Paperclip.log(command) if Paperclip.options[:log_command]
  output = `#{command}`
  unless [expected_outcodes].flatten.include?($?.exitstatus)
    raise PaperclipCommandLineError, "Error while running #{cmd}"
  end
  output
end