Commit c46b40b9 by Jon Yurek

Don't assume we have Rails.env if we have Rails

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.
parent 0e39d750
...@@ -2,7 +2,7 @@ source "https://rubygems.org" ...@@ -2,7 +2,7 @@ source "https://rubygems.org"
gemspec gemspec
gem 'sqlite3', '1.3.8', :platforms => :ruby gem 'sqlite3', '~>1.3.8', :platforms => :ruby
gem 'jruby-openssl', :platforms => :jruby gem 'jruby-openssl', :platforms => :jruby
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
source "https://rubygems.org" source "https://rubygems.org"
gem "sqlite3", "1.3.8", :platforms => :ruby gem "sqlite3", "~>1.3.8", :platforms => :ruby
gem "jruby-openssl", :platforms => :jruby gem "jruby-openssl", :platforms => :jruby
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
gem "rubysl", :platforms => :rbx gem "rubysl", :platforms => :rbx
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
source "https://rubygems.org" source "https://rubygems.org"
gem "sqlite3", "1.3.8", :platforms => :ruby gem "sqlite3", "~>1.3.8", :platforms => :ruby
gem "jruby-openssl", :platforms => :jruby gem "jruby-openssl", :platforms => :jruby
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
gem "rubysl", :platforms => :rbx gem "rubysl", :platforms => :rbx
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
source "https://rubygems.org" source "https://rubygems.org"
gem "sqlite3", "1.3.8", :platforms => :ruby gem "sqlite3", "~>1.3.8", :platforms => :ruby
gem "jruby-openssl", :platforms => :jruby gem "jruby-openssl", :platforms => :jruby
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
gem "rubysl", :platforms => :rbx gem "rubysl", :platforms => :rbx
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
source "https://rubygems.org" source "https://rubygems.org"
gem "sqlite3", "1.3.8", :platforms => :ruby gem "sqlite3", "~>1.3.8", :platforms => :ruby
gem "jruby-openssl", :platforms => :jruby gem "jruby-openssl", :platforms => :jruby
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
gem "rubysl", :platforms => :rbx gem "rubysl", :platforms => :rbx
......
...@@ -55,6 +55,7 @@ require 'paperclip/helpers' ...@@ -55,6 +55,7 @@ require 'paperclip/helpers'
require 'paperclip/has_attached_file' require 'paperclip/has_attached_file'
require 'paperclip/attachment_registry' require 'paperclip/attachment_registry'
require 'paperclip/filename_cleaner' require 'paperclip/filename_cleaner'
require 'paperclip/rails_environment'
require 'mime/types' require 'mime/types'
require 'logger' require 'logger'
require 'cocaine' require 'cocaine'
......
module Paperclip
class RailsEnvironment
def self.get
new.get
end
def get
if rails_exists? && rails_environment_exists?
Rails.env
else
nil
end
end
private
def rails_exists?
Object.const_defined?("Rails")
end
def rails_environment_exists?
Rails.respond_to?(:env)
end
end
end
...@@ -156,8 +156,7 @@ module Paperclip ...@@ -156,8 +156,7 @@ module Paperclip
def parse_credentials(creds) def parse_credentials(creds)
creds = find_credentials(creds).stringify_keys creds = find_credentials(creds).stringify_keys
env = Object.const_defined?(:Rails) ? Rails.env : nil (creds[RailsEnvironment.get] || creds).symbolize_keys
(creds[env] || creds).symbolize_keys
end end
def copy_to_local_file(style, local_dest_path) def copy_to_local_file(style, local_dest_path)
......
...@@ -288,8 +288,7 @@ module Paperclip ...@@ -288,8 +288,7 @@ module Paperclip
def parse_credentials creds def parse_credentials creds
creds = creds.respond_to?('call') ? creds.call(self) : creds creds = creds.respond_to?('call') ? creds.call(self) : creds
creds = find_credentials(creds).stringify_keys creds = find_credentials(creds).stringify_keys
env = Object.const_defined?(:Rails) ? Rails.env : nil (creds[RailsEnvironment.get] || creds).symbolize_keys
(creds[env] || creds).symbolize_keys
end end
def exists?(style = default_style) def exists?(style = default_style)
......
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment