Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

other fixes, but most importantly: Don't try to remove thumbnails if …

…there aren't any. Closes #3 [ben stiglitz]

git-svn-id: http://svn.techno-weenie.net/projects/plugins/attachment_fu@2830 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information...
commit 1c14574906dcdcbfa445b94558a7e0789ed68850 1 parent 62d8d40
technoweenie authored
View
13 CHANGELOG
@@ -0,0 +1,13 @@
+* allow customization of the S3 configuration file path with the :s3_config_path option.
+* Don't try to remove thumbnails if there aren't any. Closes #3 [ben stiglitz]
+
+* BC * (before changelog)
+
+* add default #temp_paths entry [mattly]
+* add MiniMagick support to attachment_fu [Isacc]
+* update #destroy_file to clear out any empty directories too [carlivar]
+* fix references to S3Backend module [Hunter Hillegas]
+* make #current_data public with db_file and s3 backends [ebryn]
+* oops, actually svn add the files for s3 backend. [Jeffrey Hardy]
+* experimental s3 support, egad, no tests.... [Jeffrey Hardy]
+* doh, fix a few bad references to ActsAsAttachment [sixty4bit]
View
4 README
@@ -1,4 +1,6 @@
attachment-fu
=====================
-Acts as Attachment rewrite, come back later please!
+It works! Mike Clark writes a mean tutorial:
+
+http://clarkware.com/cgi/blosxom/2007/02/24#FileUploadFu
View
37 lib/technoweenie/attachment_fu.rb
@@ -44,23 +44,28 @@ def has_attachment(options = {})
options[:thumbnails] ||= {}
options[:thumbnail_class] ||= self
options[:s3_access] ||= :public_read
+ options[:content_type] = [options[:content_type]].flatten.collect! { |t| t == :image ? Technoweenie::AttachmentFu.content_types : t }.flatten unless options[:content_type].nil?
+
+ # doing these shenanigans so that #attachment_options is available to processors and backends
+ class_inheritable_accessor :attachment_options
+ self.attachment_options = options
# only need to define these once on a class
- unless included_modules.include? InstanceMethods
- class_inheritable_accessor :attachment_options
+ unless included_modules.include?(InstanceMethods)
attr_accessor :thumbnail_resize_options
- options[:storage] ||= (options[:file_system_path] || options[:path_prefix]) ? :file_system : :db_file
- options[:path_prefix] ||= options[:file_system_path]
- if options[:path_prefix].nil?
- options[:path_prefix] = options[:storage] == :s3 ? table_name : File.join("public", table_name)
+ attachment_options[:storage] ||= (attachment_options[:file_system_path] || attachment_options[:path_prefix]) ? :file_system : :db_file
+ attachment_options[:path_prefix] ||= attachment_options[:file_system_path]
+ if attachment_options[:path_prefix].nil?
+ attachment_options[:path_prefix] = attachment_options[:storage] == :s3 ? table_name : File.join("public", table_name)
end
- options[:path_prefix] = options[:path_prefix][1..-1] if options[:path_prefix].first == '/'
+ attachment_options[:path_prefix] = attachment_options[:path_prefix][1..-1] if options[:path_prefix].first == '/'
with_options :foreign_key => 'parent_id' do |m|
- m.has_many :thumbnails, :dependent => :destroy, :class_name => options[:thumbnail_class].to_s
+ m.has_many :thumbnails, :class_name => attachment_options[:thumbnail_class].to_s
m.belongs_to :parent, :class_name => base_class.to_s
end
+ before_destroy :destroy_thumbnails
before_validation :set_size_from_temp_path
after_save :after_process_attachment
@@ -68,7 +73,7 @@ def has_attachment(options = {})
extend ClassMethods
include InstanceMethods
include Technoweenie::AttachmentFu::Backends.const_get("#{options[:storage].to_s.classify}Backend")
- case options[:processor]
+ case attachment_options[:processor]
when :none
when nil
processors = Technoweenie::AttachmentFu.default_processors.dup
@@ -79,13 +84,14 @@ def has_attachment(options = {})
retry
end
else
- include Technoweenie::AttachmentFu::Processors.const_get("#{options[:processor].to_s.classify}Processor")
+ begin
+ include Technoweenie::AttachmentFu::Processors.const_get("#{options[:processor].to_s.classify}Processor")
+ rescue LoadError, MissingSourceFile
+ puts "Problems loading #{options[:processor]}Processor: #{$!}"
+ end
end
after_validation :process_attachment
end
-
- options[:content_type] = [options[:content_type]].flatten.collect { |t| t == :image ? Technoweenie::AttachmentFu.content_types : t }.flatten unless options[:content_type].nil?
- self.attachment_options = options
end
end
@@ -384,6 +390,11 @@ def callback_with_args(method, arg = self)
return result
end
+
+ # Removes the thumbnails for the attachment, if it has any
+ def destroy_thumbnails
+ self.thumbnails.each { |thumbnail| thumbnail.destroy } if thumbnailable?
+ end
end
end
end
View
11 lib/technoweenie/attachment_fu/backends/s3_backend.rb
@@ -34,6 +34,10 @@ module Backends
# access_key_id: <your key>
# secret_access_key: <your key>
#
+ # You can change the location of the config path by passing a full path to the :s3_config_path option.
+ #
+ # has_attachment :storage => :s3, :s3_config_path => (RAILS_ROOT + '/config/s3.yml')
+ #
# === Required configuration parameters
#
# * <tt>:access_key_id</tt> - The access key id for your S3 account. Provided by Amazon.
@@ -128,9 +132,10 @@ def self.included(base) #:nodoc:
end
begin
- @@s3_config = YAML.load_file(RAILS_ROOT + '/config/amazon_s3.yml')[ENV['RAILS_ENV']].symbolize_keys
- rescue
- raise ConfigFileNotFoundError.new('File RAILS_ROOT/config/amazon_s3.yml not found')
+ @@s3_config_path = base.attachment_options[:s3_config_path] || (RAILS_ROOT + '/config/amazon_s3.yml')
+ @@s3_config = YAML.load_file(@@s3_config_path)[ENV['RAILS_ENV']].symbolize_keys
+ #rescue
+ # raise ConfigFileNotFoundError.new('File %s not found' % @@s3_config_path)
end
@@bucket_name = s3_config[:bucket_name]
View
6 test/amazon_s3.yml
@@ -0,0 +1,6 @@
+test:
+ bucket_name: afu
+ access_key_id: YOURACCESSKEY
+ secret_access_key: YOURSECRETACCESSKEY
+ server: 127.0.0.1
+ port: 3002
View
8 test/backends/remote/s3_test.rb
@@ -2,7 +2,7 @@
require 'net/http'
class S3Test < Test::Unit::TestCase
- if File.exist?(RAILS_ROOT + '/config/amazon_s3.yml')
+ if File.exist?(File.join(File.dirname(__FILE__), '../../amazon_s3.yml'))
include BaseAttachmentTests
attachment_model S3Attachment
@@ -85,15 +85,15 @@ def http_response_for(url)
end
def s3_protocol
- Technoweenie::AttachmentFu::Backends::S3.protocol
+ Technoweenie::AttachmentFu::Backends::S3Backend.protocol
end
def s3_hostname
- Technoweenie::AttachmentFu::Backends::S3.hostname
+ Technoweenie::AttachmentFu::Backends::S3Backend.hostname
end
def s3_port_string
- Technoweenie::AttachmentFu::Backends::S3.port_string
+ Technoweenie::AttachmentFu::Backends::S3Backend.port_string
end
else
def test_flunk_s3
View
3  test/fixtures/attachment.rb
@@ -114,7 +114,7 @@ class MiniMagickAttachment < ActiveRecord::Base
begin
class S3Attachment < ActiveRecord::Base
- has_attachment :storage => :s3, :processor => :rmagick
+ has_attachment :storage => :s3, :processor => :rmagick, :s3_config_path => File.join(File.dirname(__FILE__), '../amazon_s3.yml')
validates_as_attachment
end
@@ -123,4 +123,5 @@ class S3WithPathPrefixAttachment < S3Attachment
validates_as_attachment
end
rescue Technoweenie::AttachmentFu::Backends::S3Backend::ConfigFileNotFoundError
+ puts "S3 error: #{$!}"
end
View
4 test/processors/image_science_test.rb
@@ -9,7 +9,7 @@ def test_should_resize_image
assert_valid attachment
assert attachment.image?
# test image science thumbnail
- assert_equal 43, attachment.width
+ assert_equal 42, attachment.width
assert_equal 55, attachment.height
thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
@@ -21,7 +21,7 @@ def test_should_resize_image
# test geometry string
assert_equal 31, geo.width
- assert_equal 40, geo.height
+ assert_equal 41, geo.height
end
else
def test_flunk
View
23 test/test_helper.rb
@@ -10,7 +10,28 @@
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
-ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
+
+db_adapter = ENV['DB']
+
+# no db passed, try one of these fine config-free DBs before bombing.
+db_adapter ||=
+ begin
+ require 'rubygems'
+ require 'sqlite'
+ 'sqlite'
+ rescue MissingSourceFile
+ begin
+ require 'sqlite3'
+ 'sqlite3'
+ rescue MissingSourceFile
+ end
+ end
+
+if db_adapter.nil?
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
+end
+
+ActiveRecord::Base.establish_connection(config[db_adapter])
load(File.dirname(__FILE__) + "/schema.rb")
Please sign in to comment.
Something went wrong with that request. Please try again.