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

Commit

Permalink
Don't assume we have Rails.env if we have Rails
Browse files Browse the repository at this point in the history
As referenced in #1739

Just because the `Rails` constant is defined, it doesn't mean we're
actually in a Rails app. Since there are people who use Paperclip
outside of Rails, and there's no reason we shouldn't be able to run in
those situations. This commit checks for `Rails.env` instead of just
checking for `Rails` and assuming `Rails.env` works.
  • Loading branch information
Jon Yurek committed Jan 6, 2015
1 parent 0e39d75 commit 34ddd54
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -2,7 +2,7 @@ source "https://rubygems.org"

gemspec

gem 'sqlite3', '1.3.8', :platforms => :ruby
gem 'sqlite3', '~>1.3.8', :platforms => :ruby

gem 'jruby-openssl', :platforms => :jruby
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
Expand Down
1 change: 1 addition & 0 deletions lib/paperclip.rb
Expand Up @@ -55,6 +55,7 @@
require 'paperclip/has_attached_file'
require 'paperclip/attachment_registry'
require 'paperclip/filename_cleaner'
require 'paperclip/rails_environment'
require 'mime/types'
require 'logger'
require 'cocaine'
Expand Down
23 changes: 23 additions & 0 deletions lib/paperclip/rails_environment.rb
@@ -0,0 +1,23 @@
module Paperclip
class RailsEnvironment
def self.get
new.get
end

def get
if rails_exists? && rails_environment_exists?
Rails.env
else
nil
end
end

def rails_exists?
Object.const_defined?("Rails")
end

def rails_environment_exists?
Rails.respond_to?(:env)
end
end
end
3 changes: 1 addition & 2 deletions lib/paperclip/storage/fog.rb
Expand Up @@ -156,8 +156,7 @@ def expiring_url(time = (Time.now + 3600), style_name = default_style)

def parse_credentials(creds)
creds = find_credentials(creds).stringify_keys
env = Object.const_defined?(:Rails) ? Rails.env : nil
(creds[env] || creds).symbolize_keys
(creds[RailsEnvironment.get] || creds).symbolize_keys
end

def copy_to_local_file(style, local_dest_path)
Expand Down
3 changes: 1 addition & 2 deletions lib/paperclip/storage/s3.rb
Expand Up @@ -288,8 +288,7 @@ def set_storage_class(storage_class)
def parse_credentials creds
creds = creds.respond_to?('call') ? creds.call(self) : creds
creds = find_credentials(creds).stringify_keys
env = Object.const_defined?(:Rails) ? Rails.env : nil
(creds[env] || creds).symbolize_keys
(creds[RailsEnvironment.get] || creds).symbolize_keys
end

def exists?(style = default_style)
Expand Down
33 changes: 33 additions & 0 deletions spec/paperclip/rails_environment_spec.rb
@@ -0,0 +1,33 @@
require 'spec_helper'

describe Paperclip::RailsEnvironment do

it "returns nil when Rails isn't defined" do
resetting_rails_to(nil) do
expect(Paperclip::RailsEnvironment.get).to be_nil
end
end

it "returns nil when Rails.env isn't defined" do
resetting_rails_to({}) do
expect(Paperclip::RailsEnvironment.get).to be_nil
end
end

it "returns the value of Rails.env if it is set" do
resetting_rails_to(OpenStruct.new(env: "foo")) do
expect(Paperclip::RailsEnvironment.get).to eq "foo"
end
end

def resetting_rails_to(new_value)
begin
previous_rails = Object.send(:remove_const, "Rails")
Object.const_set("Rails", new_value) unless new_value.nil?
yield
ensure
Object.send(:remove_const, "Rails") if Object.const_defined?("Rails")
Object.const_set("Rails", previous_rails)
end
end
end

0 comments on commit 34ddd54

Please sign in to comment.