Skip to content

Commit

Permalink
other fixes, but most importantly: Don't try to remove thumbnails if …
Browse files Browse the repository at this point in the history
…there aren't any. Closes technoweenie#3 [ben stiglitz]

git-svn-id: http://svn.techno-weenie.net/projects/plugins/attachment_fu@2830 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information
technoweenie committed Apr 2, 2007
1 parent 62d8d40 commit 1c14574
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 25 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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]
4 changes: 3 additions & 1 deletion README
Original file line number Diff line number Diff line change
@@ -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
37 changes: 24 additions & 13 deletions lib/technoweenie/attachment_fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,36 @@ 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
after_destroy :destroy_file
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
Expand All @@ -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

Expand Down Expand Up @@ -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
11 changes: 8 additions & 3 deletions lib/technoweenie/attachment_fu/backends/s3_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]
Expand Down
6 changes: 6 additions & 0 deletions test/amazon_s3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test:
bucket_name: afu
access_key_id: YOURACCESSKEY
secret_access_key: YOURSECRETACCESSKEY
server: 127.0.0.1
port: 3002
8 changes: 4 additions & 4 deletions test/backends/remote/s3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -123,4 +123,5 @@ class S3WithPathPrefixAttachment < S3Attachment
validates_as_attachment
end
rescue Technoweenie::AttachmentFu::Backends::S3Backend::ConfigFileNotFoundError
puts "S3 error: #{$!}"
end
4 changes: 2 additions & 2 deletions test/processors/image_science_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/ }
Expand All @@ -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
Expand Down
23 changes: 22 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 1c14574

Please sign in to comment.