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

Commit

Permalink
Also abstract out the string geometry parser from Thumbnail, to reduc…
Browse files Browse the repository at this point in the history
…e coupling.
  • Loading branch information
mike-burns committed Aug 24, 2011
1 parent 3f7aee3 commit eebc7d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
11 changes: 6 additions & 5 deletions lib/paperclip/thumbnail.rb
Expand Up @@ -18,19 +18,20 @@ class Thumbnail < Processor
# Options include:
#
# +geometry+ - the desired width and height of the thumbnail
# +file_geometry_parser+ - an object with a method named +from_file+ that takes an image file and produces its geometry. Defaults to Paperclip::Geometry
# +file_geometry_parser+ - an object with a method named +from_file+ that takes an image file and produces its geometry and a +transformation_to+. Defaults to Paperclip::Geometry
# +string_geometry_parser+ - an object with a method named +parse+ that takes a string and produces an object with +width+, +height+, and +to_s+ accessors. Defaults to Paperclip::Geometry
# +source_file_options+ - flags passed to the +convert+ command that influence how the source file is read
# +convert_options+ - flags passed to the +convert+ command that influence how the image is processed
# +whiny+ - whether to raise an error when processing fails. Defaults to true.
# +whiny+ - whether to raise an error when processing fails. Defaults to true
# +format+ - the desired filename extension
# +animated+ - whether to merge all the layers in the image. Defaults to true.
# +animated+ - whether to merge all the layers in the image. Defaults to true
def initialize(file, options = {}, attachment = nil)
super

geometry = options[:geometry]
geometry = options[:geometry] # this is not an option
@file = file
@crop = geometry[-1,1] == '#'
@target_geometry = Geometry.parse(geometry)
@target_geometry = (options[:string_geometry_parser] || Geometry).parse(geometry)
@current_geometry = (options[:file_geometry_parser] || Geometry).from_file(@file)
@source_file_options = options[:source_file_options]
@convert_options = options[:convert_options]
Expand Down
25 changes: 23 additions & 2 deletions test/thumbnail_test.rb
Expand Up @@ -225,7 +225,7 @@ class ThumbnailTest < Test::Unit::TestCase
end
end

context "passing a custom geometry parser" do
context "passing a custom file geometry parser" do
should "produce the appropriate transformation_command" do
GeoParser = Class.new do
def self.from_file(file)
Expand All @@ -236,7 +236,7 @@ def transformation_to(target, should_crop)
end
end

thumb = Paperclip::Thumbnail.new(@file, :geometry => "50x50", :file_geometry_parser => GeoParser)
thumb = Paperclip::Thumbnail.new(@file, :geometry => '50x50', :file_geometry_parser => GeoParser)

transformation_command = thumb.transformation_command

Expand All @@ -250,6 +250,27 @@ def transformation_to(target, should_crop)
%{expected #{transformation_command.inspect} to include '"SCALE"'}
end
end

context "passing a custom geometry string parser" do
should "produce the appropriate transformation_command" do
GeoParser = Class.new do
def self.parse(s)
new
end

def to_s
"151x167"
end
end

thumb = Paperclip::Thumbnail.new(@file, :geometry => '50x50', :string_geometry_parser => GeoParser)

transformation_command = thumb.transformation_command

assert transformation_command.include?('"151x167"'),
%{expected #{transformation_command.inspect} to include '151x167'}
end
end
end

context "A multipage PDF" do
Expand Down

0 comments on commit eebc7d9

Please sign in to comment.