Module: Paperclip

Defined in:
lib/paperclip.rb,
lib/paperclip/style.rb,
lib/paperclip/upfile.rb,
lib/paperclip/railtie.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/storage/s3.rb,
lib/paperclip/storage/fog.rb,
lib/paperclip/interpolations.rb,
lib/paperclip/storage/filesystem.rb,
lib/paperclip/callback_compatibility.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, Glue, InstanceMethods, Interpolations, Shoulda, Storage, Upfile Classes: Attachment, CommandNotFoundError, Geometry, InfiniteInterpolationError, NotIdentifiedByImageMagickError, PaperclipError, Processor, Railtie, StorageMethodNotFound, Style, Tempfile, Thumbnail

Constant Summary collapse

VERSION =
"2.3.15"

Class Method Summary collapse

Class Method Details

.class_for(class_name) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/paperclip.rb', line 138

def class_for(class_name)
  # Ruby 1.9 introduces an inherit argument for Module#const_get and
  # #const_defined? and changes their default behavior.
  # https://github.com/rails/rails/blob/v3.0.9/activesupport/lib/active_support/inflector/methods.rb#L89
  if Module.method(:const_get).arity == 1
    class_name.split('::').inject(Object) do |klass, partial_class_name|
      klass.const_defined?(partial_class_name) ? klass.const_get(partial_class_name) : klass.const_missing(partial_class_name)
    end
  else
    class_name.split('::').inject(Object) do |klass, partial_class_name|
      klass.const_defined?(partial_class_name) ? klass.const_get(partial_class_name, false) : klass.const_missing(partial_class_name)
    end
  end
rescue ArgumentError => e
  # Sadly, we need to capture ArguementError here because Rails 2.3.x
  # Active Support dependency's management will try to the constant inherited
  # from Object, and fail misably with "Object is not missing constant X" error
  # https://github.com/rails/rails/blob/v2.3.12/activesupport/lib/active_support/dependencies.rb#L124
  if e.message =~ /is not missing constant/
    raise NameError, "uninitialized constant #{class_name}"
  else
    raise e
  end
end

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Paperclip)

    the object that the method was called on



70
71
72
# File 'lib/paperclip.rb', line 70

def configure
  yield(self) if block_given?
end

.each_instance_with_attachment(klass, name) ⇒ Object



118
119
120
121
122
# File 'lib/paperclip.rb', line 118

def each_instance_with_attachment(klass, name)
  class_for(klass).all.each do |instance|
    yield(instance) if instance.send(:"#{name}?")
  end
end

.interpolates(key, &block) ⇒ Object



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

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

.load_processor(name) ⇒ Object



112
113
114
115
116
# File 'lib/paperclip.rb', line 112

def load_processor(name)
  if defined?(Rails.root) && Rails.root
    require File.expand_path(Rails.root.join("lib", "paperclip_processors", "#{name.underscore}.rb"))
  end
end

.log(message) ⇒ Object

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



126
127
128
# File 'lib/paperclip.rb', line 126

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

.loggerObject

:nodoc:



130
131
132
# File 'lib/paperclip.rb', line 130

def logger #:nodoc:
  defined?(ActiveRecord::Base) ? ActiveRecord::Base.logger : Rails.logger
end

.logging?Boolean

:nodoc:

Returns:

  • (Boolean)


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

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.



59
60
61
62
63
64
65
66
67
68
# File 'lib/paperclip.rb', line 59

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

.processor(name) ⇒ Object

:nodoc:



102
103
104
105
106
107
108
109
110
# File 'lib/paperclip.rb', line 102

def processor name #:nodoc:
  name = name.to_s.camelize
  load_processor(name) unless Paperclip.const_defined?(name)
  processor = Paperclip.const_get(name)
  unless processor.ancestors.include?(Paperclip::Processor)
    raise PaperclipError.new("Processor #{name} was not found")
  end
  processor
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 Cocaine::CommandLineError 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.



94
95
96
97
98
99
100
# File 'lib/paperclip.rb', line 94

def run cmd, *params
  if options[:image_magick_path]
    Paperclip.log("[DEPRECATION] :image_magick_path is deprecated and will be removed. Use :command_path instead")
  end
  Cocaine::CommandLine.path = options[:command_path] || options[:image_magick_path]
  Cocaine::CommandLine.new(cmd, *params).run
end