Skip to content

Commit

Permalink
Added :rails_env as a path/url interpolation variable. Useful for net…
Browse files Browse the repository at this point in the history
…worked storage.
  • Loading branch information
Brian Tatnall committed Sep 5, 2008
1 parent 8164a6f commit a1adaf3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions test/attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'tempfile'

require 'active_record'
require 'active_support'
begin
require 'ruby-debug'
rescue LoadError
Expand Down Expand Up @@ -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.