This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Jon Yurek
committed
Jan 6, 2015
1 parent
0e39d75
commit 34ddd54
Showing
6 changed files
with
60 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |