Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom file location #178

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,27 @@ Pusher.key = Figaro.env.pusher_key!
Pusher.secret = Figaro.env.pusher_secret!
```

### Custom file location

Say your deployment process require you to have your configuration at a certain file path or that you want to provide defaults for different platforms in development.
You can achieve this by using an environment variable to tell Figaro where to look for your yaml file.

```bash
$ docker run -e FIGARO_PATH=./config/application.docker.yml my-container rails s
```

```bash
$ FIGARO_PATH=/storage_device/configuration/my_app_variables.yml rails s
```

If you're not using rails or if you want to determine the file path by other means, set the `Figaro.adapter.path` to your desired value.

```ruby
require "figaro"
Figaro.adapter.path = "/my/config/file.yml"
Figaro.load
```

### Deployment

Figaro is written with deployment in mind. In fact, [Heroku](https://www.heroku.com)'s use of `ENV` for application configuration was the original inspiration for Figaro.
Expand Down
6 changes: 5 additions & 1 deletion lib/figaro/rails/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ class Application < Figaro::Application
def default_path
rails_not_initialized! unless ::Rails.root

::Rails.root.join("config", "application.yml")
if ::ENV["FIGARO_PATH"]
::Rails.root.join(::ENV["FIGARO_PATH"])
else
::Rails.root.join("config", "application.yml")
end
end

def default_environment
Expand Down
22 changes: 20 additions & 2 deletions spec/figaro/rails/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,34 @@ module Rails
describe "#default_path" do
let!(:application) { Application.new }

it "defaults to config/application.yml in Rails.root" do
allow(::Rails).to receive(:root) { Pathname.new("/path/to/app") }
before { allow(::Rails).to receive(:root) { Pathname.new("/path/to/app") } }

it "defaults to config/application.yml in Rails.root" do
expect {
allow(::Rails).to receive(:root) { Pathname.new("/app") }
}.to change {
application.send(:default_path).to_s
}.from("/path/to/app/config/application.yml").to("/app/config/application.yml")
end

it "defaults to path relative to Rails.root from the FIGARO_PATH environment variable" do
expect {
::ENV["FIGARO_PATH"] = "./config/application.docker.yml"
}.to change {
application.send(:default_path).to_s
}.from("/path/to/app/config/application.yml").to("/path/to/app/config/application.docker.yml")
::ENV["FIGARO_PATH"] = nil
end

it "defaults to absolute path from the FIGARO_PATH environment variable" do
expect {
::ENV["FIGARO_PATH"] = "/var/config/my-app.yml"
}.to change {
application.send(:default_path).to_s
}.from("/path/to/app/config/application.yml").to("/var/config/my-app.yml")
::ENV["FIGARO_PATH"] = nil
end

it "raises an error when Rails.root isn't set yet" do
allow(::Rails).to receive(:root) { nil }

Expand Down