Commit c8011b1f by Edgar Gonzalez Committed by Jon Yurek

Re-add lib/paperclip_processors for custom processors.

Also, add integration tests for custom processor definition and unit
test for Paperclip.load_processor (thoughtbot/paperclip#1535).
parent 82507cf7
...@@ -24,6 +24,21 @@ Feature: Rails integration ...@@ -24,6 +24,21 @@ Feature: Rails integration
And I should see an image with a path of "/paperclip/custom/attachments/original/5k.png" And I should see an image with a path of "/paperclip/custom/attachments/original/5k.png"
And the file at "/paperclip/custom/attachments/original/5k.png" should be the same as "spec/support/fixtures/5k.png" And the file at "/paperclip/custom/attachments/original/5k.png" should be the same as "spec/support/fixtures/5k.png"
Scenario: Add custom processors
Given I add a "test" processor in "lib/paperclip"
And I add a "cool" processor in "lib/paperclip_processors"
And I attach :attachment with:
"""
styles: { original: {} }, processors: [:test, :cool]
"""
And I start the rails application
When I go to the new user page
And I fill in "Name" with "something"
And I attach the file "spec/support/fixtures/5k.png" to "Attachment"
And I press "Submit"
Then I should see "Name: something"
And I should see an image with a path of "/paperclip/custom/attachments/original/5k.png"
Scenario: Filesystem integration test Scenario: Filesystem integration test
Given I attach :attachment with: Given I attach :attachment with:
""" """
......
...@@ -196,6 +196,35 @@ Given /^I am using Rails newer than ([\d\.]+)$/ do |version| ...@@ -196,6 +196,35 @@ Given /^I am using Rails newer than ([\d\.]+)$/ do |version|
end end
end end
Given(/^I add a "(.*?)" processor in "(.*?)"$/) do |processor, directory|
filename = "#{directory}/#{processor}.rb"
in_current_dir do
FileUtils.mkdir_p directory
File.open(filename, "w") do |f|
f.write(<<-CLASS)
module Paperclip
class #{processor.capitalize} < Processor
def make
basename = File.basename(file.path, File.extname(file.path))
dst_format = options[:format] ? ".\#{options[:format]}" : ''
dst = Tempfile.new([basename, dst_format])
dst.binmode
convert(':src :dst',
src: File.expand_path(file.path),
dst: File.expand_path(dst.path)
)
dst
end
end
end
CLASS
end
end
end
def transform_file(filename) def transform_file(filename)
if File.exist?(filename) if File.exist?(filename)
content = File.read(filename) content = File.read(filename)
......
...@@ -33,6 +33,7 @@ require 'paperclip/geometry_parser_factory' ...@@ -33,6 +33,7 @@ require 'paperclip/geometry_parser_factory'
require 'paperclip/geometry_detector_factory' require 'paperclip/geometry_detector_factory'
require 'paperclip/geometry' require 'paperclip/geometry'
require 'paperclip/processor' require 'paperclip/processor'
require 'paperclip/processor_helpers'
require 'paperclip/tempfile' require 'paperclip/tempfile'
require 'paperclip/thumbnail' require 'paperclip/thumbnail'
require 'paperclip/interpolations/plural_cache' require 'paperclip/interpolations/plural_cache'
......
...@@ -45,41 +45,4 @@ module Paperclip ...@@ -45,41 +45,4 @@ module Paperclip
Paperclip.run('identify', arguments, local_options) Paperclip.run('identify', arguments, local_options)
end end
end end
module ProcessorHelpers
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)
if defined?(Rails.root) && Rails.root
require File.expand_path(Rails.root.join("lib", "paperclip", "#{name.underscore}.rb"))
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
end
end end
module Paperclip
module ProcessorHelpers
class NoSuchProcessor < StandardError; end
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)
if defined?(Rails.root) && Rails.root
filename = "#{name.to_s.underscore}.rb"
directories = %w(lib/paperclip lib/paperclip_processors)
required = directories.map do |directory|
pathname = File.expand_path(Rails.root.join(directory, filename))
file_exists = File.exist?(pathname)
require pathname if file_exists
file_exists
end
raise LoadError, "Could not find the '#{name}' processor in any of these paths: #{directories.join(', ')}" unless required.any?
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
end
end
require 'spec_helper'
describe Paperclip::ProcessorHelpers do
describe '.load_processor' do
context 'when the file exists in lib/paperclip' do
it 'loads it correctly' do
pathname = Pathname.new('my_app')
main_path = 'main_path'
alternate_path = 'alternate_path'
Rails.stubs(:root).returns(pathname)
File.expects(:expand_path).with(pathname.join('lib/paperclip', 'custom.rb')).returns(main_path)
File.expects(:expand_path).with(pathname.join('lib/paperclip_processors', 'custom.rb')).returns(alternate_path)
File.expects(:exist?).with(main_path).returns(true)
File.expects(:exist?).with(alternate_path).returns(false)
Paperclip.expects(:require).with(main_path)
Paperclip.load_processor(:custom)
end
end
context 'when the file exists in lib/paperclip_processors' do
it 'loads it correctly' do
pathname = Pathname.new('my_app')
main_path = 'main_path'
alternate_path = 'alternate_path'
Rails.stubs(:root).returns(pathname)
File.expects(:expand_path).with(pathname.join('lib/paperclip', 'custom.rb')).returns(main_path)
File.expects(:expand_path).with(pathname.join('lib/paperclip_processors', 'custom.rb')).returns(alternate_path)
File.expects(:exist?).with(main_path).returns(false)
File.expects(:exist?).with(alternate_path).returns(true)
Paperclip.expects(:require).with(alternate_path)
Paperclip.load_processor(:custom)
end
end
context 'when the file does not exist in lib/paperclip_processors' do
it 'raises an error' do
pathname = Pathname.new('my_app')
main_path = 'main_path'
alternate_path = 'alternate_path'
Rails.stubs(:root).returns(pathname)
File.stubs(:expand_path).with(pathname.join('lib/paperclip', 'custom.rb')).returns(main_path)
File.stubs(:expand_path).with(pathname.join('lib/paperclip_processors', 'custom.rb')).returns(alternate_path)
File.stubs(:exist?).with(main_path).returns(false)
File.stubs(:exist?).with(alternate_path).returns(false)
assert_raises(LoadError) { Paperclip.processor(:custom) }
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment