• Stars
    star
    975
  • Rank 45,111 (Top 1.0 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 15 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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 3.0 or newer
  • Rails 6.1 or newer (including Rails 7.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

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

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

module UserDashboardConcern
  extend ActiveSupport::Concern

  included do
    has_settings persistent: true do |s|
      s.key :dashboard
    end
  end
end

class User < ActiveRecord::Base
  has_settings persistent: true do |s|
    s.key :calendar
  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! :theme => 'black'
user.settings(:calendar).update! :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! :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 '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-2023 Georg Ledermann

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

More Repositories

1

docker-rails

Dockerize Rails 7 with ActionCable, Webpacker, Stimulus, Elasticsearch, Sidekiq
Ruby
1,105
star
2

unread

Handle unread records and mark them as read with Ruby on Rails
Ruby
710
star
3

docker-rails-base

Optimized Docker image for Rails applications
Dockerfile
287
star
4

pingcrm

PingCRM on Rails - A Ruby on Rails demo application to illustrate how Inertia.js works
Ruby
238
star
5

templatus-hotwire

Opinionated template for starting new web applications with Ruby on Rails and Hotwire
Ruby
89
star
6

keepr

Double entry bookkeeping with Ruby on Rails
Ruby
88
star
7

docker-vue

Frontend for DockerRails, built with Vue.js
Vue
84
star
8

drafting

Ruby gem for saving drafts of ActiveRecord models
Ruby
64
star
9

datev

Ruby gem for DATEV exports via CSV
Ruby
53
star
10

templatus

Opinionated template for starting new web applications with Ruby on Rails and Vue.js 3
Ruby
45
star
11

workcation

How to use Inertia.js to build a Vue.js frontend within a Ruby on Rails application (ARCHIVED)
Ruby
45
star
12

tinnef

Ruby wrapper for "tnef"
Ruby
13
star
13

blurhash-vue

Demo app to show progressive image loading with Blurhash
Vue
11
star
14

address_parser

Ruby
9
star
15

ibanomat

Ruby wrapper to calculate the IBAN of german bank account numbers
Ruby
7
star
16

docker-base

Base Docker image to build container for Ruby applications
5
star
17

dialy

Formatting phone numbers in E.123
Ruby
5
star
18

redundant_links

Automatic handling of a join table for indirect (transitive) associations.
Ruby
2
star
19

staticmatic-demo

Example of using StaticMatic to build static website with Ruby, HAML and SAS
Ruby
1
star