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

Feat/environment variables #435

Merged
merged 4 commits into from
Apr 21, 2023
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

## Unreleased
Features
- Add environment variables module recipe [#435](https://github.com/platanus/potassium/pull/435)

Fixes
- Fix CircleCI config [#434](https://github.com/platanus/potassium/pull/434)

## 7.0.0

Expand Down
12 changes: 6 additions & 6 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ Now, to add some behavior, thanks to the [DSL](/docs/DSL.md) we have a kind of s
4. Install the gems, filling the `Gemfile` before with all the gathered gems.
5. Finally, create the database.

The main step is the 3rd, when we call the `create` methods from the recipes. A recipe can do anything (because is a ruby script) but their responsability should be to gather gems and register callbacks for the process.
The main step is the 3rd, when we call the `create` methods from the recipes. A recipe can do anything (because is a ruby script) but their responsibility should be to gather gems and register callbacks for the process.

### Recipe

The recipes are classes defined in the `Recipes` module that inherit from
`Rails::AppBuilder` and by convention can implement some of the following
methods `ask`, `create` and `install`. So they took this form.
methods `ask`, `create` and `install`. They take this form.

```ruby
class Recipes::MyRecipe < Rails::AppBuilder
Expand Down Expand Up @@ -73,7 +73,7 @@ This method is used if you need to ask something to the user before doing someth

We'll call this method to add specific functionality to the rails project.

1. In the `create` method register a gem using `gather_gem` and create a callback to be called after the `gem_install` action succeded to run the generator. `gem_install` is one of the main actions that should be easily visible with a sneak peek in [the template][the-template].
1. In the `create` method register a gem using `gather_gem` and create a callback to be called after the `gem_install` action succeeds to run the generator. `gem_install` is one of the main actions that should be easily visible with a sneak peek in [the template][the-template].

```ruby
def create
Expand All @@ -99,8 +99,8 @@ We'll call this method to add specific functionality to the rails project.
##### | `install`

The install method will be called when you use the `install` command from potassium.
For example if you run `portassium install devise` this will use
[the recipe template](/lib/potassium/templates/recipe.rb) to load an execute the
For example, if you run `potassium install devise` this will use
[the recipe template](/lib/potassium/templates/recipe.rb) to load and execute the
`install` method for the **devise** recipe.

You can define the main functionallity of a recipe in a private method and call
Expand All @@ -127,6 +127,6 @@ end

To see further documentation of what we added to the rails template's DSL, check the [DSL documentation](/docs/DSL.md).

> Remember that the DSL we are documenting is an extension over the [Rails Application Template DSL](http://edgeguides.rubyonrails.org/rails_application_templates.html), that itself is a dsl based on [Thor](https://github.com/erikhuda/thor/wiki).
> Remember that the DSL we are documenting is an extension over the [Rails Application Template DSL](http://edgeguides.rubyonrails.org/rails_application_templates.html), that itself is a DSL based on [Thor](https://github.com/erikhuda/thor/wiki).

[the-template]: /lib/potassium/templates/application.rb
1 change: 1 addition & 0 deletions lib/potassium/assets/.env.development.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ DEFAULT_EMAIL_ADDRESS=
EXECJS_RUNTIME=Node
SECRET_KEY_BASE=development_secret
WEB_CONCURRENCY=1
APPLICATION_HOST=localhost:3000
1 change: 1 addition & 0 deletions lib/potassium/assets/.env.test.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
APPLICATION_HOST=localhost:3000
9 changes: 9 additions & 0 deletions lib/potassium/assets/lib/environment_variables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module EnvironmentVariables
extend self

APPLICATION_HOST = ENV.fetch('APPLICATION_HOST')

def application_host
APPLICATION_HOST
end
end
1 change: 1 addition & 0 deletions lib/potassium/assets/testing/simplecov_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
add_filter 'app/responders'
add_filter 'lib/fake_data_loader.rb'
add_filter 'lib/vue_component.rb'
add_filter 'lib/environment_variables.rb'

if ENV["CIRCLECI"]
formatter(
Expand Down
18 changes: 18 additions & 0 deletions lib/potassium/recipes/environment_variables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Recipes::EnvironmentVariables < Rails::AppBuilder
def create
template '../assets/.env.test.erb', '.env.test'
copy_file '../assets/lib/environment_variables.rb', 'lib/environment_variables.rb'

application(before_configuration_require)
end

private

def before_configuration_require
<<~RUBY
config.before_configuration do
require Rails.root.join('lib/environment_variables.rb')
end
RUBY
end
end
1 change: 1 addition & 0 deletions lib/potassium/templates/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
create :front_end_vite
create :admin
create :vue_admin
create :environment_variables
end

info "Gathered enough information. Applying the template. Wait a minute."
Expand Down
22 changes: 22 additions & 0 deletions spec/features/environment_variables_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'spec_helper'

RSpec.describe 'EnvironmentVariables' do
let(:environment_variables) { IO.read("#{project_path}/lib/environment_variables.rb") }

it 'creates a .env.test file' do
expect(File.exists?("#{project_path}/.env.test")).to eq(true)
end

it 'creates an EnvironmentVariables module with a constant and its method' do
expect(environment_variables).to include(
'module EnvironmentVariables',
"APPLICATION_HOST = ENV.fetch('APPLICATION_HOST')",
'def application_host'
)
end

it 'requires module in application configuration' do
expect(File.read("#{project_path}/config/application.rb"))
.to include("require Rails.root.join('lib/environment_variables.rb')")
end
end