Settingify

This is small global settings system with some features:

  1. Works without table returning default values. Migrating db loaded the whole rails environment and using settings in lazy code blocks (like activeadmin resource files) may cause problems.
  2. May works with admin systems like simple model. For example, integration with active admin:
rails g active_admin:resource Settingify::Setting

Installation

Add this line to your application's Gemfile:

gem 'settingify', '0.0.2'

And then execute:

$ bundle

Or install it yourself as:

$ gem install settingify -v 0.0.2

Post installation

Run intallation generator for installing migration and initializer:

rails g settingify:install

Then migrate:

rake db:migrate

Usage

Defining settings

It may be done by patching created initializer in config/initializers/settingify.rb. There is simple DSL:

Settingify.prepare_settings do
  setting :some_name, type: SettingType, default: 'some default value'
end

Supported types are String, Integer and Float. But you may define own convertors. There are 2 conditions for that:

  1. Convertor must contains class method ConvertorClass.try_convert
  2. This method accepts string value

Also exists ability for set type as passing class or simple a symbol. For example:

Settingify.prepare_settings do
  setting :some_name, type: :string, default: 'some default value'
end

Default type is string.

Reading settings

There are two cases:

  1. Setting table is not exists - the default value will be returned.
  2. Setting table is exists - value from record will be returned.
  3. Setting table is exists and record not found - the default value will be returned too.

According to example above to read setting use the next code:

Settingify.some_name

Syncing settings

When you define settings in initializer this is no effect on DB. But in most cases you (as site administator) want to change some settings without redeploy the whole application. For this you may add settings to the DB manually or run built-in rake task:

bundle exec rake settingify:sync

Contributing

  1. Fork it ( https://github.com/alterego-labs/settingify/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request