• Stars
    star
    20
  • Rank 1,083,315 (Top 22 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⚙ Settings for Ruby apps – fast, immutable, better.

Better Settings

Build Status Maintainability Test Coverage Gem Version License

A robust settings library for Ruby. Access your settings by calling methods on a safe immutable object.

Features ⚡️

  • 🚀 Light and Performant: settings are eagerly loaded, no method_missing tricks, no dependencies.
  • 💬 Useful Error Messages: when trying to access a setting that does not exist.
  • 💎 Immutability: once created settings can't be modified.
  • 🗂 Multiple Files: useful to create multiple environment-specific source files.
  • No Optional Setings: since it encourages unsafe access patterns.

You can read more about it in the blog announcement.

Installation 💿

Add this line to your application's Gemfile:

gem 'better_settings'

And then execute:

$ bundle

Or install it yourself as:

$ gem install better_settings

Usage 🚀

1. Define a class

Create a class in your application that extends BetterSettings:

# app/models/settings.rb
class Settings < BetterSettings
  source Rails.root.join('config', 'application.yml'), namespace: Rails.env
end

We use Rails.root in this example to obtain an absolute path to a plain YML file, but when using other Ruby frameworks you can use File.expand_path with __dir__ instead.

Also, we specified a namespace with the current environment. You can provide any value that corresponds to a key in the YAML file that you want to use. This allows to target different environments with the same file.

2. Create your settings

Now, create a YAML file that contains all the possible namespaces:

# config/application.yml
defaults: &defaults
  port: 80
  mailer:
    root: www.example.com
  dynamic: <%= "Did you know you can use ERB inside the YML file? Env is #{ Rails.env }." %>

development:
  <<: *defaults
  port: 3000

test:
  <<: *defaults

production:
  <<: *defaults

The defaults group in this example won't be used directly, we are using YAML's syntax to reuse those values when we use <<: *defaults, allowing us to share these values across environments.

3. Access your settings

You can use these settings anywhere, for example in a model:

class Post < ActiveRecord::Base
  self.per_page = Settings.pagination.posts_per_page
end

or in the console:

>> Rails.env
=> "development"

>> Settings.mailer
=> "#<Settings ... >"

>> Settings.mailer.root
=> "www.example.com

>> Settings.port
=> 3000

>> Settings.dynamic
=> "Did you know you can use ERB inside the YML file? Env is development."

Advanced Setup

You can create as many setting classes as you need, and name them in different ways, and read from as many files as necessary (nested keys will be merged).

The way I like to use it, is by reading a few optional files for the development and test environments, which allows each developer to override some settings in their own local environment (and git ignoring development.yml and test.yml).

# app/models/settings.rb
class Settings < BetterSettings
  source Rails.root.join('config/application.yml'), namespace: Rails.env
  source Rails.root.join('config/development.yml'), namespace: Rails.env, optional: true if Rails.env.development?
  source Rails.root.join('config/test.yml'), namespace: Rails.env, optional: true if Rails.env.test?
end

Then application.yml looks like this:

# application.yml
defaults: &defaults
  auto_logout: false
  secret_key_base: 'fake_secret_key_base'

server_defaults: &server_defaults
  <<: *defaults
  auto_logout: true
  secret_key: <%= ENV['SECRET_KEY'] %>

development:
  <<: *defaults
  host: 'localhost'

test:
  <<: *defaults
  host: '127.0.0.1'

staging:
  <<: *server_defaults
  host: 'staging.example.com'

production:
  <<: *server_defaults
  host: 'example.com'

A developer might want to override some settings by defining a development.yml such as:

development:
  auto_logout: true

The main advantage is that those changes won't be tracked in source control 😃

More Repositories

1

vite_ruby

⚡️ Vite.js in Ruby, bringing joy to your JavaScript experience
Ruby
1,124
star
2

iles

🏝 The joyful site generator
TypeScript
1,043
star
3

vite-plugin-image-presets

🖼 Image Presets for Vite.js apps
TypeScript
243
star
4

vite-plugin-environment

Easily expose environment variables in Vite.js
TypeScript
132
star
5

vite-plugin-full-reload

♻️ Automatically reload the page when files are modified
JavaScript
121
star
6

oj_serializers

⚡️ Faster JSON serialization for Ruby on Rails. Easily migrate away from Active Model Serializers.
Ruby
98
star
7

js_from_routes

🛣️ Generate path helpers and API methods from your Rails routes
Ruby
86
star
8

request_store_rails

📦 Per-request global storage for Rails prepared for multi-threaded apps
Ruby
83
star
9

types_from_serializers

✅ Generate TypeScript interfaces from your JSON serializers
Ruby
71
star
10

vuex-stores

🗄 Store objects for Vuex, a simple and more fluid API for state-management.
JavaScript
63
star
11

vue-custom-element-example

An example on how to define custom elements using Vue 3
TypeScript
54
star
12

mongoid_includes

🌿 Improves eager loading support for Mongoid
Ruby
46
star
13

jekyll-vite

⚡️🩸 Use Vite.js in Jekyll as your assets pipeline
Ruby
44
star
14

queryable

❔ Gives your queries a home and avoid tucking scopes inside your models
Ruby
42
star
15

vite-plugin-stimulus-hmr

⚡️ HMR for Stimulus controllers in Vite.js
TypeScript
42
star
16

stimulus-vite-helpers

Helpers to easily load all your Stimulus controllers when using Vite.js
TypeScript
37
star
17

capybara-compose

✅ Easily write fluent integration tests with Capybara in Ruby
Ruby
31
star
18

vite-plugin-bugsnag

Report builds and upload source maps to Bugsnag
TypeScript
18
star
19

i18n_multitenant

🌎 Provides a convenient way to use tenant-specific translations
Ruby
16
star
20

vite-plugin-manifest-sri

Subresource Integrity for Vite.js manifest files
JavaScript
13
star
21

resourcerer

✨ Works like magic to dry up your controllers
Ruby
10
star
22

sublime-toggle-dark-mode

🌚🌞 Toggle between dark and light mode in Sublime Text 4
JavaScript
9
star
23

pakiderm

🐘 Pakiderm will never forget the return value
Ruby
7
star
24

presenter_rails

🔭 Expose your view models in a convenient way
Ruby
6
star
25

vite-plugin-erb

Use ERB files in Vite.js projects with a Ruby backend
TypeScript
5
star
26

journeyman

Let your factories use your business logic, making them flexible and easier to update.
Ruby
5
star
27

jekyll-vite-minima

⚡️🩸 Use Vite.js in Jekyll minima theme as your assets pipeline
Ruby
3
star
28

automatic-music-transcription

Automatically exported from code.google.com
C
2
star
29

vite-plugin-xdm

Use XDM in VIte.js
JavaScript
2
star
30

crouton

🍞 Context sensitive notifications for Rails
Ruby
1
star
31

fast-food-mvc

Automatically exported from code.google.com
C#
1
star
32

ElMassimo

1
star
33

vite-vue-router-hmr-repro

Vue
1
star