Added default_url, which will replace default_path

This commit is contained in:
Jonas Nicklas 2009-09-01 22:55:40 +02:00
parent cbd5bbf6f7
commit 8f31e10727
6 changed files with 112 additions and 5 deletions

View File

@ -189,15 +189,15 @@ CarrierWave from writing to the file system by setting
`CarrierWave.config[:cache_to_cache_dir] = false`. This will however break
redisplays of forms.
== Providing a default path
== Providing a default URL
In many cases, especially when working with images, it might be a good idea to
provide a default path, a fallback in case no file has been uploaded. You can do
this easily by overriding the +default_path+ method in your uploader:
provide a default url, a fallback in case no file has been uploaded. You can do
this easily by overriding the +default_url+ method in your uploader:
class MyUploader < CarrierWave::Uploader::Base
def default_path
"images/fallback/" + [version_name, "default.png"].compact.join('_')
def default_url
"/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
end

View File

@ -64,6 +64,7 @@ module CarrierWave
autoload :Paths, 'carrierwave/uploader/paths'
autoload :ExtensionWhitelist, 'carrierwave/uploader/extension_whitelist'
autoload :DefaultPath, 'carrierwave/uploader/default_path'
autoload :DefaultUrl, 'carrierwave/uploader/default_url'
autoload :Proxy, 'carrierwave/uploader/proxy'
autoload :Url, 'carrierwave/uploader/url'
autoload :Mountable, 'carrierwave/uploader/mountable'

View File

@ -37,6 +37,7 @@ module CarrierWave
use CarrierWave::Uploader::DefaultPath
use CarrierWave::Uploader::Processing
use CarrierWave::Uploader::Versions
use CarrierWave::Uploader::DefaultUrl
end # Base
end # Uploader

View File

@ -7,6 +7,7 @@ module CarrierWave
def initialize(*args)
super
if default_path
puts "WARNING: Default Path is deprecated and will be removed in CarrierWave 0.4. Please use default_url instead!"
@file = CarrierWave::SanitizedFile.new(File.expand_path(default_path, public))
def @file.blank?; true; end
end

View File

@ -0,0 +1,19 @@
# encoding: utf-8
module CarrierWave
module Uploader
module DefaultUrl
def url(*args)
super || default_url
end
##
# Override this method in your uploader to provide a default url
# in case no file has been cached/stored yet.
#
def default_url; end
end # DefaultPath
end # Uploader
end # CarrierWave

View File

@ -0,0 +1,85 @@
# encoding: utf-8
require File.dirname(__FILE__) + '/../spec_helper'
describe CarrierWave::Uploader do
before do
@uploader_class = Class.new(CarrierWave::Uploader::Base)
@uploader = @uploader_class.new
end
after do
FileUtils.rm_rf(public_path)
end
describe 'with a default url' do
before do
@uploader_class.class_eval do
version :thumb
def default_url
["http://someurl.example.com", version_name].compact.join('/')
end
end
@uploader = @uploader_class.new
end
describe '#blank?' do
it "should be true by default" do
@uploader.should be_blank
end
end
describe '#current_path' do
it "should return nil" do
@uploader.current_path.should be_nil
end
end
describe '#url' do
it "should return the default url" do
@uploader.url.should == 'http://someurl.example.com'
end
it "should return the default url with version when given" do
@uploader.url(:thumb).should == 'http://someurl.example.com/thumb'
end
end
describe '#cache!' do
before do
CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
end
it "should cache a file" do
@uploader.cache!(File.open(file_path('test.jpg')))
@uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
end
it "should be cached" do
@uploader.cache!(File.open(file_path('test.jpg')))
@uploader.should be_cached
end
it "should no longer be blank" do
@uploader.cache!(File.open(file_path('test.jpg')))
@uploader.should_not be_blank
end
it "should set the current_path" do
@uploader.cache!(File.open(file_path('test.jpg')))
@uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
end
it "should set the url" do
@uploader.cache!(File.open(file_path('test.jpg')))
@uploader.url.should_not == 'http://someurl.example.com'
@uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
end
end
end
end