Permalink
Browse files

move the thumbnail class preparation later in the processing cycle

  • Loading branch information...
1 parent 0ce3dd0 commit d7ec1e388691eb08c10459e5363fa79279b7277e @technoweenie committed Dec 9, 2008
Showing with 43 additions and 39 deletions.
  1. +43 −39 lib/attachment_fu/tasks/thumbnails.rb
@@ -1,7 +1,7 @@
module AttachmentFu
class Tasks
class Thumbnails
- attr_reader :options
+ attr_reader :options, :klass
attr_accessor :thumbnail_class
# Some valid options:
@@ -32,6 +32,7 @@ def initialize(klass, options)
@options[:parent_association] ||= :parent
@options[:parent_foreign_key] ||= @options[:parent_association].to_s.foreign_key
@options[:thumbnails_association] ||= :thumbnails
+ @thumbnail_class_processed = false
@klass.class_eval do
# ensure that the get_image_size task is at the top
@@ -41,53 +42,19 @@ def initialize(klass, options)
prepend :get_image_size, :with => options[:with]
end
- # Set the given class as the thumbnail class for the current attachment class
- def self.attachment_thumbnail_class(klass)
- th_task = attachment_tasks[:thumbnails]
- if th_task.thumbnail_class ; raise ArgumentError, "#{name} already has a thumbnail class: #{th_task.thumbnail_class.name}, not #{klass.name}" ; end
- th_task.thumbnail_class = klass
-
- # create thumbnails association
- unless reflect_on_association(:thumbnails)
- has_many th_task.options[:thumbnails_association], :class_name => "::#{klass.base_class.name}", :foreign_key => th_task.options[:parent_foreign_key], :dependent => :destroy
- end
-
- # modify a thumbnail class to be used as the thumbnail for this attachment
- klass.class_eval do
- # The attachment ID used in the full path of a file
- def attachment_path_id
- parent_id
- end
-
- unless reflect_on_association(:parent)
- belongs_to th_task.options[:parent_association], :class_name => "::#{klass.base_class.name}", :foreign_key => th_task.options[:parent_foreign_key]
- end
-
- validates_presence_of th_task.options[:parent_foreign_key]
-
- # ensure that the thumbnails task is not carried over to the thumbnail class,
- # and also ensure that get_image_size is the first task.
- attachment_tasks do
- load :resize
- unqueue :thumbnails, :get_image_size
- prepend :get_image_size, :with => th_task.options[:with]
- end
- end
- klass
- end
-
def self.inherited(klass)
- attachment_thumbnail_class(klass)
+ if th_task = attachment_tasks[:thumbnails]
+ th_task.thumbnail_class ||= klass
+ end
super
end
end
- @klass.attachment_thumbnail_class(@options[:thumbnail_class]) if @options.key?(:thumbnail_class)
end
# task :thumbnails, :sizes => {:thumb => '50x50', :tiny => [10, 10]}
#
def call(attachment, options)
- @thumbnail_class ||= @klass.const_set(:Thumbnail, Class.new(@klass))
+ assign_thumbnail_class_to_attachment_class unless @thumbnail_class_processed
options[:sizes].each do |name, size|
thumb_name = thumbnail_name_for(attachment, name)
@@ -103,6 +70,43 @@ def call(attachment, options)
end
end
+ # Set the given class as the thumbnail class for the current attachment class
+ def assign_thumbnail_class_to_attachment_class
+ @thumbnail_class = @options[:thumbnail_class] || @klass.const_set(:Thumbnail, Class.new(@klass))
+ if @thumbnail_class.is_a?(String) ; @thumbnail_class = @thumbnail_class.constantize; end
+ th_task = self
+
+ # create thumbnails association
+ @klass.class_eval do
+ unless reflect_on_association(:thumbnails)
+ has_many th_task.options[:thumbnails_association], :class_name => "::#{base_class.name}", :foreign_key => th_task.options[:parent_foreign_key], :dependent => :destroy
+ end
+ end
+
+ # modify a thumbnail class to be used as the thumbnail for this attachment
+ @thumbnail_class.class_eval do
+ # The attachment ID used in the full path of a file
+ def attachment_path_id
+ parent_id
+ end
+
+ unless reflect_on_association(:parent)
+ belongs_to th_task.options[:parent_association], :class_name => "::#{th_task.klass.base_class.name}", :foreign_key => th_task.options[:parent_foreign_key]
+ end
+
+ validates_presence_of th_task.options[:parent_foreign_key]
+
+ # ensure that the thumbnails task is not carried over to the thumbnail class,
+ # and also ensure that get_image_size is the first task.
+ attachment_tasks do
+ load :resize
+ unqueue :thumbnails, :get_image_size
+ prepend :get_image_size, :with => th_task.options[:with]
+ end
+ end
+ @thumbnail_class_processed = true
+ end
+
def thumbnail_name_for(attachment, thumbnail = nil)
return attachment.filename if thumbnail.blank?
ext = nil

0 comments on commit d7ec1e3

Please sign in to comment.