Module: Paperclip

Defined in:
lib/paperclip.rb,
lib/paperclip/style.rb,
lib/paperclip/schema.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/url_generator.rb,
lib/paperclip/interpolations.rb,
lib/paperclip/attachment_options.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, Schema, Shoulda, Storage, Upfile Classes: Attachment, AttachmentOptions, CommandNotFoundError, Geometry, InfiniteInterpolationError, NotIdentifiedByImageMagickError, PaperclipError, Processor, Railtie, StorageMethodNotFound, Style, Tempfile, Thumbnail, UrlGenerator

Constant Summary collapse

VERSION =
"2.6.0"

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



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

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 && @names_url[name][:url] !~ /:class/
    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



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

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 ArgumentError here because Rails 2.3.x
  # ActiveSupport dependency management will try to the constant inherited
  # from Object, and fail miserably 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



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

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.



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

def each_instance_with_attachment(klass, name)
  unscope_method = class_for(klass).respond_to?(:unscoped) ? :unscoped : :with_exclusive_scope
  class_for(klass).send(unscope_method) do
    class_for(klass).find(:all, :order => 'id').each do |instance|
      yield(instance) if instance.send(:"#{name}?")
    end
  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



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

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.



153
154
155
# File 'lib/paperclip.rb', line 153

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

.loggerObject

:nodoc:



157
158
159
# File 'lib/paperclip.rb', line 157

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

.logger=(logger) ⇒ Object



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

def logger=(logger)
  @logger = logger
end

.logging?Boolean

:nodoc:

Returns:

  • (Boolean)


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

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. The options available are:

  • 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:



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

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


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

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

.reset_duplicate_clash_check!Object



203
204
205
# File 'lib/paperclip.rb', line 203

def reset_duplicate_clash_check!
  @names_url = nil
end

.run(cmd, arguments = "", local_options = {}) ⇒ Object

The run method takes the name of a binary to run, the arguments to that binary and some options:

:command_path -> A $PATH-like variable that defines where to look for the binary
                 on the filesystem. Colon-separated, just like $PATH.

:expected_outcodes -> An array of integers that defines the expected exit codes
                      of the binary. Defaults to [0].

:log_command -> Log the command being run when set to true (defaults to false).
                This will only log if logging in general is set to true as well.

:swallow_stderr -> Set to true if you don't care what happens on STDERR.


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

def run(cmd, arguments = "", local_options = {})
  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 )
  local_options = local_options.merge(:logger => logger) if logging? && (options[:log_command] || local_options[:log_command])
  Cocaine::CommandLine.new(cmd, arguments, local_options).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