0
require File.join(File.dirname(__FILE__), 'attachment_fu', 'backends')
0
+require File.join(File.dirname(__FILE__), 'attachment_fu', 'processors')
0
module Technoweenie # :nodoc:
0
module AttachmentFu # :nodoc:
0
@@ -46,20 +47,22 @@ module Technoweenie # :nodoc:
0
unless included_modules.include? InstanceMethods
0
class_inheritable_accessor :attachment_options
0
+ options[:processor] ||= :rmagick
0
options[:storage] ||= options[:file_system_path] ? :file_system : :db_file
0
options[:file_system_path] ||= File.join("public", table_name)
0
options[:file_system_path] = options[:file_system_path][1..-1] if options[:file_system_path].first == '/'
0
- ##with_options :foreign_key => 'parent_id' do |m|
0
- ## m.has_many :thumbnails, :dependent => :destroy, :class_name => options[:thumbnail_class].to_s
0
- ## m.belongs_to :parent, :class_name => self.base_class.to_s
0
- ##after_save :create_attachment_thumbnails # allows thumbnails with parent_id to be created
0
+ with_options :foreign_key => 'parent_id' do |m|
0
+ m.has_many :thumbnails, :dependent => :destroy, :class_name => options[:thumbnail_class].to_s
0
+ m.belongs_to :parent, :class_name => base_class.to_s
0
after_destroy :destroy_file
0
- include InstanceMethods, Technoweenie::AttachmentFu::const_get("#{options[:storage].to_s.classify}Backend")
0
+ include InstanceMethods
0
+ include Technoweenie::AttachmentFu::const_get("#{options[:storage].to_s.classify}Backend")
0
+ include Technoweenie::AttachmentFu::const_get("#{options[:processor].to_s.classify}Processor")
0
+ before_save :process_attachment
0
options[:content_type] = [options[:content_type]].flatten.collect { |t| t == :image ? Technoweenie::ActsAsAttachment.content_types : t }.flatten unless options[:content_type].nil?
0
@@ -81,6 +84,18 @@ module Technoweenie # :nodoc:
0
content_types.include?(content_type)
0
+ # Callback after an image has been resized.
0
+ # class Foo < ActiveRecord::Base
0
+ # after_resize do |record, img|
0
+ # record.aspect_ratio = img.columns.to_f / img.rows.to_f
0
+ def after_resize(&block)
0
+ write_inheritable_array(:after_resize, [block])
0
# Callback after an attachment has been saved either to the file system or the DB.
0
# Only called if the file has been changed, not necessarily if the record is updated.
0
@@ -93,6 +108,25 @@ module Technoweenie # :nodoc:
0
def after_attachment_saved(&block)
0
write_inheritable_array(:after_attachment_saved, [block])
0
+ # Callback before a thumbnail is saved. Use this to pass any necessary extra attributes that may be required.
0
+ # class Foo < ActiveRecord::Base
0
+ # before_thumbnail_saved do |record, thumbnail|
0
+ def before_thumbnail_saved(&block)
0
+ write_inheritable_array(:before_thumbnail_saved, [block])
0
+ # Get the thumbnail class, which is the current attachment class by default.
0
+ # Configure this with the :thumbnail_class option.
0
+ attachment_options[:thumbnail_class] = attachment_options[:thumbnail_class].constantize unless attachment_options[:thumbnail_class].is_a?(Class)
0
+ attachment_options[:thumbnail_class]
0
@@ -152,7 +186,6 @@ module Technoweenie # :nodoc:
0
@save_attachment = false
0
- return nil if data.nil?
0
self.size = data.length
0
@@ -160,6 +193,26 @@ module Technoweenie # :nodoc:
0
@attachment_data = data
0
+ # sets a temporary location to the asset. Use this if the file is already on the local file system
0
+ # and if you do not need to load it into memory.
0
+ def attachment_file=(file)
0
+ @attachment_file = nil
0
+ @save_attachment = false
0
+ if file && File.file?(file)
0
+ file_stat = File.stat(file)
0
+ self.size = file_stat.size
0
+ @save_attachment = true
0
+ @attachment_file = file
0
+ # Retrieve the temporary attachment file data if it exists, or return nil
0
+ def attachment_file_data
0
+ (@attachment_file && File.file?(@attachment_file)) ? File.read(@attachment_file) : nil
0
# Sets the content type.
0
def content_type=(new_type)
0
@@ -171,6 +224,11 @@ module Technoweenie # :nodoc:
0
write_attribute :filename, sanitize_filename(new_name)
0
+ # Returns the width/height in a suitable format for the image_tag helper: (100x100)
0
+ [width.to_s, height.to_s] * 'x'
0
@@filename_basename_regex = /^.*(\\|\/)/
0
@@filename_character_regex = /[^\w\.\-]/
0
@@ -193,6 +251,9 @@ module Technoweenie # :nodoc:
0
+ # Stub for a #process_attachment method in a processor
0
+ def process_attachment() end
0
# Yanked from ActiveRecord::Callbacks, modified so I can pass args to the callbacks besides self.
0
# Only accept blocks, however
0
def callback_with_args(method, arg = self)
Comments
No one has commented yet.