dotenv-rails 2.8.1 → 3.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7d0c13519d33404b622266fbf7ff71079a199716ac564e4a2729384b15b970d7
4
- data.tar.gz: f2da97acce1fb1e953e000114f9f01edb596adb0676b20a1e11cc8d77e6c405b
3
+ metadata.gz: c0e302fb028f65e97a5a1c22d97534a0fbc42fc6e84b6ba11fde5d5406dd2334
4
+ data.tar.gz: cc3d75dc764b994ce40ac42dabbcb89f88a611236f0122f1ae91517a752f71d9
5
5
  SHA512:
6
- metadata.gz: 8d2102375cd88ce4b90c44629d0976062f9d9ebe05f95e480f38f6dcd509c28d23d8dff295339e2f4870263e595fcff6f45d6f972fee2e00a67a8b946979032a
7
- data.tar.gz: 420d8614065db0398d72e13548c53ee25aa1da6d1676a96a7a0f93dbe33aca24e5ea4c48073b37ee29645a0bbcc56e6082289bd9473e37ade4f395eec2176099
6
+ metadata.gz: dd0c122d22515d49b9c245c9ba2f4f62ebe21d60fec5e599ea521c6ddb536b3d222e09d1dd356144252c96e4d4ffd9b9a526a7d0f828807d7f588f4cb62b33d8
7
+ data.tar.gz: c5979568723021877ab6706d2a16a6c3ab5f274d307a25de670f5a34a486a60483a13d14ad851fa94dbce6a9505ebb36d934cb93f55e734318da08cad09a7fa8
data/README.md CHANGED
@@ -1,59 +1,48 @@
1
- # dotenv [![Gem Version](https://badge.fury.io/rb/dotenv.svg)](https://badge.fury.io/rb/dotenv) [![Join the chat at https://gitter.im/bkeepers/dotenv](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/bkeepers/dotenv?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
1
+ # dotenv [![Gem Version](https://badge.fury.io/rb/dotenv.svg)](https://badge.fury.io/rb/dotenv)
2
2
 
3
3
  Shim to load environment variables from `.env` into `ENV` in *development*.
4
4
 
