Browse files

make attachment uploads optional

  • Loading branch information...
1 parent b29b8d4 commit 1c2366399b9212c9ad4cab816ba827cfc9accaa4 @technoweenie committed Nov 28, 2009
Showing with 42 additions and 4 deletions.
  1. +11 −3 lib/attachment_fu.rb
  2. +31 −1 spec/attachment_fu_spec.rb
View
14 lib/attachment_fu.rb
@@ -196,12 +196,17 @@ def partitioned_path(*args)
("%08d" % attachment_path_id).scan(/..../) + args
end
+ def has_attachment?
+ !(attachment_path_id.blank? || filename.blank?)
+ end
+
def public_path(thumbnail = nil)
+ return nil if !has_attachment?
File.join(attachment_path, *partitioned_path(thumbnailed_filename(thumbnail))).sub(/^public\//, '/')
end
def full_path(thumbnail = nil)
- return nil if attachment_path_id.nil?
+ return nil if !has_attachment?
File.expand_path(File.join(AttachmentFu.root_path, attachment_path, *partitioned_path(thumbnailed_filename(thumbnail))))
end
@@ -331,8 +336,10 @@ def check_task_progress(stack)
# Deletes the attachment from the file system, and attempts to clean up
# the empty asset paths.
def delete_attachment
- FileUtils.rm full_path if File.exist?(full_path)
- dir_name = File.dirname(full_path)
+ fp = full_path
+ return if fp.blank?
+ FileUtils.rm fp if File.exist?(fp)
+ dir_name = File.dirname(fp)
default = %w(. ..)
while dir_name != AttachmentFu.root_path
dir_exists = File.exists?(dir_name)
@@ -401,6 +408,7 @@ def strip_filename(value)
# Needed to tell the difference between an attachment that has just been saved,
# vs one saved in a previous request or object instantiation.
def set_new_attachment
+ return true if @temp_path.blank?
@old_asset_path = full_path_for(@temp_path)
if @old_asset_path.blank? then raise AssetMissing.new('...') end
@old_asset_path = File.expand_path(@old_asset_path)
View
32 spec/attachment_fu_spec.rb
@@ -57,7 +57,37 @@ class QueuedAsset < ActiveRecord::Base
@asset.should be_queued
end
end
-
+
+ describe "being created without attachment" do
+ before :all do
+ @assets = []
+ @assets << (@asset = BasicAsset.create!)
+ end
+
+ after :all do
+ @assets.each(&:destroy)
+ end
+
+ it "accepts attachment in future operation" do
+ file = File.join(File.dirname(__FILE__), 'guinea_pig.rb')
+ FileUtils.cp __FILE__, file
+
+ @assets << (asset = BasicAsset.create!)
+ asset.content_type = 'application/x-ruby'
+ asset.set_temp_path file
+ asset.save!
+ asset.full_path.should == File.expand_path(File.join(AttachmentFu.root_path, "public/afu_spec_assets/#{asset.partitioned_path * '/'}/guinea_pig.rb"))
+ end
+
+ it "has no full_path" do
+ @asset.full_path.should == nil
+ end
+
+ it "has no public_path" do
+ @asset.public_path.should == nil
+ end
+ end
+
describe "being created" do
before :all do
@file = File.join(File.dirname(__FILE__), 'guinea_pig.rb')

0 comments on commit 1c23663

Please sign in to comment.