Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Rails3 and attachment_fu #30

wants to merge 1 commit into from

2 participants


hey technoweenie,

first I am thankful to you for developing attachment_fu in the first place.

I have a production project running on rails 2.3.8 with attachment_fu. I started upgrading to rails3.0.3 and run into a bunch of problems. at first I got a little scared and thought I should just switch my code to use something like paperclip or carrierwave. looking at those projects made me realize I would run into many more troubles trying to convert my code.

so I cast away all the fear and pulled up my sleeves. I made some minor changes to attachment_fu and made it compatible with the latest rails release.

maybe You want to pull this for other developers that may be walking the same path.

all the best and thanks again!


It doesn't seem to work on rails 3.0.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 6, 2010
  1. rails 3.0.3 compatible

    mihael authored
This page is out of date. Refresh to see the latest.
36 lib/technoweenie/attachment_fu.rb
@@ -1,7 +1,7 @@
module Technoweenie # :nodoc:
module AttachmentFu # :nodoc:
@@default_processors = %w(ImageScience Rmagick MiniMagick Gd2 CoreImage)
- @@tempfile_path = File.join(RAILS_ROOT, 'tmp', 'attachment_fu')
+ @@tempfile_path = Rails.root.join( 'tmp', 'attachment_fu')
@@content_types = [
@@ -184,7 +184,7 @@ def self.extended(base)
base.after_destroy :destroy_file
base.after_validation :process_attachment
if defined?(::ActiveSupport::Callbacks)
- base.define_callbacks :after_resize, :after_attachment_saved, :before_thumbnail_saved
+ base.define_callbacks :after_resize, :before_thumbnail_saved
@@ -201,19 +201,6 @@ def after_resize(&block)
write_inheritable_array(:after_resize, [block])
- # Callback after an attachment has been saved either to the file system or the DB.
- # Only called if the file has been changed, not necessarily if the record is updated.
- #
- # class Foo < ActiveRecord::Base
- # acts_as_attachment
- # after_attachment_saved do |record|
- # ...
- # end
- # end
- def after_attachment_saved(&block)
- write_inheritable_array(:after_attachment_saved, [block])
- end
# Callback before a thumbnail is saved. Use this to pass any necessary extra attributes that may be required.
# class Foo < ActiveRecord::Base
@@ -237,7 +224,7 @@ def thumbnail_class
# Copies the given file path to a new tempfile, returning the closed tempfile.
def copy_to_temp_file(file, temp_base_name)
- returning, Technoweenie::AttachmentFu.tempfile_path) do |tmp|
+, Technoweenie::AttachmentFu.tempfile_path).tap do |tmp|
FileUtils.cp file, tmp.path
@@ -245,7 +232,7 @@ def copy_to_temp_file(file, temp_base_name)
# Writes the given data to a new tempfile, returning the closed tempfile.
def write_to_temp_file(data, temp_base_name)
- returning, Technoweenie::AttachmentFu.tempfile_path) do |tmp|
+, Technoweenie::AttachmentFu.tempfile_path).tap do |tmp|
tmp.write data
@@ -288,7 +275,7 @@ def thumbnail_name_for(thumbnail = nil)
# Creates or updates the thumbnail for the current attachment.
def create_or_update_thumbnail(temp_file, file_name_suffix, *size)
thumbnailable? || raise("Can't create a thumbnail if the content type is not an image or there is no parent_id column"))
- returning find_or_initialize_thumbnail(file_name_suffix) do |thumb|
+ find_or_initialize_thumbnail(file_name_suffix).tap do |thumb|
thumb.temp_paths.unshift temp_file
thumb.send(:'attributes=', {
:content_type => content_type,
@@ -317,7 +304,7 @@ def image_size
# Returns true if the attachment data will be written to the storage system on the next save
def save_attachment?
- File.file?(temp_path.to_s)
+ File.file?(temp_path.to_filename)
# nil placeholder in case this field is used in a form.
@@ -349,7 +336,7 @@ def uploaded_data=(file_data)
- self.temp_paths.unshift file_data
+ self.temp_paths.unshift file_data.tempfile.path
@@ -409,7 +396,7 @@ def random_tempfile_filename
def sanitize_filename(filename)
return unless filename
- returning filename.strip do |name|
+ filename.strip.tap do |name|
# NOTE: File.basename doesn't work right with Windows paths on Unix
# get only the filename, not the whole path
name.gsub! /^.*(\\|\/)/, ''
@@ -458,7 +445,6 @@ def after_process_attachment
@saved_attachment = nil
- callback :after_attachment_saved
@@ -471,9 +457,13 @@ def resize_image_or_thumbnail!(img)
+ if defined?(Rails) && Rails::VERSION::MAJOR >= 3
+ def callback_with_args(method, arg = self)
+ send(method, arg) if respond_to?(method)
+ end
# Yanked from ActiveRecord::Callbacks, modified so I can pass args to the callbacks besides self.
# Only accept blocks, however
- if ActiveSupport.const_defined?(:Callbacks)
+ elsif ActiveSupport.const_defined?(:Callbacks)
# Rails 2.1 and beyond!
def callback_with_args(method, arg = self)
4 lib/technoweenie/attachment_fu/backends/file_system_backend.rb
@@ -20,12 +20,12 @@ def self.included(base) #:nodoc:
# The optional thumbnail argument will output the thumbnail's filename.
def full_filename(thumbnail = nil)
file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:path_prefix].to_s
- File.join(RAILS_ROOT, file_system_path, *partitioned_path(thumbnail_name_for(thumbnail)))
+ Rails.root.join( file_system_path, *partitioned_path(thumbnail_name_for(thumbnail))).to_s
# Used as the base path that #public_filename strips off full_filename to create the public path
def base_path
- @base_path ||= File.join(RAILS_ROOT, 'public')
+ @base_path ||= Rails.root.join('public')
# The attachment ID used in the full path of a file
Something went wrong with that request. Please try again.