Rails3 and attachment_fu #30

Open
wants to merge 1 commit into from
View
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 = [
'image/jpeg',
'image/pjpeg',
@@ -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
end
end
@@ -201,19 +201,6 @@ def after_resize(&block)
write_inheritable_array(:after_resize, [block])
end
- # 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,15 +224,15 @@ 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 Tempfile.new(temp_base_name, Technoweenie::AttachmentFu.tempfile_path) do |tmp|
+ Tempfile.new(temp_base_name, Technoweenie::AttachmentFu.tempfile_path).tap do |tmp|
tmp.close
FileUtils.cp file, tmp.path
end
end
# Writes the given data to a new tempfile, returning the closed tempfile.
def write_to_temp_file(data, temp_base_name)
- returning Tempfile.new(temp_base_name, Technoweenie::AttachmentFu.tempfile_path) do |tmp|
+ Tempfile.new(temp_base_name, Technoweenie::AttachmentFu.tempfile_path).tap do |tmp|
tmp.binmode
tmp.write data
tmp.close
@@ -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(ThumbnailError.new("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)
end
# nil placeholder in case this field is used in a form.
@@ -349,7 +336,7 @@ def uploaded_data=(file_data)
file_data.rewind
set_temp_data file_data.read
else
- self.temp_paths.unshift file_data
+ self.temp_paths.unshift file_data.tempfile.path
end
end
@@ -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
save_to_storage
@temp_paths.clear
@saved_attachment = nil
- callback :after_attachment_saved
end
end
@@ -471,9 +457,13 @@ def resize_image_or_thumbnail!(img)
end
end
+ 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)
notify(method)
View
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
end
# 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')
end
# The attachment ID used in the full path of a file