Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Add a Paperclip.register_processor configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-burns committed Aug 30, 2011
1 parent 6ced9eb commit 630c634
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
34 changes: 28 additions & 6 deletions lib/paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,24 @@ def interpolates key, &block
# This method can log the command being run when
# Paperclip.options[:log_command] is set to true (defaults to false). This
# will only log if logging in general is set to true as well.
def run cmd, *params
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
Cocaine::CommandLine.path = options[:command_path] || options[:image_magick_path]
Cocaine::CommandLine.new(cmd, *params).run
end

def processor name #:nodoc:
name = name.to_s.camelize
load_processor(name) unless Paperclip.const_defined?(name)
processor = Paperclip.const_get(name)
processor
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

def load_processor(name)
Expand All @@ -113,6 +118,23 @@ def load_processor(name)
end
end

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

# 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
def register_processor(name, processor)
@known_processors ||= {}
@known_processors[name.to_s] = processor
end

def each_instance_with_attachment(klass, name)
class_for(klass).all.each do |instance|
yield(instance) if instance.send(:"#{name}?")
Expand Down
22 changes: 22 additions & 0 deletions test/paperclip_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,26 @@ def self.should_validate validation, options, valid_file, invalid_file
end

end

context "configuring a custom processor" do
setup do
@freedom_processor = Class.new do
def make(file, options = {}, attachment = nil)
file
end
end.new

Paperclip.configure do |config|
config.register_processor(:freedom, @freedom_processor)
end
end

should "be able to find the custom processor" do
assert_equal @freedom_processor, Paperclip.processor(:freedom)
end

teardown do
Paperclip.clear_processors!
end
end
end

0 comments on commit 630c634

Please sign in to comment.