loading...

Moving from ENV to database-backed config

ben profile image Ben Halpern ・1 min read

In an effort to simplify what it means to spin up a forem from a systems side of things, we're moving things which were once environment variables into database-backed "Config".

For example, we once had COMMUNITY_NAME as an environment variable, which meant that we could spin up a forem with a name like "forem.dev"....

Add community_name to site config #9864

What type of PR is this? (check all applicable)

  • [x] Refactor
  • [x] Feature
  • [ ] Bug Fix
  • [ ] Optimization
  • [ ] Documentation Update

Description

In an effort to minimize what systems needs to care about, let's move this variable to what is available to forem admins.

water ski

But that means that we need a portal into environment variables and then restart the app every time we want to let admins make a change.

We are instead working to reduce the scope of what needs to be done in the environment, and instead make the app more flexible within its own universe.

These variables are stored in Postgres, but generally served in a more optimized way through the Rails cache.

We have already been using this gem to accomplish this for the rest of config, the only new thing is broadening the scope of what we do within this context.

GitHub logo huacnlee / rails-settings-cached

Global settings for your Rails application.

Rails Settings Cached

This a plugin that makes managing a table of а global key, value pairs easy. Think of it like a global Hash stored in your database that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you don't want to hard code into your rails app. You can store any kind of object. Strings, numbers, arrays, or any object.

🚨 BREAK CHANGES WARNING rails-settings-cached 2.x has redesigned the API, the new version will compatible with the stored setting values by an older version When you want to upgrade 2.x, you must read the README again, and follow guides to change your Setting model. 0.x stable branch: https://github.com/huacnlee/rails-settings-cached/tree/0.x

Status

Gem Version CI Status Code Climate codecov.io

Setup

Edit your Gemfile:

gem "rails-settings-cached", "~> 2.0"

Generate your settings:

$ rails g settings:install

If you want custom model name:

$ rails g settings:install

Or use a custom name:

$ rails g

Some of these shifts are fairly straightforward, but some require re-organizing code because a lot of Rails conventions assume ENV and to be defined at boot.

Posted on by:

ben profile

Ben Halpern

@ben

A Canadian software developer who thinks he’s funny. He/Him.

Discussion

markdown guide
 

Very nice! I saw that today with Stripe config and that it was auto populated from my Heroku env variables 😎