moiristo / settler

Settler can be used for defining application wide settings in ruby
MIT License
11 stars 5 forks source link

Settler

<img src="https://travis-ci.org/moiristo/settler.svg?branch=master" alt="Build Status" />

Settler can be used for defining application wide settings in Ruby. Settings are loaded from a YAML file and stored in the database using ActiveRecord to allow users to update settings on the fly. The YAML configuration allows you to not only specify defaults, but setting value validations and typecasts as well!

NOTE

Settler v3 has not been released yet! You can find the latest v2 release code and docs here: https://github.com/moiristo/settler/tree/a2ed5a5d9e6932aa7ae68a2c2d4c3eef2a10c608

Requirements

Supported ORMs

Installation

Add the gem to your Gemfile:

gem 'settler'

Upgrading from v2

In settler v3, settler objects have been namespaced under the namespace Settler. Please update your sources accordingly:

Setting     => Settler::ORM::Activerecord::Setting
Typecaster  => Settler::Typecaster

Setup

General

Rails/Activerecord

Settings

Configuration

The initial version of settler.yml contains an example settings for specifying a google analytics key. A setting consists of at least a key and a value. Consider the following example:

google_analytics_key:
  label: Google analytics key
  value: 'UA-xxxxxx-x'
  editable: true
  deletable: false
  validations:
    presence: true

In this case, google_analytics_key is the key of the setting. Every nested property is either an attribute of the setting or a list of validations. Label, value, editable and deletable are attributes of the Settler::Setting model. If a setting with a given key does not exist, it is created with the attributes found. Therefore, you can consider these attributes as the default values for the setting. See the validations section for more info on validations.

Note that you can use ERB in the configuration file if you need to. For example:

google_analytics_key:
  value: '<%= GOOGLE_ANALYTICS_KEY %>'

will evaluate the GOOGLE_ANALYTICS_KEY constant.

Access settings

Typecasting

When you require this class in your application, you can easily use your typecaster by specifying its class name in the settler configuration:

  custom_value:
    label: An integer that should be custom typecasted
    value: 1
    typecast: CustomTypecaster

Validations

Activerecord

Changing / Destroying settings

Settings are represented by your ORM of choice. In the case of ActiveRecord, you can just update and destroy settings like you would update or destroy any AR model.

Activerecord

Ruby

Advanced usage

Note on Patches/Pull Requests

Copyright

Copyright (c) 2016 Reinier de Lange. See LICENSE for details.