Manage settings with Ruby on Rails
Ruby gem to handle settings for ActiveRecord instances by storing them as serialized Hash in a separate database table. Namespaces and defaults included.
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
class User { :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 'ProjectSettingObject'
end
class ProjectSettingObject 'black'
user.settings(:calendar).update! :scope => 'all', :display => 'daily'
user = User.find(1)
user.settings(:dashboard).theme
# => 'black
user.settings(:dashboard).view
# => 'monthly' (it's the default)
user.settings(:calendar).scope
# => 'all'
user = User.find(1)
user.settings(:dashboard).update! :theme => nil
user.settings(:dashboard).view = nil
user.settings(:dashboard).save!
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 'calendar'
User.without_settings_for(:calendar)
# => all users without having settings for 'calendar'
User.includes(:setting_objects)
# => Eager load setting_objects when querying many users
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.
See https://github.com/ledermann/rails-settings/releases
MIT License
Copyright (c) 2012-2024 Georg Ledermann
This gem is a complete rewrite of rails-settings by Alex Wayne
No open issues yet, or sync has not completed.