5
+ <img align="left" src="https://github.com/user-attachments/assets/0052ed0b-00d2-416a-bdaa-0466c0226933" width="80" />
6
+ <div><sup>Dotenv is proud to be <a href="https://github.com/sponsors/bkeepers">sponsored by</a>:</sup></div>
7
+ <strong><a href="https://bit.ly/dotenv-stoked-seagull">Stoked Seagull Software</a></strong>
8
+ <div>Need help with a software project but don't know where to begin? <a href="https://bit.ly/dotenv-stoked-seagull">Stoked Seagull can help.</a></div><br><br>
9
+
5
10
  Storing [configuration in the environment](http://12factor.net/config) is one of the tenets of a [twelve-factor app](http://12factor.net). Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables.
6
11
 
7
12
  But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. dotenv loads variables from a `.env` file into `ENV` when the environment is bootstrapped.
8
13
 
9
14
  ## Installation
10
15
 
11
- ### Rails
12
-
13
- Add this line to the top of your application's Gemfile:
16
+ Add this line to the top of your application's Gemfile and run `bundle install`:
14
17
 
15
18
  ```ruby
16
- gem 'dotenv-rails', groups: [:development, :test]
19
+ gem 'dotenv', groups: [:development, :test]
17
20
  ```
18
21
 
19
- And then execute:
22
+ ## Usage
23
+
24
+ Add your application configuration to your `.env` file in the root of your project:
20
25
 
21
26
  ```shell
22
- $ bundle
27
+ S3_BUCKET=YOURS3BUCKET
28
+ SECRET_KEY=YOURSECRETKEYGOESHERE
23
29
  ```
24
30
 
25
- #### Note on load order
26
-
27
- dotenv is initialized in your Rails app during the `before_configuration` callback, which is fired when the `Application` constant is defined in `config/application.rb` with `class Application < Rails::Application`. If you need it to be initialized sooner, you can manually call `Dotenv::Railtie.load`.
31
+ Whenever your application loads, these variables will be available in `ENV`:
28
32
 
29
33
  ```ruby
30
- # config/application.rb
31
- Bundler.require(*Rails.groups)
32
-
33
- # Load dotenv only in development or test environment
34
- if ['development', 'test'].include? ENV['RAILS_ENV']
35
- Dotenv::Railtie.load
36
- end
37
-
38
- HOSTNAME = ENV['HOSTNAME']
34
+ config.fog_directory = ENV['S3_BUCKET']
39
35
  ```
40
36
 
41
- If you use gems that require environment variables to be set before they are loaded, then list `dotenv-rails` in the `Gemfile` before those other gems and require `dotenv/rails-now`.
37
+ See the [API Docs](https://rubydoc.info/github/bkeepers/dotenv/main) for more.
42
38
 
43
- ```ruby
44
- gem 'dotenv-rails', require: 'dotenv/rails-now'
45
- gem 'gem-that-requires-env-variables'
46
- ```
39
+ ### Rails
47
40
 
48
- ### Sinatra or Plain ol' Ruby
41
+ Dotenv will automatically load when your Rails app boots. See [Customizing Rails](#customizing-rails) to change which files are loaded and when.
49
42
 
50
- Install the gem:
43
+ ### Sinatra / Ruby
51
44
 
52
- ```shell
53
- $ gem install dotenv
54
- ```
55
-
56
- As early as possible in your application bootstrap process, load `.env`:
45
+ Load Dotenv as early as possible in your application bootstrap process:
57
46
 
58
47
  ```ruby
59
48
  require 'dotenv/load'
@@ -70,17 +59,17 @@ require 'dotenv'
70
59
  Dotenv.load('file1.env', 'file2.env')
71
60
  ```
72
61
 
73
- Alternatively, you can use the `dotenv` executable to launch your application:
62
+ ### Autorestore in tests
74
63
 
75
- ```shell
76
- $ dotenv ./script.rb
77
- ```
64
+ Since 3.0, dotenv in a Rails app will automatically restore `ENV` after each test. This means you can modify `ENV` in your tests without fear of leaking state to other tests. It works with both `ActiveSupport::TestCase` and `Rspec`.
78
65
 
79
- The `dotenv` executable also accepts a single flag, `-f`. Its value should be a comma-separated list of configuration files, in the order of most important to least. All of the files must exist. There _must_ be a space between the flag and its value.
66
+ To disable this behavior, set `config.dotenv.autorestore = false` in `config/application.rb` or `config/environments/test.rb`. It is disabled by default if your app uses [climate_control](https://github.com/thoughtbot/climate_control) or [ice_age](https://github.com/dpep/ice_age_rb).
80
67
 
81
- ```
82
- $ dotenv -f ".env.local,.env" ./script.rb
83
- ```
68
+ To use this behavior outside of a Rails app, just `require "dotenv/autorestore"` in your test suite.
69
+
70
+ See [`Dotenv.save`](https://rubydoc.info/github/bkeepers/dotenv/main/Dotenv:save), [Dotenv.restore](https://rubydoc.info/github/bkeepers/dotenv/main/Dotenv:restore), and [`Dotenv.modify(hash) { ... }`](https://rubydoc.info/github/bkeepers/dotenv/main/Dotenv:modify) for manual usage.
71
+
72
+ ### Rake
84
73
 
85
74
  To ensure `.env` is loaded in rake, load the tasks:
86
75
 
@@ -88,41 +77,121 @@ To ensure `.env` is loaded in rake, load the tasks:
88
77
  require 'dotenv/tasks'
89
78
 
90
79
  task mytask: :dotenv do
91
- # things that require .env
80
+ # things that require .env
92
81
  end
93
82
  ```
94
83
 
95
- ## Usage
84
+ ### CLI
96
85
 
97
- Add your application configuration to your `.env` file in the root of your project:
86
+ You can use the `dotenv` executable load `.env` before launching your application:
98
87
 
99
- ```shell
100
- S3_BUCKET=YOURS3BUCKET
101
- SECRET_KEY=YOURSECRETKEYGOESHERE
88
+ ```console
89
+ $ dotenv ./script.rb
102
90
  ```
103
91
 
104
- Whenever your application loads, these variables will be available in `ENV`:
92
+ The `dotenv` executable also accepts the flag `-f`. Its value should be a comma-separated list of configuration files, in the order of the most important to the least important. All of the files must exist. There _must_ be a space between the flag and its value.
105
93
 
106
- ```ruby
107
- config.fog_directory = ENV['S3_BUCKET']
94
+ ```console
95
+ $ dotenv -f ".env.local,.env" ./script.rb
108
96
  ```
109
97
 
110
- You may also add `export` in front of each line so you can `source` the file in bash:
98
+ The `dotenv` executable can optionally ignore missing files with the `-i` or `--ignore` flag. For example, if the `.env.local` file does not exist, the following will ignore the missing file and only load the `.env` file.
111
99
 
112
- ```shell
113
- export S3_BUCKET=YOURS3BUCKET
114
- export SECRET_KEY=YOURSECRETKEYGOESHERE
100
+ ```console
101
+ $ dotenv -i -f ".env.local,.env" ./script.rb
115
102
  ```
116
103
 
117
- ### Multi-line values
104
+ ### Load Order
118
105
 
119
- If you need multiline variables, for example private keys, you can double quote strings and use the `\n` character for newlines:
106
+ If you use gems that require environment variables to be set before they are loaded, then list `dotenv` in the `Gemfile` before those other gems and require `dotenv/load`.
120
107
 
121
- ```shell
122
- PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nHkVN9...\n-----END DSA PRIVATE KEY-----\n"
108
+ ```ruby
109
+ gem 'dotenv', require: 'dotenv/load'
110
+ gem 'gem-that-requires-env-variables'
123
111
  ```
124
112
 
125
- Alternatively, multi-line values with line breaks are now supported for quoted values.
113
+ ### Customizing Rails
114
+
115
+ Dotenv will load the following files depending on `RAILS_ENV`, with the first file having the highest precedence, and `.env` having the lowest precedence:
116
+
117
+ <table>
118
+ <thead>
119
+ <tr>
120
+ <th>Priority</th>
121
+ <th colspan="3">Environment</th>
122
+ <th><code>.gitignore</code>it?</th>
123
+ <th>Notes</th>
124
+ </tr>
125
+ <tr>
126
+ <th></th>
127
+ <th>development</th>
128
+ <th>test</th>
129
+ <th>production</th>
130
+ <th></th>
131
+ <th></th>
132
+ </tr>
133
+ </thead>
134
+ <tr>
135
+ <td>highest</td>
136
+ <td><code>.env.development.local</code></td>
137
+ <td><code>.env.test.local</code></td>
138
+ <td><code>.env.production.local</code></td>
139
+ <td>Yes</td>
140
+ <td>Environment-specific local overrides</td>
141
+ </tr>
142
+ <tr>
143
+ <td>2nd</td>
144
+ <td><code>.env.local</code></td>
145
+ <td><strong>N/A</strong></td>
146
+ <td><code>.env.local</code></td>
147
+ <td>Yes</td>
148
+ <td>Local overrides</td>
149
+ </tr>
150
+ <tr>
151
+ <td>3rd</td>
152
+ <td><code>.env.development</code></td>
153
+ <td><code>.env.test</code></td>
154
+ <td><code>.env.production</code></td>
155
+ <td>No</td>
156
+ <td>Shared environment-specific variables</td>
157
+ </tr>
158
+ <tr>
159
+ <td>last</td>
160
+ <td><code>.env</code></td>
161
+ <td><code>.env</code></td>
162
+ <td><code>.env</code></td>
163
+ <td><a href="#should-i-commit-my-env-file">Maybe</a></td>
164
+ <td>Shared for all environments</td>
165
+ </tr>
166
+ </table>
167
+
168
+
169
+ These files are loaded during the `before_configuration` callback, which is fired when the `Application` constant is defined in `config/application.rb` with `class Application < Rails::Application`. If you need it to be initialized sooner, or need to customize the loading process, you can do so at the top of `application.rb`
170
+
171
+ ```ruby
172
+ # config/application.rb
173
+ Bundler.require(*Rails.groups)
174
+
175
+ # Load .env.local in test
176
+ Dotenv::Rails.files.unshift(".env.local") if ENV["RAILS_ENV"] == "test"
177
+
178
+ module YourApp
179
+ class Application < Rails::Application
180
+ # ...
181
+ end
182
+ end
183
+ ```
184
+
185
+ Available options:
186
+
187
+ * `Dotenv::Rails.files` - list of files to be loaded, in order of precedence.
188
+ * `Dotenv::Rails.overwrite` - Overwrite existing `ENV` variables with contents of `.env*` files
189
+ * `Dotenv::Rails.logger` - The logger to use for dotenv's logging. Defaults to `Rails.logger`
190
+ * `Dotenv::Rails.autorestore` - Enable or disable [autorestore](#autorestore-in-tests)
191
+
192
+ ### Multi-line values
193
+
194
+ Multi-line values with line breaks must be surrounded with double quotes.
126
195
 
127
196
  ```shell
128
197
  PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
@@ -132,7 +201,12 @@ HkVN9...
132
201
  -----END DSA PRIVATE KEY-----"
133
202
  ```
134
203
 
135
- This is particularly helpful when using the Heroku command line plugin [`heroku-config`](https://github.com/xavdid/heroku-config) to pull configuration variables down that may have line breaks.
204
+ Prior to 3.0, dotenv would replace `\n` in quoted strings with a newline, but that behavior is deprecated. To use the old behavior, set `DOTENV_LINEBREAK_MODE=legacy` before any variables that include `\n`:
205
+
206
+ ```shell
207
+ DOTENV_LINEBREAK_MODE=legacy
208
+ PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nHkVN9...\n-----END DSA PRIVATE KEY-----\n"
209
+ ```
136
210
 
137
211
  ### Command Substitution
138
212
 
@@ -144,7 +218,7 @@ DATABASE_URL="postgres://$(whoami)@localhost/my_database"
144
218
 
145
219
  ### Variable Substitution
146
220
 
147
- You need to add the value of another variable in one of your variables? You can reference the variable with `${VAR}` or often just `$VAR` in unqoted or double-quoted values.
221
+ You need to add the value of another variable in one of your variables? You can reference the variable with `${VAR}` or often just `$VAR` in unquoted or double-quoted values.
148
222
 
149
223
  ```shell
150
224
  DATABASE_URL="postgres://${USER}@localhost/my_database"
@@ -166,6 +240,15 @@ SECRET_KEY=YOURSECRETKEYGOESHERE # comment
166
240
  SECRET_HASH="something-with-a-#-hash"
167
241
  ```
168
242
 
243
+ ### Exports
244
+
245
+ For compatability, you may also add `export` in front of each line so you can `source` the file in bash:
246
+
247
+ ```shell
248
+ export S3_BUCKET=YOURS3BUCKET
249
+ export SECRET_KEY=YOURSECRETKEYGOESHERE
250
+ ```
251
+
169
252
  ### Required Keys
170
253
 
171
254
  If a particular configuration value is required but not set, it's appropriate to raise an error.
@@ -191,42 +274,14 @@ Dotenv.parse(".env.local", ".env")
191
274
 
192
275
  This method returns a hash of the ENV var name/value pairs.
193
276
 
194
- ## Frequently Answered Questions
195
-
196
- ### Can I use dotenv in production?
197
-
198
- dotenv was originally created to load configuration variables into `ENV` in *development*. There are typically better ways to manage configuration in production environments - such as `/etc/environment` managed by [Puppet](https://github.com/puppetlabs/puppet) or [Chef](https://github.com/chef/chef), `heroku config`, etc.
199
-
200
- However, some find dotenv to be a convenient way to configure Rails applications in staging and production environments, and you can do that by defining environment-specific files like `.env.production` or `.env.test`.
201
-
202
- If you use this gem to handle env vars for multiple Rails environments (development, test, production, etc.), please note that env vars that are general to all environments should be stored in `.env`. Then, environment specific env vars should be stored in `.env.<that environment's name>`.
203
-
204
- ### What other .env* files can I use?
205
-
206
- `dotenv-rails` will override in the following order (highest defined variable overrides lower):
207
-
208
- | Hierarchy Priority | Filename | Environment | Should I `.gitignore`it? | Notes |
209
- | ------------------ | ------------------------ | -------------------- | --------------------------------------------------- | ------------------------------------------------------------ |
210
- | 1st (highest) | `.env.development.local` | Development | Yes! | Local overrides of environment-specific settings. |
211
- | 1st | `.env.test.local` | Test | Yes! | Local overrides of environment-specific settings. |
212
- | 1st | `.env.production.local` | Production | Yes! | Local overrides of environment-specific settings. |
213
- | 2nd | `.env.local` | Wherever the file is | Definitely. | Local overrides. This file is loaded for all environments _except_ `test`. |
214
- | 3rd | `.env.development` | Development | No. | Shared environment-specific settings |
215
- | 3rd | `.env.test` | Test | No. | Shared environment-specific settings |
216
- | 3rd | `.env.production` | Production | No. | Shared environment-specific settings |
217
- | Last | `.env` | All Environments | Depends (See [below](#should-i-commit-my-env-file)) | The Original® |
218
-
219
-
220
- ### Should I commit my .env file?
221
-
222
- Credentials should only be accessible on the machines that need access to them. Never commit sensitive information to a repository that is not needed by every development machine and server.
223
-
277
+ ### Templates
224
278
 
225
279
  You can use the `-t` or `--template` flag on the dotenv cli to create a template of your `.env` file.
226
- ```shell
280
+
281
+ ```console
227
282
  $ dotenv -t .env
228
283
  ```
229
- A template will be created in your working directory named `{FINAME}.template`. So in the above example, it would create a `.env.template` file.
284
+ A template will be created in your working directory named `{FILENAME}.template`. So in the above example, it would create a `.env.template` file.
230
285
 
231
286
  The template will contain all the environment variables in your `.env` file but with their values set to the variable names.
232
287
 
@@ -244,14 +299,29 @@ S3_BUCKET=S3_BUCKET
244
299
  SECRET_KEY=SECRET_KEY
245
300
  ```
246
301
 
302
+ ## Frequently Answered Questions
303
+
304
+ ### Can I use dotenv in production?
305
+
306
+ dotenv was originally created to load configuration variables into `ENV` in *development*. There are typically better ways to manage configuration in production environments - such as `/etc/environment` managed by [Puppet](https://github.com/puppetlabs/puppet) or [Chef](https://github.com/chef/chef), `heroku config`, etc.
307
+
308
+ However, some find dotenv to be a convenient way to configure Rails applications in staging and production environments, and you can do that by defining environment-specific files like `.env.production` or `.env.test`.
309
+
310
+ If you use this gem to handle env vars for multiple Rails environments (development, test, production, etc.), please note that env vars that are general to all environments should be stored in `.env`. Then, environment specific env vars should be stored in `.env.<that environment's name>`.
311
+
312
+ ### Should I commit my .env file?
313
+
314
+ Credentials should only be accessible on the machines that need access to them. Never commit sensitive information to a repository that is not needed by every development machine and server.
315
+
247
316
  Personally, I prefer to commit the `.env` file with development-only settings. This makes it easy for other developers to get started on the project without compromising credentials for other environments. If you follow this advice, make sure that all the credentials for your development environment are different from your other deployments and that the development credentials do not have access to any confidential data.
248
317
 
249
- ### Why is it not overriding existing `ENV` variables?
318
+ ### Why is it not overwriting existing `ENV` variables?
250
319
 
251
- By default, it **won't** overwrite existing environment variables as dotenv assumes the deployment environment has more knowledge about configuration than the application does. To overwrite existing environment variables you can use `Dotenv.overload`.
320
+ By default, it **won't** overwrite existing environment variables as dotenv assumes the deployment environment has more knowledge about configuration than the application does. To overwrite existing environment variables you can use `Dotenv.load files, overwrite: true`.
252
321
 
253
- You can also use the `-o` or `--overload` flag on the dotenv cli to override existing `ENV` variables.
254
- ```shell
322
+ You can also use the `-o` or `--overwrite` flag on the dotenv cli to overwrite existing `ENV` variables.
323
+
324
+ ```console
255
325
  $ dotenv -o -f ".env.local,.env"
256
326
  ```
257
327
 
data/lib/dotenv-rails.rb CHANGED
@@ -1 +1 @@
1
- require "dotenv/rails"
1
+ require "dotenv"
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotenv-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.8.1
4
+ version: 3.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Keepers
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2022-07-27 00:00:00.000000000 Z
10
+ date: 2025-04-10 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: dotenv
@@ -16,28 +15,28 @@ dependencies:
16
15
  requirements:
17
16
  - - '='
18
17
  - !ruby/object:Gem::Version
19
- version: 2.8.1
18
+ version: 3.1.8
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
23
  - - '='
25
24
  - !ruby/object:Gem::Version
26
- version: 2.8.1
25
+ version: 3.1.8
27
26
  - !ruby/object:Gem::Dependency
28
27
  name: railties
29
28
  requirement: !ruby/object:Gem::Requirement
30
29
  requirements:
31
30
  - - ">="
32
31
  - !ruby/object:Gem::Version
33
- version: '3.2'
32
+ version: '6.1'
34
33
  type: :runtime
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
37
  - - ">="
39
38
  - !ruby/object:Gem::Version
40
- version: '3.2'
39
+ version: '6.1'
41
40
  - !ruby/object:Gem::Dependency
42
41
  name: spring
43
42
  requirement: !ruby/object:Gem::Requirement
@@ -62,13 +61,12 @@ files:
62
61
  - LICENSE
63
62
  - README.md
64
63
  - lib/dotenv-rails.rb
65
- - lib/dotenv/rails-now.rb
66
- - lib/dotenv/rails.rb
67
64
  homepage: https://github.com/bkeepers/dotenv
68
65
  licenses:
69
66
  - MIT
70
- metadata: {}
71
- post_install_message:
67
+ metadata:
68
+ changelog_uri: https://github.com/bkeepers/dotenv/releases
69
+ funding_uri: https://github.com/sponsors/bkeepers
72
70
  rdoc_options: []
73
71
  require_paths:
74
72
  - lib
@@ -83,8 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
81
  - !ruby/object:Gem::Version
84
82
  version: '0'
85
83
  requirements: []
86
- rubygems_version: 3.2.32
87
- signing_key:
84
+ rubygems_version: 3.6.2
88
85
  specification_version: 4
89
86
  summary: Autoload dotenv in Rails.
90
87
  test_files: []
@@ -1,10 +0,0 @@
1
- # If you use gems that require environment variables to be set before they are
2
- # loaded, then list `dotenv-rails` in the `Gemfile` before those other gems and
3
- # require `dotenv/rails-now`.
4
- #
5
- # gem "dotenv-rails", require: "dotenv/rails-now"
6
- # gem "gem-that-requires-env-variables"
7
- #
8
-
9
- require "dotenv/rails"
10
- Dotenv::Railtie.load
data/lib/dotenv/rails.rb DELETED
@@ -1,77 +0,0 @@
1
- require "dotenv"
2
-
3
- # Fix for rake tasks loading in development
4
- #
5
- # Dotenv loads environment variables when the Rails application is initialized.
6
- # When running `rake`, the Rails application is initialized in development.
7
- # Rails includes some hacks to set `RAILS_ENV=test` when running `rake test`,
8
- # but rspec does not include the same hacks.
9
- #
10
- # See https://github.com/bkeepers/dotenv/issues/219
11
- if defined?(Rake.application)
12
- task_regular_expression = /^(default$|parallel:spec|spec(:|$))/
13
- if Rake.application.top_level_tasks.grep(task_regular_expression).any?
14
- environment = Rake.application.options.show_tasks ? "development" : "test"
15
- Rails.env = ENV["RAILS_ENV"] ||= environment
16
- end
17
- end
18
-
19
- Dotenv.instrumenter = ActiveSupport::Notifications
20
-
21
- # Watch all loaded env files with Spring
22
- begin
23
- require "spring/commands"
24
- ActiveSupport::Notifications.subscribe(/^dotenv/) do |*args|
25
- event = ActiveSupport::Notifications::Event.new(*args)
26
- Spring.watch event.payload[:env].filename if Rails.application
27
- end
28
- rescue LoadError, ArgumentError
29
- # Spring is not available
30
- end
31
-
32
- module Dotenv
33
- # Dotenv Railtie for using Dotenv to load environment from a file into
34
- # Rails applications
35
- class Railtie < Rails::Railtie
36
- # Public: Load dotenv
37
- #
38
- # This will get called during the `before_configuration` callback, but you
39
- # can manually call `Dotenv::Railtie.load` if you needed it sooner.
40
- def load
41
- Dotenv.load(*dotenv_files)
42
- end
43
-
44
- # Public: Reload dotenv
45
- #
46
- # Same as `load`, but will override existing values in `ENV`
47
- def overload
48
- Dotenv.overload(*dotenv_files)
49
- end
50
-
51
- # Internal: `Rails.root` is nil in Rails 4.1 before the application is
52
- # initialized, so this falls back to the `RAILS_ROOT` environment variable,
53
- # or the current working directory.
54
- def root
55
- Rails.root || Pathname.new(ENV["RAILS_ROOT"] || Dir.pwd)
56
- end
57
-
58
- # Rails uses `#method_missing` to delegate all class methods to the
59
- # instance, which means `Kernel#load` gets called here. We don't want that.
60
- def self.load
61
- instance.load
62
- end
63
-
64
- private
65
-
66
- def dotenv_files
67
- [
68
- root.join(".env.#{Rails.env}.local"),
69
- (root.join(".env.local") unless Rails.env.test?),
70
- root.join(".env.#{Rails.env}"),
71
- root.join(".env")
72
- ].compact
73
- end
74
-
75
- config.before_configuration { load }
76
- end
77
- end