Module: Paperclip

Extended by:
Helpers, Logger, ProcessorHelpers
Defined in:
lib/paperclip.rb,
lib/paperclip/glue.rb,
lib/paperclip/style.rb,
lib/paperclip/errors.rb,
lib/paperclip/logger.rb,
lib/paperclip/schema.rb,
lib/paperclip/helpers.rb,
lib/paperclip/railtie.rb,
lib/paperclip/version.rb,
lib/paperclip/geometry.rb,
lib/paperclip/matchers.rb,
lib/paperclip/tempfile.rb,
lib/paperclip/callbacks.rb,
lib/paperclip/processor.rb,
lib/paperclip/thumbnail.rb,
lib/paperclip/attachment.rb,
lib/paperclip/storage/s3.rb,
lib/paperclip/validators.rb,
lib/paperclip/storage/fog.rb,
lib/paperclip/url_generator.rb,
lib/paperclip/interpolations.rb,
lib/paperclip/filename_cleaner.rb,
lib/paperclip/tempfile_factory.rb,
lib/paperclip/has_attached_file.rb,
lib/paperclip/processor_helpers.rb,
lib/paperclip/rails_environment.rb,
lib/paperclip/storage/filesystem.rb,
lib/paperclip/attachment_registry.rb,
lib/paperclip/io_adapters/registry.rb,
lib/paperclip/content_type_detector.rb,
lib/paperclip/geometry_parser_factory.rb,
lib/paperclip/io_adapters/nil_adapter.rb,
lib/paperclip/io_adapters/uri_adapter.rb,
lib/paperclip/io_adapters/file_adapter.rb,
lib/paperclip/geometry_detector_factory.rb,
lib/paperclip/media_type_spoof_detector.rb,
lib/paperclip/missing_attachment_styles.rb,
lib/paperclip/interpolations/plural_cache.rb,
lib/paperclip/io_adapters/abstract_adapter.rb,
lib/paperclip/io_adapters/data_uri_adapter.rb,
lib/paperclip/io_adapters/identity_adapter.rb,
lib/paperclip/io_adapters/stringio_adapter.rb,
lib/paperclip/io_adapters/attachment_adapter.rb,
lib/paperclip/io_adapters/empty_string_adapter.rb,
lib/paperclip/io_adapters/uploaded_file_adapter.rb,
lib/paperclip/file_command_content_type_detector.rb,
lib/paperclip/io_adapters/http_url_proxy_adapter.rb,
lib/paperclip/matchers/have_attached_file_matcher.rb,
lib/paperclip/validators/attachment_size_validator.rb,
lib/paperclip/validators/attachment_presence_validator.rb,
lib/paperclip/matchers/validate_attachment_size_matcher.rb,
lib/paperclip/validators/attachment_file_name_validator.rb,
lib/paperclip/validators/attachment_content_type_validator.rb,
lib/paperclip/matchers/validate_attachment_presence_matcher.rb,
lib/paperclip/validators/media_type_spoof_detection_validator.rb,
lib/paperclip/matchers/validate_attachment_content_type_matcher.rb,
lib/paperclip/validators/attachment_file_type_ignorance_validator.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: Callbacks, ClassMethods, Errors, Glue, Helpers, Interpolations, Logger, ProcessorHelpers, Schema, Shoulda, Storage, TempfileEncoding, Validators Classes: AbstractAdapter, AdapterRegistry, Attachment, AttachmentAdapter, AttachmentRegistry, ContentTypeDetector, DataUriAdapter, EmptyStringAdapter, Error, FileAdapter, FileCommandContentTypeDetector, FilenameCleaner, Geometry, GeometryDetector, GeometryParser, HasAttachedFile, HttpUrlProxyAdapter, IdentityAdapter, MediaTypeSpoofDetector, NilAdapter, Processor, RailsEnvironment, Railtie, StringioAdapter, Style, Tempfile, TempfileFactory, Thumbnail, UploadedFileAdapter, UriAdapter, UrlGenerator

Constant Summary collapse

AVAILABLE_BACKENDS =
[:image_magick, :vips].freeze
VERSION =
"8.0.6"
REQUIRED_VALIDATORS =
[AttachmentFileNameValidator, AttachmentContentTypeValidator, AttachmentFileTypeIgnoranceValidator].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Helpers

class_for, configure, each_instance_with_attachment, imagemagick7?, interpolates, reset_duplicate_clash_check!, run, which

Methods included from Logger

log, logger, logger=, logging?

Methods included from ProcessorHelpers

clear_processors!, load_processor, processor, register_processor

Class Attribute Details

.registered_attachments_styles_pathObject



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

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

Class Method Details

.block_untrusted_vips_loadersObject



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

def self.block_untrusted_vips_loaders
  return if ENV["VIPS_BLOCK_UNTRUSTED"]
  return unless defined?(::Vips) && ::Vips.respond_to?(:block_untrusted)

  ::Vips.block_untrusted(true)
end

.io_adaptersObject



140
141
142
# File 'lib/paperclip.rb', line 140

def self.io_adapters
  @io_adapters ||= Paperclip::AdapterRegistry.new
end

.io_adapters=(new_registry) ⇒ Object



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

def self.io_adapters=(new_registry)
  @io_adapters = new_registry
end

.missing_attachments_stylesObject

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

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


62
63
64
65
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 62

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 = begin
                       registered_styles[klass][attachment_name] || []
                     rescue StandardError
                       []
                     end
        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.

  • use_exif_orientation: Whether to inspect EXIF data to determine an image’s orientation. Defaults to true.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/paperclip.rb', line 120

def self.options
  @options ||= {
    command_path: nil,
    content_type_mappings: {},
    log: true,
    log_command: true,
    read_timeout: nil,
    swallow_stderr: true,
    use_exif_orientation: true,
    whiny: true,
    is_windows: Gem.win_platform?,
    add_validation_errors_to: :both,
    backend: :image_magick
  }
end

.require_vipsObject



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

def self.require_vips
  return if @vips_loaded

  require "vips"
  block_untrusted_vips_loaders
  @vips_loaded = true
rescue LoadError
  raise Errors::CommandNotFoundError.new("Could not load ruby-vips. Please install libvips.")
end

.resolve_backend(backend) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/paperclip.rb', line 84

def self.resolve_backend(backend)
  backend ||= :image_magick
  unless AVAILABLE_BACKENDS.include?(backend)
    log("Warning: Invalid backend: #{backend}. Falling back to :image_magick. Allowed backends: #{AVAILABLE_BACKENDS.join(', ')}")
    backend = :image_magick
  end
  backend
end

.save_current_attachments_styles!Object



22
23
24
25
26
# File 'lib/paperclip/missing_attachment_styles.rb', line 22

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