Skip to content
Browse files

Added :rails_env as a path/url interpolation variable. Useful for net…

…worked storage.
  • Loading branch information...
1 parent 8164a6f commit a1adaf31ff4644f1a6f1678425edaecf38266a61 Brian Tatnall committed Sep 5, 2008
Showing with 33 additions and 0 deletions.
  1. +1 −0 lib/paperclip/attachment.rb
  2. +20 −0 test/attachment_test.rb
  3. +12 −0 test/helper.rb
View
1 lib/paperclip/attachment.rb
@@ -161,6 +161,7 @@ def updated_at
def self.interpolations
@interpolations ||= {
:rails_root => lambda{|attachment,style| RAILS_ROOT },
+ :rails_env => lambda{|attachment,style| RAILS_ENV },
:class => lambda do |attachment,style|
attachment.instance.class.name.underscore.pluralize
end,
View
20 test/attachment_test.rb
@@ -86,6 +86,26 @@ class AttachmentTest < Test::Unit::TestCase
end
end
+ context "An attachment with a :rails_env interpolation" do
+ setup do
+ @rails_env = "blah"
+ @id = 1024
+ rebuild_model :path => ":rails_env/:id.png"
+ @dummy = Dummy.new
+ @dummy.stubs(:id).returns(@id)
+ @file = File.new(File.join(File.dirname(__FILE__),
+ "fixtures",
+ "5k.png"))
+ @dummy.avatar = @file
+ end
+
+ should "return the proper path" do
+ temporary_rails_env(@rails_env) {
+ assert_equal "#{@rails_env}/#{@id}.png", @dummy.avatar.path
+ }
+ end
+ end
+
context "Assigning an attachment" do
setup do
rebuild_model
View
12 test/helper.rb
@@ -5,6 +5,7 @@
require 'tempfile'
require 'active_record'
+require 'active_support'
begin
require 'ruby-debug'
rescue LoadError
@@ -43,3 +44,14 @@ def rebuild_model options = {}
has_attached_file :avatar, options
end
end
+
+def temporary_rails_env(new_env)
+ old_env = defined?(RAILS_ENV) ? RAILS_ENV : nil
+ silence_warnings do
+ Object.const_set("RAILS_ENV", new_env)
+ end
+ yield
+ silence_warnings do
+ Object.const_set("RAILS_ENV", old_env)
+ end
+end

0 comments on commit a1adaf3

Please sign in to comment.
Something went wrong with that request. Please try again.