Mournful Settings

Adds a settings class to a rails app. The settings are mournful because they can be stored encrypted. Aren’t puns wonderful.

Installation

gem mournful_settings

Setting are stored in a database table ‘mournful_settings_settings’. To add mournful_settings migrations to the host app run this rake task:

rake mournful_settings:install:migrations

Then run ‘rake db:migrate’ to create the ‘mournful_settings_settings’ table

Usage

In the host rails app, create a class you wish to use as the object to hold settings, and have it inherit from MournfulSettings::Setting. For example (/app/models/settings.rb)

class Setting < MournfulSettings::Setting 
end

Fields

Each setting has five fields:

name

Identifies the setting. Used in ‘for’ (see below)

value

The value being stored.

value_type

Values are stored as strings. value_type defines how that string should be presented. For example, ‘1.23’ with value_type ‘number’ will be presented as numeric 1.23. If the value_type was ‘text’ the value returned would be ‘1.23’.

description

Information about the setting being stored

encrypted

Boolean: If set to true, the value will be stored in an encrypted format. Otherwise the value will be stored as plain text.

Retrieving a setting

To use a stored setting, use the ‘for’ class method:

Setting.create(:name => 'pi', :value => '3.14159', :value_type => 'number')

Setting.for(:pi)    -->   3.14159

In this example, Setting.for(:pi) will return nil if there is no Setting with a name of ‘pi’ in the database.

Supplying a default

If you wish an alternative value to be returned if no matching setting has been defined, you can add a default to the for declaration.

Setting.for(:pi, 3.14)

This will return 3.14 until a ‘pi’ setting has been created.

The

Encryption

By default mournful settings are encrypted. You can choose not to encrypt a setting, by setting :encrypted => false.

Setting.create(
  :name => 'pi', 
  :value => '3.14159', 
  :value_type => 'number',
  :encrypted => false
)

Out of the box, encryption uses a blowfish cipher, and a generic key string.

Set key and cipher

If you wish to use your own encryption key, you can define the key like this:

MournfulSettings::Setting::Cipher.key = 'your key'

Mournful settings uses Ruby’s OpenSSL::Cipher. If you wish to change the cipher from blowfish, you can alter it like this:

MournfulSettings::Setting::Cipher.config = 'aes-128-cbc'

To see a list of the available options use:

puts OpenSSL::Cipher.ciphers

See: ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL/Cipher.html

Where to set the cipher within your app

If you use a setting in an initializer you need to ensure that your cipher configuration is set before the setting is used. This means you either need to order your initializers putting your mournful_settings initializer first or define the cipher settings in a before_initialize block defined in config/application:

module YourRailsApp
  class Application < Rails::Application

    .....

    config.before_initialize do
      MournfulSettings::Setting::Cipher.key = 'your key'
    end
  end
end

See: guides.rubyonrails.org/configuring.html#initialization-events

Changing key and/or cipher

If you change the cipher configuration, existing encrypted settings will break. Therefore, to make the change after you have started using encrypted settings, you must decrypt your settings, make the change and then re-encrypt the settings again. To ease this task, use the Setting.recrypt_all method:

Setting.recrypt_all { Setting::Cipher.key = 'your key' }

So the process would be:

  • Stop the server

  • Run the recrypt task

  • Add/Update the configuration code in the app

  • Start the server

Integration with ActiveAdmin

Mournful settings contains an ActiveAdmin register file, that allows settings to be managed from within the parent app’s active_admin space. Of course ActiveAdmin needs to be installed and working in the parent rails application, for this to work.

To use the Mournful settings’ ActiveAdmin register files, add this to the active_admin initializer in your application.

config.load_paths << MournfulSettings.active_admin_load_path

Alternatively, copy lib/active_admin/admin/setting.rb to app/admin within the host rails app, and modify it as you desire.