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/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, PaperclipError, Processor, Railtie, StorageMethodNotFound, Style, Tempfile, Thumbnail
Constant Summary collapse
- VERSION =
"2.4.0"
Class Attribute Summary collapse
-
.classes_with_attachments ⇒ Object
Returns the value of attribute classes_with_attachments.
- .registered_attachments_styles_path ⇒ Object
Class Method Summary collapse
- .class_for(class_name) ⇒ Object
- .clear_processors! ⇒ Object
- .configure {|_self| ... } ⇒ Object
- .each_instance_with_attachment(klass, name) ⇒ Object
- .interpolates(key, &block) ⇒ Object
- .load_processor(name) ⇒ Object
-
.log(message) ⇒ Object
Log a paperclip-specific line.
-
.logger ⇒ Object
:nodoc:.
- .logger=(logger) ⇒ Object
-
.logging? ⇒ Boolean
:nodoc:.
-
.missing_attachments_styles ⇒ Object
Returns hash with styles missing from recent run of rake paperclip:refresh:missing_styles { :User => => [:big], :Book => { :cover => [:croppable]}, } }.
-
.options ⇒ Object
Provides configurability to Paperclip.
-
.processor(name) ⇒ Object
:nodoc:.
-
.register_processor(name, processor) ⇒ Object
You can add your own processor via the Paperclip configuration.
-
.run(cmd, *params) ⇒ Object
The run method takes a command to execute and an array of parameters that get passed to it.
- .save_current_attachments_styles! ⇒ Object
Class Attribute Details
.classes_with_attachments ⇒ Object
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 |
.registered_attachments_styles_path ⇒ Object
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
.class_for(class_name) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/paperclip.rb', line 163 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. =~ /is not missing constant/ raise NameError, "uninitialized constant #{class_name}" else raise e end end |
.clear_processors! ⇒ Object
122 123 124 |
# File 'lib/paperclip.rb', line 122 def clear_processors! @known_processors.try(:clear) end |
.configure {|_self| ... } ⇒ Object
72 73 74 |
# File 'lib/paperclip.rb', line 72 def configure yield(self) if block_given? end |
.each_instance_with_attachment(klass, name) ⇒ Object
139 140 141 142 143 |
# File 'lib/paperclip.rb', line 139 def (klass, name) class_for(klass).all.each do |instance| yield(instance) if instance.send(:"#{name}?") end end |
.interpolates(key, &block) ⇒ Object
76 77 78 |
# File 'lib/paperclip.rb', line 76 def interpolates key, &block Paperclip::Interpolations[key] = block end |
.load_processor(name) ⇒ Object
116 117 118 119 120 |
# File 'lib/paperclip.rb', line 116 def load_processor(name) if defined?(Rails.root) && Rails.root require File.(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.
147 148 149 |
# File 'lib/paperclip.rb', line 147 def log logger.info("[paperclip] #{}") if logging? end |
.logger ⇒ Object
:nodoc:
151 152 153 |
# File 'lib/paperclip.rb', line 151 def logger #:nodoc: @logger ||= [:logger] || Logger.new(STDOUT) end |
.logger=(logger) ⇒ Object
155 156 157 |
# File 'lib/paperclip.rb', line 155 def logger=(logger) @logger = logger end |
.logging? ⇒ Boolean
:nodoc:
159 160 161 |
# File 'lib/paperclip.rb', line 159 def logging? #:nodoc: [:log] end |
.missing_attachments_styles ⇒ Object
Returns hash with styles missing from recent run of rake paperclip:refresh:missing_styles
{
:User => {:avatar => [:big]},
:Book => {
:cover => [:croppable]},
}
}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/paperclip/missing_attachment_styles.rb', line 65 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 |
.options ⇒ Object
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.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/paperclip.rb', line 61 def @options ||= { :whiny => true, :image_magick_path => nil, :command_path => nil, :log => true, :log_command => true, :swallow_stderr => true } end |
.processor(name) ⇒ Object
:nodoc:
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/paperclip.rb', line 104 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
134 135 136 137 |
# File 'lib/paperclip.rb', line 134 def register_processor(name, processor) @known_processors ||= {} @known_processors[name.to_s] = 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.
96 97 98 99 100 101 102 |
# File 'lib/paperclip.rb', line 96 def run(cmd, *params) if [:image_magick_path] Paperclip.log("[DEPRECATION] :image_magick_path is deprecated and will be removed. Use :command_path instead") end Cocaine::CommandLine.path = [:command_path] || [:image_magick_path] Cocaine::CommandLine.new(cmd, *params).run end |