rails-app_env 0.1.0 → 0.2.0

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: 14e75a290a83ff554c4a22d5b4de442f067335f7520bf32cac50e1cca8402587
4
- data.tar.gz: 1ce34786cc77f55066f68999286290fea50ebdb77ca4067657ca9e56f378bd8d
3
+ metadata.gz: 68b627e3b58352cf6fdcb39e88bb2de7ace7e6ec73889226ffd66ee479de8c3c
4
+ data.tar.gz: 9382437f7ad8a5d1fdfe575871914946e7aabe7f1e2832518f388b9bec60d4bb
5
5
  SHA512:
6
- metadata.gz: bec82722ff90a2fcb647e0c705793f19568b4105bb9c8b1d7836c139e6aca334a77902d74ff5166f1db057c0906c677a517214b5a66c68dc6bed4be1730bc173
7
- data.tar.gz: 9cf5d2d52dbe36a1dc98c83239fcfe0a49dfd3f6425b94dd480514e441eb5d15ac466370494f7d0b50afac09ebc3a91d0732ab9f15c200d8de604b7da0235276
6
+ metadata.gz: 8bb85243784b26e19b41611d80c6026b6219cf23d02b28acd729507512d536f29b18bfed9fa8f1a8449b6444b817b2d7183e7438ac5bf97ffa6d16e6754e342d
7
+ data.tar.gz: 7b90bf76133badc4f903165bb3abf2e94716f08ac37c55a8ca57ce616e0ea70f46202a48c11b9892e8b7d05e8984fd1a23a0e11a51f74f90b9acde60ed07a0fe
data/README.md CHANGED
@@ -1,28 +1,148 @@
1
- # Rails::AppEnv
2
- Short description and motivation.
1
+ <div align="center">
3
2
 
4
- ## Usage
5
- How to use my plugin.
3
+ # Rails APP_ENV
6
4
 
