Skip to content

Commit

Permalink
make attachment uploads optional
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Nov 28, 2009
1 parent b29b8d4 commit 1c23663
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
14 changes: 11 additions & 3 deletions lib/attachment_fu.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -196,12 +196,17 @@ def partitioned_path(*args)
("%08d" % attachment_path_id).scan(/..../) + args ("%08d" % attachment_path_id).scan(/..../) + args
end end


def has_attachment?
!(attachment_path_id.blank? || filename.blank?)
end

def public_path(thumbnail = nil) def public_path(thumbnail = nil)
return nil if !has_attachment?
File.join(attachment_path, *partitioned_path(thumbnailed_filename(thumbnail))).sub(/^public\//, '/') File.join(attachment_path, *partitioned_path(thumbnailed_filename(thumbnail))).sub(/^public\//, '/')
end end


def full_path(thumbnail = nil) 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)))) File.expand_path(File.join(AttachmentFu.root_path, attachment_path, *partitioned_path(thumbnailed_filename(thumbnail))))
end end


Expand Down Expand Up @@ -331,8 +336,10 @@ def check_task_progress(stack)
# Deletes the attachment from the file system, and attempts to clean up # Deletes the attachment from the file system, and attempts to clean up
# the empty asset paths. # the empty asset paths.
def delete_attachment def delete_attachment
FileUtils.rm full_path if File.exist?(full_path) fp = full_path
dir_name = File.dirname(full_path) return if fp.blank?
FileUtils.rm fp if File.exist?(fp)
dir_name = File.dirname(fp)
default = %w(. ..) default = %w(. ..)
while dir_name != AttachmentFu.root_path while dir_name != AttachmentFu.root_path
dir_exists = File.exists?(dir_name) dir_exists = File.exists?(dir_name)
Expand Down Expand Up @@ -401,6 +408,7 @@ def strip_filename(value)
# Needed to tell the difference between an attachment that has just been saved, # Needed to tell the difference between an attachment that has just been saved,
# vs one saved in a previous request or object instantiation. # vs one saved in a previous request or object instantiation.
def set_new_attachment def set_new_attachment
return true if @temp_path.blank?
@old_asset_path = full_path_for(@temp_path) @old_asset_path = full_path_for(@temp_path)
if @old_asset_path.blank? then raise AssetMissing.new('...') end if @old_asset_path.blank? then raise AssetMissing.new('...') end
@old_asset_path = File.expand_path(@old_asset_path) @old_asset_path = File.expand_path(@old_asset_path)
Expand Down
32 changes: 31 additions & 1 deletion spec/attachment_fu_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -57,7 +57,37 @@ class QueuedAsset < ActiveRecord::Base
@asset.should be_queued @asset.should be_queued
end end
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 describe "being created" do
before :all do before :all do
@file = File.join(File.dirname(__FILE__), 'guinea_pig.rb') @file = File.join(File.dirname(__FILE__), 'guinea_pig.rb')
Expand Down

0 comments on commit 1c23663

Please sign in to comment.