Permalink
Browse files

Add config.base_url, use it as prefix in Uploader#url.

In Rails it defaults to ENV['RAILS_RELATIVE_URL_ROOT'] so that CarrierWave
now works nicely with rails applications deployed to subdirectories.
  • Loading branch information...
1 parent 392c91d commit 2870718fefedd8ba5665660279e6c4b95e7f6b44 @noniq noniq committed Sep 1, 2011
Showing with 16 additions and 2 deletions.
  1. +2 −1 lib/carrierwave.rb
  2. +2 −0 lib/carrierwave/uploader/configuration.rb
  3. +3 −1 lib/carrierwave/uploader/url.rb
  4. +9 −0 spec/uploader/url_spec.rb
View
@@ -15,7 +15,7 @@
module CarrierWave
class << self
- attr_accessor :root
+ attr_accessor :root, :base_url
def configure(&block)
CarrierWave::Uploader::Base.configure(&block)
@@ -92,6 +92,7 @@ module CarrierWave
class Railtie < Rails::Railtie
initializer "carrierwave.setup_paths" do
CarrierWave.root = Rails.root.join(Rails.public_path).to_s
+ CarrierWave.base_url = ENV['RAILS_RELATIVE_URL_ROOT']
end
initializer "carrierwave.active_record" do
@@ -8,6 +8,7 @@ module Configuration
class_attribute :_storage, :instance_writer => false
add_config :root
+ add_config :base_url
add_config :permissions
add_config :storage_engines
add_config :s3_access_policy
@@ -150,6 +151,7 @@ def reset_config
config.validate_integrity = true
config.validate_processing = true
config.root = CarrierWave.root
+ config.base_url = CarrierWave.base_url
config.enable_processing = true
config.ensure_multipart_form = true
end
@@ -3,6 +3,8 @@
module CarrierWave
module Uploader
module Url
+ extend ActiveSupport::Concern
+ include CarrierWave::Uploader::Configuration
##
# === Returns
@@ -13,7 +15,7 @@ def url
if file.respond_to?(:url) and not file.url.blank?
file.url
elsif current_path
- File.expand_path(current_path).gsub(File.expand_path(root), '')
+ (base_url || "") + File.expand_path(current_path).gsub(File.expand_path(root), '')
end
end
@@ -51,6 +51,15 @@
@uploader.url(:thumb, :mini).should == '/uploads/tmp/20071201-1234-345-2255/thumb_mini_test.jpg'
end
+ it "should prepend the config option 'base_url', if set" do
+ @uploader_class.version(:thumb)
+ @uploader.class.configure do |config|
+ config.base_url = "/base_url"
+ end
+ @uploader.cache!(File.open(file_path('test.jpg')))
+ @uploader.url(:thumb).should == '/base_url/uploads/tmp/20071201-1234-345-2255/thumb_test.jpg'
+ end
+
it "should return file#url if available" do
@uploader.cache!(File.open(file_path('test.jpg')))
@uploader.file.stub!(:url).and_return('http://www.example.com/someurl.jpg')

0 comments on commit 2870718

Please sign in to comment.