Module: Paperclip
- Defined in:
- lib/paperclip.rb,
lib/paperclip/style.rb,
lib/paperclip/upfile.rb,
lib/paperclip/schema.rb,
lib/paperclip/railtie.rb,
lib/paperclip/version.rb,
lib/paperclip/geometry.rb,
lib/paperclip/matchers.rb,
lib/paperclip/thumbnail.rb,
lib/paperclip/processor.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/storage/filesystem.rb,
lib/paperclip/attachment_options.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
- VERSION =
"2.7.0"
Class Attribute Summary (collapse)
-
+ (Object) classes_with_attachments
Returns the value of attribute classes_with_attachments.
- + (Object) registered_attachments_styles_path
Class Method Summary (collapse)
- + (Object) check_for_url_clash(name, url, klass)
- + (Object) class_for(class_name)
- + (Object) clear_processors!
- + (Object) configure {|_self| ... }
-
+ (Object) current_attachments_styles
Returns hash with styles for all classes using Paperclip.
-
+ (Object) each_instance_with_attachment(klass, name)
Find all instances of the given Active Record model klass with attachment name.
-
+ (Object) get_registered_attachments_styles
Get list of styles saved on previous deploy (running rake paperclip:refresh:missing_styles).
- + (Object) interpolates(key, &block)
- + (Object) load_processor(name)
-
+ (Object) log(message)
Log a paperclip-specific line.
-
+ (Object) logger
:nodoc:.
- + (Object) logger=(logger)
-
+ (Boolean) logging?
:nodoc:.
-
+ (Object) missing_attachments_styles
Returns hash with styles missing from recent run of rake paperclip:refresh:missing_styles.
-
+ (Object) options
Provides configurability to Paperclip.
-
+ (Object) processor(name)
:nodoc:.
-
+ (Object) register_processor(name, processor)
You can add your own processor via the Paperclip configuration.
- + (Object) reset_duplicate_clash_check!
-
+ (Object) run(cmd, arguments = "", local_options = {})
The run method takes the name of a binary to run, the arguments to that binary and some options:.
- + (Object) save_current_attachments_styles!
Class Attribute Details
+ (Object) classes_with_attachments
Returns the value of attribute classes_with_attachments
6 7 8 |
# File 'lib/paperclip/missing_attachment_styles.rb', line 6 def @classes_with_attachments end |
+ (Object) registered_attachments_styles_path
8 9 10 |
# File 'lib/paperclip/missing_attachment_styles.rb', line 8 def @registered_attachments_styles_path ||= Rails.root.join('public/system/paperclip_attachments.yml').to_s end |
Class Method Details
+ (Object) check_for_url_clash(name, url, klass)
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.[: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 |
+ (Object) class_for(class_name)
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. =~ /is not missing constant/ raise NameError, "uninitialized constant #{class_name}" else raise e end end |
+ (Object) clear_processors!
123 124 125 |
# File 'lib/paperclip.rb', line 123 def clear_processors! @known_processors.try(:clear) end |
+ (Object) configure {|_self| ... }
73 74 75 |
# File 'lib/paperclip.rb', line 73 def configure yield(self) if block_given? end |
+ (Object) current_attachments_styles
Returns hash with styles for all classes using Paperclip. Unfortunately current version does not work with lambda styles:(
{
:User => {:avatar => [:small, :big]},
:Book => {
:cover => [:thumb, :croppable]},
:sample => [:thumb, :big]},
}
}
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/paperclip/missing_attachment_styles.rb', line 39 def self. Hash.new.tap do |current_styles| Paperclip..each do |klass_name| klass = Paperclip.class_for(klass_name) klass..each do |, | # TODO: is it even possible to take into account Procs? next if [:styles].kind_of?(Proc) [:styles].try(:keys).try(:each) do |style_name| klass_sym = klass.to_s.to_sym current_styles[klass_sym] ||= Hash.new current_styles[klass_sym][.to_sym] ||= Array.new current_styles[klass_sym][.to_sym] << style_name.to_sym current_styles[klass_sym][.to_sym].map!(&:to_s).sort!.map!(&:to_sym).uniq! end end end end end |
+ (Object) each_instance_with_attachment(klass, name)
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 (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 |
+ (Object) get_registered_attachments_styles
Get list of styles saved on previous deploy (running rake paperclip:refresh:missing_styles)
17 18 19 20 21 |
# File 'lib/paperclip/missing_attachment_styles.rb', line 17 def self. YAML.load_file(Paperclip.) rescue Errno::ENOENT nil end |
+ (Object) interpolates(key, &block)
77 78 79 |
# File 'lib/paperclip.rb', line 77 def interpolates key, &block Paperclip::Interpolations[key] = block end |
+ (Object) load_processor(name)
117 118 119 120 121 |
# File 'lib/paperclip.rb', line 117 def load_processor(name) if defined?(Rails.root) && Rails.root require File.(Rails.root.join("lib", "paperclip_processors", "#{name.underscore}.rb")) end end |
+ (Object) log(message)
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 logger.info("[paperclip] #{}") if logging? end |
+ (Object) logger
:nodoc:
157 158 159 |
# File 'lib/paperclip.rb', line 157 def logger #:nodoc: @logger ||= [:logger] || Logger.new(STDOUT) end |
+ (Object) logger=(logger)
161 162 163 |
# File 'lib/paperclip.rb', line 161 def logger=(logger) @logger = logger end |
+ (Boolean) logging?
:nodoc:
165 166 167 |
# File 'lib/paperclip.rb', line 165 def logging? #:nodoc: [:log] end |
+ (Object) missing_attachments_styles
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. current_styles = registered_styles = Hash.new.tap do |missing_styles| current_styles.each do |klass, | .each do |, styles| registered = registered_styles[klass][] || [] rescue [] missed = styles - registered if missed.present? klass_sym = klass.to_s.to_sym missing_styles[klass_sym] ||= Hash.new missing_styles[klass_sym][.to_sym] ||= Array.new missing_styles[klass_sym][.to_sym].concat(missed.to_a) missing_styles[klass_sym][.to_sym].map!(&:to_s).sort!.map!(&:to_sym).uniq! end end end end end |
+ (Object) options
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 ||= { :whiny => true, :image_magick_path => nil, :command_path => nil, :log => true, :log_command => true, :swallow_stderr => true } end |
+ (Object) processor(name)
: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 |
+ (Object) register_processor(name, processor)
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 |
+ (Object) reset_duplicate_clash_check!
203 204 205 |
# File 'lib/paperclip.rb', line 203 def reset_duplicate_clash_check! @names_url = nil end |
+ (Object) run(cmd, arguments = "", local_options = {})
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 = "", = {}) if [:image_magick_path] Paperclip.log("[DEPRECATION] :image_magick_path is deprecated and will be removed. Use :command_path instead") end command_path = [:command_path] || [:image_magick_path] Cocaine::CommandLine.path = ( Cocaine::CommandLine.path ? [Cocaine::CommandLine.path, command_path ].flatten : command_path ) = .merge(:logger => logger) if logging? && ([:log_command] || [:log_command]) Cocaine::CommandLine.new(cmd, arguments, ).run end |
+ (Object) save_current_attachments_styles!
24 25 26 27 28 |
# File 'lib/paperclip/missing_attachment_styles.rb', line 24 def self. File.open(Paperclip., 'w') do |f| YAML.dump(, f) end end |