Module: Paperclip

Defined in:
lib/paperclip.rb,
lib/paperclip/style.rb,
lib/paperclip/upfile.rb,
lib/paperclip/options.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/missing_attachment_styles.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, Options, PaperclipError, Processor, Railtie, StorageMethodNotFound, Style, Tempfile, Thumbnail

Constant Summary collapse

VERSION =
"2.4.3.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.classes_with_attachmentsObject

Returns the value of attribute classes_with_attachments.



6
7
8
# File 'lib/paperclip/missing_attachment_styles.rb', line 6

def classes_with_attachments
  @classes_with_attachments
end

.registered_attachments_styles_pathObject



8
9
10
# File 'lib/paperclip/missing_attachment_styles.rb', line 8

def registered_attachments_styles_path
  @registered_attachments_styles_path ||= Rails.root.join('public/system/paperclip_attachments.yml').to_s
end

Class Method Details

.check_for_url_clash(name, url, klass) ⇒ Object



192
193
194
195
196
197
198
199
# File 'lib/paperclip.rb', line 192

def check_for_url_clash(name,url,klass)
  @names_url ||= {}
  default_url = url || Attachment.default_options[:url]
  if @names_url[name] && @names_url[name][:url] == default_url && @names_url[name][:class] != klass
    log("Duplicate URL for #{name} with #{default_url}. This will clash with attachment defined in #{@names_url[name][:class]} class")
  end
  @names_url[name] = {:url => default_url, :class => klass}
end

.class_for(class_name) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/paperclip.rb', line 167

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

.clear_processors!Object



124
125
126
# File 'lib/paperclip.rb', line 124

def clear_processors!
  @known_processors.try(:clear)
end

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Paperclip)

    the object that the method was called on



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

def configure
  yield(self) if block_given?
end

.each_instance_with_attachment(klass, name) ⇒ Object

Find all instances of the given Active Record model klass with attachment name. This method is used by the refresh rake tasks.



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

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

.interpolates(key, &block) ⇒ Object



77
78
79
# File 'lib/paperclip.rb', line 77

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

.load_processor(name) ⇒ Object



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

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. This will logs to STDOUT by default. Set Paperclip.options to false to turn off.



151
152
153
# File 'lib/paperclip.rb', line 151

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

.loggerObject

:nodoc:



155
156
157
# File 'lib/paperclip.rb', line 155

def logger #:nodoc:
  @logger ||= options[:logger] || Logger.new(STDOUT)
end

.logger=(logger) ⇒ Object



159
160
161
# File 'lib/paperclip.rb', line 159

def logger=(logger)
  @logger = logger
end

.logging?Boolean

:nodoc:

Returns:

  • (Boolean)


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

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

.missing_attachments_stylesObject

Returns hash with styles missing from recent run of rake paperclip:refresh:missing_styles

{
  :User => {:avatar => [:big]},
  :Book => {
    :cover => [:croppable]},
  }
}


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/paperclip/missing_attachment_styles.rb', line 66

def self.missing_attachments_styles
  current_styles = current_attachments_styles
  registered_styles = get_registered_attachments_styles

  Hash.new.tap do |missing_styles|
    current_styles.each do |klass, attachment_definitions|
      attachment_definitions.each do |attachment_name, styles|
        registered = registered_styles[klass][attachment_name] rescue []
        missed = styles - registered 
        if missed.present?
          klass_sym = klass.to_s.to_sym
          missing_styles[klass_sym] ||= Hash.new
          missing_styles[klass_sym][attachment_name.to_sym] ||= Array.new
          missing_styles[klass_sym][attachment_name.to_sym].concat(missed.to_a)
          missing_styles[klass_sym][attachment_name.to_sym].map!(&:to_s).sort!.map!(&:to_sym).uniq!
        end
      end
    end  
  end
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.



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

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:



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/paperclip.rb', line 106

def processor(name) #:nodoc:
  @known_processors ||= {}
  if @known_processors[name.to_s]
    @known_processors[name.to_s]
  else
    name = name.to_s.camelize
    load_processor(name) unless Paperclip.const_defined?(name)
    processor = Paperclip.const_get(name)
    @known_processors[name.to_s] = processor
  end
end

.register_processor(name, processor) ⇒ Object

You can add your own processor via the Paperclip configuration. Normally Paperclip will load all processors from the Rails.root/lib/paperclip_processors directory, but here you can add any existing class using this mechanism.

Paperclip.configure do |c|
  c.register_processor :watermarker, WatermarkingProcessor.new
end


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

def register_processor(name, processor)
  @known_processors ||= {}
  @known_processors[name.to_s] = processor
end

.reset_duplicate_clash_check!Object



201
202
203
# File 'lib/paperclip.rb', line 201

def reset_duplicate_clash_check!
  @names_url = nil
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.



97
98
99
100
101
102
103
104
# File 'lib/paperclip.rb', line 97

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
  command_path = options[:command_path] || options[:image_magick_path]
  Cocaine::CommandLine.path = ( Cocaine::CommandLine.path ? [Cocaine::CommandLine.path, command_path ].flatten : command_path )
  Cocaine::CommandLine.new(cmd, *params).run
end

.save_current_attachments_styles!Object



24
25
26
27
28
# File 'lib/paperclip/missing_attachment_styles.rb', line 24

def self.save_current_attachments_styles!
  File.open(Paperclip.registered_attachments_styles_path, 'w') do |f|
    YAML.dump(current_attachments_styles, f)
  end
end