Skip to content

ledermann/rails-settings

Repository files navigation

Settings for Rails

Build StatusCode ClimateCoverage 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 3.2 or newer
  • Rails 6.1 or newer (including Rails 8.0)

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

classUser < ActiveRecord::Basehas_settingsdo |s| s.key:dashboard,:defaults=>{:theme=>'blue',:view=>'monthly',:filter=>false}s.key:calendar,:defaults=>{:scope=>'company'}endend

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

classUser < ActiveRecord::Basehas_settings:dashboard,:calendarend

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

classProject < ActiveRecord::Basehas_settings:info,:class_name=>'ProjectSettingObject'endclassProjectSettingObject < RailsSettings::SettingObjectvalidatedounlessself.owner_name.present? && self.owner_name.is_a?(String)errors.add(:base,"Owner name is missing")endendend

In case you need to define settings separatedly for the same models, you can use the persistent option

moduleUserDashboardConcernextendActiveSupport::Concernincludeddohas_settingspersistent: truedo |s| s.key:dashboardendendendclassUser < ActiveRecord::Basehas_settingspersistent: truedo |s| s.key:calendarendend

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!:theme=>'black'user.settings(:calendar).update!:scope=>'all',:display=>'daily'

Get settings

user=User.find(1)user.settings(:dashboard).theme# => 'blackuser.settings(:dashboard).view# => 'monthly' (it's the default)user.settings(:calendar).scope# => 'all'

Delete settings

user=User.find(1)user.settings(:dashboard).update!:theme=>niluser.settings(:dashboard).view=niluser.settings(:dashboard).save!

Using scopes

User.with_settings# => all users having any settingUser.without_settings# => all users without having any settingUser.with_settings_for(:calendar)# => all users having a setting for 'calendar'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 (c) 2012-2024 Georg Ledermann

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

About

Manage settings with Ruby on Rails

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 22

Languages