Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ledermann/rails-settings
Manage settings with Ruby on Rails
https://github.com/ledermann/rails-settings
configuration rails ruby rubygems settings
Last synced: 3 days ago
JSON representation
Manage settings with Ruby on Rails
- Host: GitHub
- URL: https://github.com/ledermann/rails-settings
- Owner: ledermann
- License: mit
- Created: 2009-05-16T07:00:46.000Z (over 15 years ago)
- Default Branch: master
- Last Pushed: 2024-12-29T10:25:19.000Z (24 days ago)
- Last Synced: 2025-01-11T23:00:32.259Z (10 days ago)
- Topics: configuration, rails, ruby, rubygems, settings
- Language: Ruby
- Homepage:
- Size: 305 KB
- Stars: 1,011
- Watchers: 25
- Forks: 138
- Open Issues: 23
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
- awesome-ruby-toolbox - ledermann-rails-settings - Settings gem for Ruby on Rails (Developer Tools / Configuration Management)
README
# Settings for Rails
[![Build Status](https://github.com/ledermann/rails-settings/actions/workflows/main.yml/badge.svg)](https://github.com/ledermann/rails-settings/actions)
[![Code Climate](https://codeclimate.com/github/ledermann/rails-settings.svg)](https://codeclimate.com/github/ledermann/rails-settings)
[![Coverage Status](https://coveralls.io/repos/ledermann/rails-settings/badge.svg?branch=master)](https://coveralls.io/r/ledermann/rails-settings?branch=master)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 2.7 or newer
- Rails 6.1 or newer (including Rails 7.0)## Installation
Include the gem in your Gemfile and run `bundle` to install it:
```ruby
gem 'ledermann-rails-settings'
```Generate and run the migration:
```shell
rails g rails_settings:migration
rake db:migrate
```## Usage
### Define settings
```ruby
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:
```ruby
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:
```ruby
class Project < ActiveRecord::Base
has_settings :info, :class_name => 'ProjectSettingObject'
endclass 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
```In case you need to define settings separatedly for the same models, you can use the persistent option
```ruby
module UserDashboardConcern
extend ActiveSupport::Concernincluded do
has_settings persistent: true do |s|
s.key :dashboard
end
end
endclass User < ActiveRecord::Base
has_settings persistent: true do |s|
s.key :calendar
end
end
```### Set settings
```ruby
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
```ruby
user = User.find(1)
user.settings(:dashboard).update! :theme => 'black'
user.settings(:calendar).update! :scope => 'all', :display => 'daily'
```### Get settings
```ruby
user = User.find(1)
user.settings(:dashboard).theme
# => 'blackuser.settings(:dashboard).view
# => 'monthly' (it's the default)user.settings(:calendar).scope
# => 'all'
```### Delete settings
```ruby
user = User.find(1)
user.settings(:dashboard).update! :theme => niluser.settings(:dashboard).view = nil
user.settings(:dashboard).save!
```### Using scopes
```ruby
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
```ruby
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](https://github.com/ledermann/rails-settings/commits/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](https://ledermann.dev)
This gem is a complete rewrite of [rails-settings](https://github.com/Squeegy/rails-settings) by [Alex Wayne](https://github.com/Squeegy)