rails-settings 0 travis-ci ruby Rubygems

Manage settings with Ruby on Rails

Settings for Rails

Build Status Code Climate Coverage Status

Ruby gem to handle settings for ActiveRecord instances by storing them as serialized Hash in a separate database table. Namespaces and defaults included.

Requirements

  • Ruby 1.9.3 or newer
  • Rails 3.1 or newer (including Rails 4.x and Rails 5)

Installation

Include the gem in your Gemfile and run bundle to install it:

gem 'ledermann-rails-settings'

Generate and run the migration:

rails g rails_settings:migration
rake db:migrate

Usage

Define settings

class User < ActiveRecord::Base
  has_settings do |s|
    s.key :dashboard, :defaults => { :theme => 'blue', :view => 'monthly', :filter => false }
    s.key :calendar,  :defaults => { :scope => 'company'}
  end
end

If no defaults are needed, a simplified syntax can be used:

class User < ActiveRecord::Base
  has_settings :dashboard, :calendar
end

Every setting is handled by the class RailsSettings::SettingObject. You can use your own class, e.g. for validations:

class Project < ActiveRecord::Base
  has_settings :info, :class_name => 'ProjectSettingObject'
end

class ProjectSettingObject < RailsSettings::SettingObject
  validate do
    unless self.owner_name.present? && self.owner_name.is_a?(String)
      errors.add(:base, "Owner name is missing")
    end
  end
end

Set settings

user = User.find(1)
user.settings(:dashboard).theme = 'black'
user.settings(:calendar).scope = 'all'
user.settings(:calendar).display = 'daily'
user.save! # saves new or changed settings, too

or

user = User.find(1)
user.settings(:dashboard).update_attributes! :theme => 'black'
user.settings(:calendar).update_attributes! :scope => 'all', :display => 'daily'

Get settings

user = User.find(1)
user.settings(:dashboard).theme
# => 'black

user.settings(:dashboard).view
# => 'monthly'  (it's the default)

user.settings(:calendar).scope
# => 'all'

Delete settings

user = User.find(1)
user.settings(:dashboard).update_attributes! :theme => nil

user.settings(:dashboard).view = nil
user.settings(:dashboard).save!

Using scopes

User.with_settings
# => all users having any setting

User.without_settings
# => all users without having any setting

User.with_settings_for(:calendar)
# => all users having a setting for 'calender'

User.without_settings_for(:calendar)
# => all users without having settings for 'calendar'

Eager Loading

User.includes(:setting_objects)
# => Eager load setting_objects when querying many users

Compatibility

Version 2 is a complete rewrite and has a new DSL, so it’s not compatible with Version 1. In addition, Rails 2.3 is not supported anymore. But the database schema is unchanged, so you can continue to use the data created by 1.x, no conversion is needed.

If you don’t want to upgrade, you find the old version in the 1.x branch. But don’t expect any updates there.

Changelog

See https://github.com/ledermann/rails-settings/releases

License

MIT License

Copyright © 2012-2016 Georg Ledermann

This gem is a complete rewrite of rails-settings by Alex Wayne

Related Repositories

config

config

Easiest way to add multi-environment yaml settings to Rails, Sinatra, Pandrino and other Ruby projects. ...

rails-settings-cached

rails-settings-cached

This is improved from rails-settings, added caching for all settings ...

rails-settings

rails-settings

Manage settings with Ruby on Rails ...

rails-settings

rails-settings

Settings is a plugin that makes managing a table of global key, value pairs easy. Think of it like a global Hash stored in your database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you don't want to ...

setler

setler

Setler lets you use the 'Feature Flags' pattern or add settings to models in Rails ...


Top Contributors

ledermann jimryan Squeegy mguymon hankwallace proteusvacuum doncote marcferna mceachen mduong michaelklishin yemartin zmillman

Releases

-   v2.4.2 zip tar
-   v2.4.1 zip tar
-   v2.4.0 zip tar
-   v2.3.0 zip tar
-   v2.2.1 zip tar
-   v2.2.0 zip tar
-   v2.1.0 zip tar
-   v2.0.3 zip tar
-   v2.0.2 zip tar
-   v2.0.1 zip tar
-   v2.0.0 zip tar
-   v1.2.1 zip tar
-   v1.2.0 zip tar
-   v1.1.0 zip tar
-   v1.0.1 zip tar
-   v1.0.0 zip tar