Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
replace deprecated RAILS_ENV and RAILS_ROOT with Rail.env and Rails.root
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Croak authored and Jon Yurek committed Apr 26, 2010
1 parent 4c6cf39 commit 933dc7b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 40 deletions.
4 changes: 2 additions & 2 deletions lib/paperclip.rb
Expand Up @@ -36,8 +36,8 @@
require 'paperclip/interpolations'
require 'paperclip/style'
require 'paperclip/attachment'
if defined? RAILS_ROOT
Dir.glob(File.join(File.expand_path(RAILS_ROOT), "lib", "paperclip_processors", "*.rb")).each do |processor|
if defined?(Rails.root) && Rails.root
Dir.glob(File.join(File.expand_path(Rails.root), "lib", "paperclip_processors", "*.rb")).each do |processor|
require processor
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/paperclip/interpolations.rb
Expand Up @@ -51,14 +51,14 @@ def timestamp attachment, style_name
attachment.instance_read(:updated_at).to_s
end

# Returns the RAILS_ROOT constant.
# Returns the Rails.root constant.
def rails_root attachment, style_name
RAILS_ROOT
Rails.root
end

# Returns the RAILS_ENV constant.
# Returns the Rails.env constant.
def rails_env attachment, style_name
RAILS_ENV
Rails.env
end

# Returns the underscored, pluralized version of the class name.
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/storage.rb
Expand Up @@ -174,7 +174,7 @@ def s3_host_alias

def parse_credentials creds
creds = find_credentials(creds).stringify_keys
(creds[RAILS_ENV] || creds).symbolize_keys
(creds[Rails.env] || creds).symbolize_keys
end

def exists?(style = default_style)
Expand Down
7 changes: 3 additions & 4 deletions test/attachment_test.rb
Expand Up @@ -11,7 +11,7 @@ class AttachmentTest < Test::Unit::TestCase
@model = @attachment.instance
@model.id = 1234
@model.avatar_file_name = "fake.jpg"
assert_equal "#{RAILS_ROOT}/public/fake_models/1234/fake", @attachment.path
assert_equal "#{Rails.root}/public/fake_models/1234/fake", @attachment.path
end

context "Attachment default_options" do
Expand Down Expand Up @@ -106,12 +106,11 @@ class AttachmentTest < Test::Unit::TestCase
@dummy.stubs(:id).returns(@id)
@file = StringIO.new(".")
@dummy.avatar = @file
Rails = stub('Rails', :root => @rails_env)
end

should "return the proper path" do
temporary_rails_env(@rails_env) {
assert_equal "#{@rails_env}/#{@id}.png", @dummy.avatar.path
}
assert_equal "#{@rails_env}/#{@id}.png", @dummy.avatar.path
end
end

Expand Down
30 changes: 16 additions & 14 deletions test/helper.rb
Expand Up @@ -16,9 +16,22 @@
puts "ruby-debug not loaded"
end

ROOT = File.join(File.dirname(__FILE__), '..')
RAILS_ROOT = ROOT
RAILS_ENV = "test"
ROOT = File.join(File.dirname(__FILE__), '..')

def silence_warnings
old_verbose, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = old_verbose
end

class Test::Unit::TestCase
def setup
silence_warnings do
Object.const_set(:Rails, stub('Rails', :root => ROOT, :env => 'test'))
end
end
end

$LOAD_PATH << File.join(ROOT, 'lib')
$LOAD_PATH << File.join(ROOT, 'lib', 'paperclip')
Expand Down Expand Up @@ -70,17 +83,6 @@ def rebuild_class options = {}
end
end

def temporary_rails_env(new_env)
old_env = Object.const_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

class FakeModel
attr_accessor :avatar_file_name,
:avatar_file_size,
Expand Down
8 changes: 4 additions & 4 deletions test/interpolations_test.rb
Expand Up @@ -11,12 +11,12 @@ class InterpolationsTest < Test::Unit::TestCase
end
end

should "return the RAILS_ROOT" do
assert_equal RAILS_ROOT, Paperclip::Interpolations.rails_root(:attachment, :style)
should "return the Rails.root" do
assert_equal Rails.root, Paperclip::Interpolations.rails_root(:attachment, :style)
end

should "return the RAILS_ENV" do
assert_equal RAILS_ENV, Paperclip::Interpolations.rails_env(:attachment, :style)
should "return the Rails.env" do
assert_equal Rails.env, Paperclip::Interpolations.rails_env(:attachment, :style)
end

should "return the class of the Interpolations module when called with no params" do
Expand Down
13 changes: 2 additions & 11 deletions test/storage_test.rb
Expand Up @@ -4,7 +4,7 @@
class StorageTest < Test::Unit::TestCase
def rails_env(env)
silence_warnings do
Object.const_set(:RAILS_ENV, env)
Object.const_set(:Rails, stub('Rails', :env => env))
end
end

Expand All @@ -17,12 +17,6 @@ def rails_env(env)

@dummy = Dummy.new
@avatar = @dummy.avatar

@current_env = RAILS_ENV
end

teardown do
rails_env(@current_env)
end

should "get the correct credentials when RAILS_ENV is production" do
Expand Down Expand Up @@ -96,7 +90,7 @@ def rails_env(env)
assert_match %r{^http://something.something.com/avatars/stringio.txt}, @dummy.avatar.url
end
end

context "Generating a url with an expiration" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
Expand Down Expand Up @@ -133,11 +127,8 @@ def rails_env(env)
:development => { :bucket => "dev_bucket" }
}
@dummy = Dummy.new
@old_env = RAILS_ENV
end

teardown{ rails_env(@old_env) }

should "get the right bucket in production" do
rails_env("production")
assert_equal "prod_bucket", @dummy.avatar.bucket_name
Expand Down

0 comments on commit 933dc7b

Please sign in to comment.