Skip to content

Commit

Permalink
Handle nil Rails.application (#2)
Browse files Browse the repository at this point in the history
* Handle the `undefined method 'credentials' for nil:NilClass` error
when the rails gem is required but ContainerConfig is not being used
within the context of a Rails application.
  • Loading branch information
wheatevo authored Mar 15, 2021
1 parent edcfd43 commit 0958100
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased]

## [0.1.1] - 2021-03-15

- Handle `nil` `Rails.application`

## [0.1.0] - 2021-03-05

- Initial release
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
container_config (0.1.0)
container_config (0.1.1)

GEM
remote: https://rubygems.org/
Expand Down
2 changes: 1 addition & 1 deletion lib/container_config/provider/rails_credential.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def name
#
def load(key, *dig_keys, **options)
super
::Rails.application.credentials.config.dig(*dig_keys.map(&:to_sym))
::Rails.application&.credentials&.config&.dig(*dig_keys.map(&:to_sym))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/container_config/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module ContainerConfig
# ContainerConfig version
VERSION = "0.1.0"
VERSION = "0.1.1"
end
11 changes: 11 additions & 0 deletions spec/container_config/provider/rails_credential_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,16 @@
expect(subject.load("NOTHING", "nothing")).to eq(nil)
expect(subject.load("TEST_VAR", "test", "var")).to eq("from_creds")
end

context "when Rails.application is nil" do
before do
allow(Rails).to receive(:application).and_return(nil)
end

it "returns nil" do
expect(subject.load("NOTHING")).to eq(nil)
expect(subject.load("TEST_VAR")).to eq(nil)
end
end
end
end

0 comments on commit 0958100

Please sign in to comment.