https://github.com/jbox-web/active_settings
Settings in Rails, easy ;)
https://github.com/jbox-web/active_settings
configuration-file configuration-management rails ruby settings
Last synced: 2 days ago
JSON representation
Settings in Rails, easy ;)
- Host: GitHub
- URL: https://github.com/jbox-web/active_settings
- Owner: jbox-web
- License: mit
- Created: 2018-10-23T03:57:22.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-03-04T21:14:54.000Z (4 months ago)
- Last Synced: 2025-06-01T09:17:45.283Z (26 days ago)
- Topics: configuration-file, configuration-management, rails, ruby, settings
- Language: Ruby
- Homepage:
- Size: 87.9 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# ActiveSettings
[](https://github.com/jbox-web/active_settings/blob/master/LICENSE)
[](https://github.com/jbox-web/active_settings/releases/latest)
[](https://github.com/jbox-web/active_settings/actions)
[](https://codeclimate.com/github/jbox-web/active_settings)
[](https://codeclimate.com/github/jbox-web/active_settings/coverage)Settings in Rails.
It's heavily based on [config](https://github.com/rubyconfig/config) gem.
I made my own because I don't like the idea of having a [ghost class globally accessible](https://github.com/rubyconfig/config#accessing-the-settings-object) that I can't modify (What if I want to add some convenient methods on `Settings`?).
## Installation
Put this in your `Gemfile` :
```ruby
git_source(:github){ |repo_name| "https://github.com/#{repo_name}.git" }gem 'active_settings', github: 'jbox-web/active_settings', tag: '1.1.0'
```then run `bundle install`.
## Usage
### 1. Define your class
Instead of defining a `Settings` constant for you, that task is left to you. Simply create a class in your application
that looks like:```ruby
class Settings < ActiveSettings::Base
source Rails.root.join('config', 'settings.yml')
namespace Rails.env
end
```Name it `Settings`, name it `Config`, name it whatever you want. Add as many or as few as you like. A good place to put
this file in a Rails app is `config/settings.rb`### 2. Create your settings
Notice above we specified an absolute path to our settings file called `settings.yml`. This is just a typical YAML file.
Also notice above that we specified a namespace for our environment. A namespace is just an optional string that corresponds
to a key in the YAML file.Using a namespace allows us to change our configuration depending on our environment:
```yaml
# config/settings.yml
defaults: &defaults
cool:
saweet: nested settings
neat_setting: 24
awesome_setting: <%= "Did you know 5 + 5 = #{5 + 5}?" %>development:
<<: *defaults
neat_setting: 800test:
<<: *defaultsproduction:
<<: *defaults
```Keys are both accessible with a string or a symbol.
### 3. Access your settings
You can use different methods to access to values :
* by using method chains :
```ruby
>> Rails.env
=> "development">> Settings.cool
=> "#">> Settings.cool.saweet
=> "nested settings">> Settings.neat_setting
=> 800>> Settings.awesome_setting
=> "Did you know 5 + 5 = 10?"
```* by using `fetch` method :
```ruby
>> Settings.cool.fetch(:saweet)
=> "nested settings">> Settings.cool.fetch('saweet')
=> "nested settings"
```You can provide default value :
```ruby
>> Settings.cool.fetch(:foo, 'bar')
=> "bar">> Settings.cool.fetch(:foo) { 'bar' }
=> "bar"
```* by using `[]` accessor :
```ruby
>> Settings[:cool][:saweet]
=> "nested settings">> Settings['cool']['saweet']
=> "nested settings"
```* by using `dig` method :
```ruby
>> Settings.dig(:cool, :saweet)
=> "nested settings">> Settings.dig('cool', 'saweet')
=> "nested settings"
```* by using `key?` method :
```ruby
>> Settings.cool.key?(:saweet)
=> "true">> Settings.cool.key?('saweet')
=> "true"
```You can use these settings anywhere, for example in a model:
```ruby
class Post < ActiveRecord::Base
self.per_page = Settings.pagination.posts_per_page
end
```