7
- ## Installation
8
- Add this line to your application's Gemfile:
5
+ [![Gem Version](https://img.shields.io/gem/v/rails-app_env)](https://badge.fury.io/rb/rails-app_env)
6
+ [![CI](https://github.com/typisttech/rails-app_env/actions/workflows/ci.yml/badge.svg)](https://github.com/typisttech/rails-app_env/actions/workflows/ci.yml)
7
+ [![License](https://img.shields.io/github/license/typisttech/rails-app_env.svg)](https://github.com/typisttech/rails-app_env/blob/master/LICENSE.txt)
8
+ [![Follow @TangRufus on X](https://img.shields.io/badge/Follow-TangRufus-15202B?logo=x&logoColor=white)](https://x.com/tangrufus)
9
+ [![Follow @TangRufus.com on Bluesky](https://img.shields.io/badge/Bluesky-TangRufus.com-blue?logo=bluesky)](https://bsky.app/profile/tangrufus.com)
10
+ [![Sponsor @TangRufus via GitHub](https://img.shields.io/badge/Sponsor-TangRufus-EA4AAA?logo=githubsponsors)](https://github.com/sponsors/tangrufus)
11
+ [![Hire Typist Tech](https://img.shields.io/badge/Hire-Typist%20Tech-778899)](https://typist.tech/contact/)
12
+
13
+ <p>
14
+ <strong>
15
+ <code>Rails APP_ENV</code> is like <code>RAILS_ENV</code> but for configurations only.
16
+ </strong>
17
+ <br>
18
+ <br>
19
+ Built with ♥ by <a href="https://typist.tech/">Typist Tech</a>
20
+ </p>
21
+
22
+ </div>
23
+
24
+ ---
25
+
26
+ ## Quick Start
27
+
28
+ TODO.
29
+
30
+ ## Why
31
+
32
+ TODO.
33
+
34
+ ## `RAILS_ENV` vs `APP_ENV`
35
+
36
+ TODO.
37
+
38
+ ## Features
39
+
40
+ ### `Rails.app_env`
41
+
42
+ `Rails.app_env` is like [`Rails.env`](https://api.rubyonrails.org/classes/Rails.html#method-c-env) but it is set by the
43
+ `APP_ENV` environment variable (`ENV["APP_ENV"]`).
44
+
45
+ It is optimization for `staging` and `review` ([the two extra Heroku pipeline stages](https://devcenter.heroku.com/articles/pipelines)),
46
+ so it doesn't need to rely on the slower delegation through `method_missing` that `ActiveSupport::EnvironmentInquirer`
47
+ would normally entail.
9
48
 
10
49
  ```ruby
11
- gem "rails-app_env"
50
+ ## Assume we booted Rails with `APP_ENV=staging RAILS_ENV=production`
51
+ Rails.app_env # => "staging"
52
+ Rails.app_env.staging? # => true
53
+ Rails.app_env.production? # => false
54
+ Rails.app_env.any_other_predicate? # => false
55
+
56
+ Rails.env # => "production"
57
+ Rails.env.staging? # => false
58
+ Rails.env.production? # => true
12
59
  ```
13
60
 
14
- And then execute:
15
- ```bash
16
- $ bundle
61
+ In case `ENV["APP_ENV"]` is blank, `Rails.app_env` falls back to `Rails.env`.
62
+
63
+ ### `Rails.application.app_config_for`
64
+
65
+ `Rails.application.app_config_for` wraps[`Rails.application.config_for`](https://api.rubyonrails.org/classes/Rails/Application.html#method-i-config_for)
66
+ with `{env: Rails.app_env}` as the second argument.
67
+
68
+ ```ruby
69
+ # These two lines are equivalent.
70
+ Rails.application.app_config_for(:foo)
71
+ Rails.application.config_for(:foo, env: Rails.app_env)
72
+ ```
73
+
74
+ ### Credentials
75
+
76
+ `Rails APP_ENV` overrides the default Rails credentials `content_path` and `key_path` according to `Rails.app_env`.
77
+
78
+ Given the following `*.yml.enc` and `*.key` files under `config/credentials/`, `APP_ENV=staging RAILS_ENV=production`
79
+ would load the credentials from `config/credentials/staging.yml.enc` and `config/credentials/staging.key`.
80
+
81
+ ```console
82
+ $ tree config/credentials
83
+ config/credentials
84
+ ├── production.key
85
+ ├── production.yml.enc
86
+ ├── staging.key
87
+ └── staging.yml.enc
88
+ ```
89
+
90
+ In case `config/credentials/#{Rails.app_env}.yml.enc` does not exist, it falls back to `config/credentials.yml.enc`.
91
+
92
+ In case `config/credentials/#{Rails.app_env}.key` does not exist, it falls back to `config/master.key`.
93
+
94
+ As with default Rails behaviours, if `ENV["RAILS_MASTER_KEY"]` is present, it takes precedence over
95
+ `config/credentials/#{Rails.app_env}.key` and `config/master.key`.
96
+
97
+ As with default Rails behaviours, when invoking `$ rails credentials` commands, specific the `--environment` option
98
+ instead of using `APP_ENV` and `RAILS_ENV`.
99
+
100
+ ```console
101
+ # APP_ENV and RAILS_ENV are ignored.
102
+ $ APP_ENV=foo RAILS_ENV=bar bin/rails credentials:edit --environment qaz
103
+ create config/credentials/qaz.key
104
+ create config/credentials/qaz.yml.enc
17
105
  ```
18
106
 
19
- Or install it yourself as:
107
+ Learn more in the [Heroku](#heroku) section below.
108
+
109
+ ### Console
110
+
111
+ Whenever Rails console is started, `Rails APP_ENV` prints the current `Rails.app_env` and gem version to the console.
112
+
113
+ If the `Rails.app_env` differs from `Rials.env`, `Rails APP_ENV` appends `Rails.app_env` to the console prompt.
114
+
115
+ ![rails console](docs/screenshot-rails-console.jpg)
116
+
117
+ ## Heroku
118
+
119
+ TODO.
120
+
121
+ ## Installation
122
+
123
+ Install the gem and add to the application's `Gemfile` or `gems.rb` by executing:
124
+
20
125
  ```bash
21
- $ gem install rails-app_env
126
+ bundle add rails-app_env
22
127
  ```
23
128
 
24
- ## Contributing
25
- Contribution directions go here.
129
+ ## Prior Art
130
+
131
+ - [anyway_config](https://github.com/palkan/anyway_config)
132
+ - [config](https://github.com/rubyconfig/config)
133
+
134
+ ## Credits
135
+
136
+ [`Rails APP_ENV`](https://github.com/typisttech/rails-app_env) is a [Typist Tech](https://typist.tech) project and
137
+ maintained by [Tang Rufus](https://x.com/TangRufus), freelance developer [for hire](https://typist.tech/contact/).
138
+
139
+ Full list of contributors can be found [on GitHub](https://github.com/typisttech/rails-app_env/graphs/contributors).
140
+
141
+ ## Copyright and License
142
+
143
+ This project is a [free software](https://www.gnu.org/philosophy/free-sw.en.html) distributed under the terms of
144
+ the MIT license. For the full license, see [LICENSE](LICENSE).
145
+
146
+ ## Contribute
26
147
 
27
- ## License
28
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
148
+ Feedbacks / bug reports / pull requests are welcome.
@@ -0,0 +1,9 @@
1
+ module Rails
2
+ module AppEnv
3
+ module ApplicationHelpers
4
+ def app_config_for(name)
5
+ config_for(name, env: Rails.app_env)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ require "rails/commands/console/irb_console"
2
+
3
+ module Rails
4
+ module AppEnv
5
+ class Console < Rails::Console::IRBConsole
6
+ def colorized_env
7
+ return super if Rails.env == Rails.app_env
8
+ super + ":" + colorized_app_env
9
+ end
10
+
11
+ private
12
+
13
+ def colorized_app_env
14
+ case Rails.app_env
15
+ when "development"
16
+ IRB::Color.colorize("dev", [:MAGENTA])
17
+ when "production"
18
+ IRB::Color.colorize("prod", [:RED])
19
+ else
20
+ IRB::Color.colorize(Rails.app_env, [:MAGENTA])
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,7 +1,30 @@
1
1
  module Rails
2
2
  module AppEnv
3
3
  module Credentials
4
+ class AlreadyInitializedError < Error; end
5
+
4
6
  class << self
7
+ attr_reader :original
8
+
9
+ def initialize!
10
+ raise AlreadyInitializedError.new "Rails::AppEnv::Credentials has already been initialized." if @initialized
11
+ @initialized = true
12
+
13
+ @original = Rails.application.config.credentials
14
+ Rails.application.config.credentials = configuration
15
+
16
+ monkey_patch_rails_credentials_command!
17
+ end
18
+
19
+ def configuration
20
+ ActiveSupport::InheritableOptions.new(
21
+ content_path: content_path,
22
+ key_path: key_path
23
+ )
24
+ end
25
+
26
+ private
27
+
5
28
  def content_path
6
29
  path = Rails.root.join("config/credentials/#{Rails.app_env}.yml.enc")
7
30
  path = Rails.root.join("config/credentials.yml.enc") unless path.exist?
@@ -13,6 +36,10 @@ module Rails
13
36
  path = Rails.root.join("config/master.key") unless path.exist?
14
37
  path
15
38
  end
39
+
40
+ def monkey_patch_rails_credentials_command!
41
+ require_relative "../rails_ext/credentials_command"
42
+ end
16
43
  end
17
44
  end
18
45
  end
@@ -0,0 +1,5 @@
1
+ module Rails
2
+ module AppEnv
3
+ class Error < StandardError; end
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module Rails
2
+ module AppEnv
3
+ module RailsHelpers
4
+ def app_env
5
+ @_app_env ||= EnvironmentInquirer.new(ENV["APP_ENV"] || Rails.env)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -2,12 +2,13 @@ module Rails
2
2
  module AppEnv
3
3
  class Railtie < Rails::Railtie
4
4
  config.before_configuration do
5
- Rails.extend(Helpers)
5
+ Rails.extend(RailsHelpers)
6
+ Rails.application.extend(ApplicationHelpers)
6
7
  end
7
8
 
8
- config.before_configuration do |app|
9
- app.config.credentials.content_path = Rails::AppEnv::Credentials.content_path
10
- app.config.credentials.key_path = Rails::AppEnv::Credentials.key_path
9
+ config.before_configuration do
10
+ # TODO: Allow opt-out.
11
+ Credentials.initialize!
11
12
  end
12
13
 
13
14
  config.after_initialize do
@@ -15,6 +16,13 @@ module Rails
15
16
  Rails.app_env
16
17
  end
17
18
  end
19
+
20
+ console do |app|
21
+ # TODO: Allow opt-out.
22
+ app.config.console = Console.new(app)
23
+
24
+ puts "Loading #{Rails.app_env} application environment (rails-app_env #{Rails::AppEnv::VERSION})" # standard:disable Rails/Output
25
+ end
18
26
  end
19
27
  end
20
28
  end
@@ -1,5 +1,5 @@
1
1
  module Rails
2
2
  module AppEnv
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
data/lib/rails/app_env.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  require_relative "app_env/version"
2
- require_relative "app_env/environment_inquirer"
3
- require_relative "app_env/helpers"
4
- require_relative "app_env/credentials"
5
2
  require_relative "app_env/railtie"
6
3
 
7
4
  module Rails
8
5
  module AppEnv
9
- # Your code goes here...
6
+ autoload :ApplicationHelpers, "rails/app_env/application_helpers"
7
+ autoload :Console, "rails/app_env/console"
8
+ autoload :Credentials, "rails/app_env/credentials"
9
+ autoload :EnvironmentInquirer, "rails/app_env/environment_inquirer"
10
+ autoload :Error, "rails/app_env/error"
11
+ autoload :RailsHelpers, "rails/app_env/rails_helpers"
10
12
  end
11
13
  end
@@ -0,0 +1,11 @@
1
+ module Rails
2
+ module Command
3
+ class CredentialsCommand
4
+ private
5
+
6
+ def config
7
+ Rails::AppEnv::Credentials.original
8
+ end
9
+ end
10
+ end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-app_env
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Typist Tech Limited
@@ -11,19 +11,33 @@ cert_chain: []
11
11
  date: 1980-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: railties
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 8.0.2
19
+ version: 8.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 8.0.2
26
+ version: 8.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 8.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 8.0.0
27
41
  email:
28
42
  - opensource+swagger_ui_standalone@typist.tech
29
43
  - tangrufus@gmail.com
@@ -35,11 +49,15 @@ files:
35
49
  - README.md
36
50
  - Rakefile
37
51
  - lib/rails/app_env.rb
52
+ - lib/rails/app_env/application_helpers.rb
53
+ - lib/rails/app_env/console.rb
38
54
  - lib/rails/app_env/credentials.rb
39
55
  - lib/rails/app_env/environment_inquirer.rb
40
- - lib/rails/app_env/helpers.rb
56
+ - lib/rails/app_env/error.rb
57
+ - lib/rails/app_env/rails_helpers.rb
41
58
  - lib/rails/app_env/railtie.rb
42
59
  - lib/rails/app_env/version.rb
60
+ - lib/rails/rails_ext/credentials_command.rb
43
61
  - lib/tasks/rails/app_env_tasks.rake
44
62
  homepage: https://github.com/typisttech/rails-app_env
45
63
  licenses:
@@ -67,5 +85,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
85
  requirements: []
68
86
  rubygems_version: 3.6.7
69
87
  specification_version: 4
70
- summary: Summary of Rails::AppEnv.
88
+ summary: Rails APP_ENV is like RAILS_ENV but for configurations only.
71
89
  test_files: []
@@ -1,11 +0,0 @@
1
- module Rails
2
- module AppEnv
3
- module Helpers
4
- def app_env
5
- return Rails.env if ENV["APP_ENV"].blank?
6
-
7
- @_app_env ||= EnvironmentInquirer.new(ENV["APP_ENV"])
8
- end
9
- end
10
- end
11
- end