Permalink
Browse files

Merge branch 'rewrite' into rewrite-s3

  • Loading branch information...
technoweenie committed Dec 1, 2008
2 parents 85afada + 4970a77 commit 09e31ee14797d7159d75089853ee429836c9ae49
@@ -12,8 +12,19 @@ def self.[](key)
# Base class for all Pixel-related tasks
class Task
def initialize(klass, options)
- options[:with] ||= :mojo_magick
- extend AttachmentFu::Pixels[options[:with]]
+ @pixel_adapter = options[:with] || klass.attachment_tasks.default_pixel_adapter
+ end
+
+ def with_image(adapter, attachment, &block)
+ AttachmentFu::Pixels[adapter || @pixel_adapter].with_image(attachment, &block)
+ end
+
+ def get_image_size(adapter, image)
+ AttachmentFu::Pixels[adapter || @pixel_adapter].get_image_size(image)
+ end
+
+ def resize_image(adapter, image, options = {})
+ AttachmentFu::Pixels[adapter || @pixel_adapter].resize_image(image, options)
end
end
@@ -3,6 +3,8 @@
module AttachmentFu # :nodoc:
module Pixels
module CoreImage
+ extend self
+
def with_image(attachment, &block)
block.call OSX::CIImage.from(attachment.full_path)
end
@@ -4,6 +4,8 @@
module AttachmentFu # :nodoc:
module Pixels
module MojoMagick
+ extend self
+
def with_image(attachment, &block)
block.call attachment.full_path
end
View
@@ -57,7 +57,7 @@ class Tasks
def self.all
@all ||= {}
end
-
+
# Gets a task by key. This loads a task's lib if given a path. This should replace the same
# task with a Class for future accesses.
def self.[](key)
@@ -69,7 +69,7 @@ def self.[](key)
else value
end
end
-
+
# Reference to the AttachmentFu model.
attr_reader :klass
@@ -79,13 +79,21 @@ def self.[](key)
# Index of task key => task instance.
attr_reader :all
-
+
+ # Default AttachmentFu::Pixels adapter
+ attr_reader :default_pixel_adapter
+
# Initializes the task for a single AttachmentFu model.
def initialize(klass, stack = [], all = {}, &block)
@klass, @stack, @all = klass, stack, all
+ set_pixel_adapter nil
instance_eval(&block) if block
end
-
+
+ def set_pixel_adapter(value)
+ @default_pixel_adapter = value || :mojo_magick
+ end
+
# Deletes all instances of the task this Tasks instance.
def delete(key)
if task = @all[key]
@@ -114,7 +122,9 @@ def any?(&block)
# Creates a copy of this Tasks instance.
def copy_for(klass, &block)
- self.class.new(klass, @stack.dup, @all.dup, &block)
+ copy = self.class.new(klass, @stack.dup, @all.dup, &block)
+ copy.set_pixel_adapter(@default_pixel_adapter)
+ copy
end
# Adds a new task to this Tasks instance. If the
@@ -2,7 +2,7 @@ module AttachmentFu
class Tasks
class Resize < AttachmentFu::Pixels::Task
def call(attachment, options)
- data = with_image(attachment) { |img| resize_image img, :size => options[:to], :to => options[:destination] }
+ data = with_image(options[:with], attachment) { |img| resize_image options[:with], img, :size => options[:to], :to => options[:destination] }
unless options[:skip_size]
attachment.width = data.width if attachment.respond_to?(:width)
attachment.height = data.height if attachment.respond_to?(:height)
@@ -12,7 +12,7 @@ def call(attachment, options)
class ImageSize < AttachmentFu::Pixels::Task
def call(attachment, options)
- attachment.width, attachment.height = with_image(attachment) { |img| get_image_size(img) }
+ attachment.width, attachment.height = with_image(options[:with], attachment) { |img| get_image_size(options[:with], img) }
end
end
end
@@ -15,6 +15,7 @@ def attachment_path_id
# :thumbnails_association => :thumbnails
#
def initialize(klass, options)
+ options[:with] ||= klass.attachment_tasks.default_pixel_adapter
options[:parent_association] ||= :parent
options[:parent_foreign_key] ||= options[:parent_association].to_s.foreign_key
options[:thumbnails_association] ||= :thumbnails
@@ -36,7 +37,7 @@ def initialize(klass, options)
end
unless klass.reflect_on_association(:parent)
- klass.belongs_to options[:parent_association], :class_name => "::#{@thumbnail_class.name}", :foreign_key => options[:parent_foreign_key]
+ klass.belongs_to options[:parent_association], :class_name => "::#{klass.name}", :foreign_key => options[:parent_foreign_key]
end
unless klass.reflect_on_association(:thumbnails)
View
@@ -39,6 +39,7 @@ def setup_spec_env
t.string :filename
t.string :content_type
t.string :thumbnail
+ t.string :type
end
end
@@ -18,33 +18,33 @@
end
it "gets accurate dimensions" do
- @pixels.with_image(@attachment) do |image|
+ @pixels.with_image(nil, @attachment) do |image|
image.extent.size.width.should == 80
image.extent.size.height.should == 75
end
end
it "resizes image with geometry string" do
- @pixels.with_image(@attachment) do |image|
- data = @pixels.resize_image image, :size => '40x40', :to => File.join(AttachmentFu.root_path, 'resized.jpg')
+ @pixels.with_image(nil, @attachment) do |image|
+ data = @pixels.resize_image nil, image, :size => '40x40', :to => File.join(AttachmentFu.root_path, 'resized.jpg')
data.width.should == 40
data.height.should == 38
data.size.should satisfy { |s| s > 0 }
end
end
it "resizes image with integer" do
- @pixels.with_image(@attachment) do |image|
- data = @pixels.resize_image image, :size => 40, :to => File.join(AttachmentFu.root_path, 'resized.jpg')
+ @pixels.with_image(nil, @attachment) do |image|
+ data = @pixels.resize_image nil, image, :size => 40, :to => File.join(AttachmentFu.root_path, 'resized.jpg')
data.width.should == 40
data.height.should == 37
data.size.should satisfy { |s| s > 0 }
end
end
it "resizes image with array" do
- @pixels.with_image(@attachment) do |image|
- data = @pixels.resize_image image, :size => [40, 40], :to => File.join(AttachmentFu.root_path, 'resized.jpg')
+ @pixels.with_image(nil, @attachment) do |image|
+ data = @pixels.resize_image nil, image, :size => [40, 40], :to => File.join(AttachmentFu.root_path, 'resized.jpg')
data.width.should == 40
data.height.should == 40
data.size.should satisfy { |s| s > 0 }
@@ -18,32 +18,32 @@
end
it "gets accurate dimensions" do
- @pixels.with_image(@attachment) do |image|
+ @pixels.with_image(nil, @attachment) do |image|
::MojoMagick.get_image_size(image).should == {:width => 80, :height => 75}
end
end
it "resizes image with geometry string" do
- @pixels.with_image(@attachment) do |image|
- data = @pixels.resize_image image, :size => '40x40', :to => File.join(AttachmentFu.root_path, 'resized.jpg')
+ @pixels.with_image(nil, @attachment) do |image|
+ data = @pixels.resize_image nil, image, :size => '40x40', :to => File.join(AttachmentFu.root_path, 'resized.jpg')
data.width.should == 40
data.height.should == 38
data.size.should satisfy { |s| s > 0 }
end
end
it "resizes image with integer" do
- @pixels.with_image(@attachment) do |image|
- data = @pixels.resize_image image, :size => 40, :to => File.join(AttachmentFu.root_path, 'resized.jpg')
+ @pixels.with_image(nil, @attachment) do |image|
+ data = @pixels.resize_image nil, image, :size => 40, :to => File.join(AttachmentFu.root_path, 'resized.jpg')
data.width.should == 40
data.height.should == 38
data.size.should satisfy { |s| s > 0 }
end
end
it "resizes image with array" do
- @pixels.with_image(@attachment) do |image|
- data = @pixels.resize_image image, :size => [40, 40], :to => File.join(AttachmentFu.root_path, 'resized.jpg')
+ @pixels.with_image(nil, @attachment) do |image|
+ data = @pixels.resize_image nil, image, :size => [40, 40], :to => File.join(AttachmentFu.root_path, 'resized.jpg')
data.width.should == 40
data.height.should == 38
data.size.should satisfy { |s| s > 0 }
@@ -19,6 +19,7 @@ class ThumbnailsTaskAsset < ActiveRecord::Base
@asset = ThumbnailsTaskAsset.new :content_type => 'image/jpg'
@asset.set_temp_path @sample
@asset.save!
+ @asset.reload
end
it "saves attachment" do
@@ -34,6 +35,10 @@ class ThumbnailsTaskAsset < ActiveRecord::Base
@asset.width.should == 80
end
+ it "sets the thumbnail parent" do
+ @asset.thumbnails.each { |th| th.parent.should == @asset }
+ end
+
it "resizes thumbnails to the resized width" do
@asset.thumbnails.each do |thumb|
thumb.width.should == (thumb.thumbnail == 'small' ? 40 : 10)
View
@@ -7,6 +7,7 @@ module AttachmentFu
:foo => FlakyTask,
:bar => lambda { |a, o| a.filename = "bar-#{o[:a]}-#{a.filename}" }
@tasks = Tasks.new self do
+ set_pixel_adapter :core_image
task :foo, :a => 1
task :bar, :a => 2
task :foo, :a => 3
@@ -42,7 +43,12 @@ module AttachmentFu
@copied.size.should == 4
@tasks.size.should == 3
end
-
+
+ it "copies pixel adapter" do
+ @copied = @tasks.copy_for(ProcessableAsset)
+ @copied.default_pixel_adapter.should == @tasks.default_pixel_adapter
+ end
+
it "allows copied tasks to be delete specific tasks" do
@copied = @tasks.copy_for ProcessableAsset do
delete :foo

0 comments on commit 09e31ee

Please sign in to comment.