reorganizations

This commit is contained in:
Jonas Nicklas 2008-10-08 15:26:53 -04:00
parent b1fc51b67e
commit f583921759
3 changed files with 43 additions and 35 deletions

View File

@ -19,8 +19,9 @@ if defined?(Merb::Plugins)
# Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it
Merb::Plugins.config[:merb_upload] = {
:storage => :file,
:use_cache => true,
:store_dir => Merb.root / 'public' / 'uploads',
:tmp_dir => Merb.root / 'public' / 'uploads' / 'tmp',
:cache_dir => Merb.root / 'public' / 'uploads' / 'tmp',
:storage_engines => {
:file => Merb::Upload::Storage::File
}

View File

@ -21,30 +21,6 @@ module Merb
end
end
def cache!(identifier, file)
uploader = self.new(identifier)
uploader.cache!(file)
uploader
end
def retrieve_from_cache!(identifier)
uploader = self.new(identifier)
uploader.retrieve_from_cache!
uploader
end
def store!(identifier, file)
uploader = self.new(identifier)
uploader.store!(file)
uploader
end
def retrieve_from_store!(identifier)
uploader = self.new(identifier)
uploader.retrieve_from_store!
uploader
end
def storage(storage = nil)
if storage.is_a?(Symbol)
@storage = Merb::Plugins.config[:merb_upload][:storage_engines][storage]
@ -69,6 +45,10 @@ module Merb
def initialize(identifier)
@identifier = identifier
end
def current_path
file ? file.path : store_path
end
def filename
identifier
@ -78,33 +58,53 @@ module Merb
Merb::Plugins.config[:merb_upload][:store_dir]
end
def tmp_dir
Merb::Plugins.config[:merb_upload][:tmp_dir]
def cache_dir
Merb::Plugins.config[:merb_upload][:cache_dir]
end
def store_path
store_dir / filename
end
def tmp_path
tmp_dir / filename
def cache_path
cache_dir / filename
end
def cache(new_file)
cache!(new_file) unless file
end
def cache!(new_file)
new_file = Merb::Upload::SanitizedFile.new(new_file)
raise Merb::Upload::FormNotMultipart, "check that your upload form is multipart encoded" if new_file.string?
@file = new_file
@file.move_to(tmp_path)
@file.move_to(cache_path)
process!
end
def retrieve_from_cache
retrieve_from_cache! unless file
end
def retrieve_from_cache!
@file = Merb::Upload::SanitizedFile.new(tmp_path)
@file = Merb::Upload::SanitizedFile.new(cache_path)
end
def store(new_file=nil)
store!(new_file) unless file
end
def store!(new_file=nil)
cache!(new_file) if new_file
@file = storage.store!(@file)
if Merb::Plugins.config[:merb_upload][:use_cache]
cache!(new_file) if new_file
@file = storage.store!(@file)
else
@file = storage.store!(Merb::Upload::SanitizedFile.new(new_file))
end
end
def retrieve_from_store
retrieve_from_store! unless file
end
def retrieve_from_store!

View File

@ -75,9 +75,9 @@ describe Merb::Upload::Uploader do
end
end
describe '#tmp_dir' do
describe '#cache_dir' do
it "should default to the config option" do
@uploader.tmp_dir.should == Merb.root / 'public' / 'uploads' / 'tmp'
@uploader.cache_dir.should == Merb.root / 'public' / 'uploads' / 'tmp'
end
end
@ -130,6 +130,13 @@ describe Merb::Upload::Uploader do
@uploader.store!(@file)
end
it "should, if a files is given as an argument and use_cache is false, not cache that file" do
Merb::Plugins.config[:merb_upload][:use_cache] = false
@uploader.should_not_receive(:cache!)
@uploader.store!(@file)
Merb::Plugins.config[:merb_upload][:use_cache] = true
end
it "should use a previously cached file if no argument is given" do
@uploader.should_not_receive(:cache!)
@uploader.store!