Make attachment_fu work with Rails 1.1.6 1

Posted by Rajesh Shetty on November 12, 2007

I was using acts_as_attachment plugin for file upload and I wanted to get the files to Amazon S3. Unfortunately acts_as_attachment does not do that and attachment_fu does (both of these great plugins are written by Rick Olsen) and attchment_fu needs Rails 1.2 and higher. I have a limitation of using Rails 1.1.6.  So made attachment_fu work with Rails 1.1.6. Not a whole lot of changes , should take less that 3 mins. So this is what you  do

  • Depending on what image processor you use (I use RMagick) open the respective image processor file. e.g. rmagick_processor.rb (under /lib/technoweenie/attachment_fu/processors). In self.included(base) method replace following line

         base.alias_method_chain :process_attachment, :processing

with

base.send :alias_method, :process_attachment_without_processing, :process_attachment
base.send :alias_method, :process_attachment, :process_attachment_with_processing

(The reason it needed Rails 1.2 in first place because of  base.alias_method_chain call which is introduced in Rails 1.2. So It was just a matter of replacing that with the Rails 1.1.6 equivalent). Similarly you can go do the same above change to other 2 processors as well (image_science and mini_magick)

  • Next thing (this one is nothing to do with Rails 1.2 issue, this issue was there in acts_as_attachment as well and I have covered that in one of my previous post’s) . open attachment_fu.rb and replace method
        def find_or_initialize_thumbnail(file_name_suffix)
          respond_to?(:parent_id) ?
            thumbnail_class.find_or_initialize_by_thumbnail_and_parent_id(file_name_suffix.to_s, id) :
            thumbnail_class.find_or_initialize_by_thumbnail(file_name_suffix.to_s)
        end

with

        def find_or_initialize_thumbnail(file_name_suffix)
          respond_to?(:parent_id) ?
            thumbnail_class.find_by_thumbnail_and_parent_id(file_name_suffix.to_s, id) || 
            thumbnail_class.new(:thumbnail=>file_name_suffix.to_s, :parent_id=>id) : 
            thumbnail_class.new(:thumbnail=>file_name_suffix.to_s)
        end

Thats it  you should be flyig with this change on Rails 1.1.6.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. […] This technique works with any alias_method_chain backward compatibility to older version of Rails. I have posted similar post for attachment_fu make it work with Rails 1.1.6 here […]

Comments