Skip to content
Browse files

Also abstract out the string geometry parser from Thumbnail, to reduc…

…e coupling.
  • Loading branch information...
1 parent 3f7aee3 commit eebc7d98a7ecafc11ac3e221c159169d9aab7d10 @mike-burns mike-burns committed Aug 24, 2011
Showing with 29 additions and 7 deletions.
  1. +6 −5 lib/paperclip/thumbnail.rb
  2. +23 −2 test/thumbnail_test.rb
View
11 lib/paperclip/thumbnail.rb
@@ -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]
View
25 test/thumbnail_test.rb
@@ -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)
@@ -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
@@ -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

0 comments on commit eebc7d9

Please sign in to comment.
Something went wrong with that request. Please try again.