All Projects → ledermann → Rails Settings

ledermann / Rails Settings

Licence: mit
Manage settings with Ruby on Rails

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Rails Settings

config
Config component, strictly typed
Stars: ✭ 14 (-98.27%)
Mutual labels:  settings, configuration
Materialize Sass
Materializecss rubygem for Rails Asset Pipeline / Sprockets
Stars: ✭ 785 (-2.73%)
Mutual labels:  rails, rubygems
gconfigs
gConfigs - Config and Secret parser
Stars: ✭ 42 (-94.8%)
Mutual labels:  settings, configuration
cfg-rs
A Configuration Library for Rust Applications
Stars: ✭ 18 (-97.77%)
Mutual labels:  settings, configuration
Anyway config
Configuration library for Ruby gems and applications
Stars: ✭ 409 (-49.32%)
Mutual labels:  rails, configuration
JsonSettings
This library simplifies creating configuration for your C# app/service by utilizing the serialization capabilities of Json.NET to serialize nested (custom) objects, dictionaries and lists as simply as by creating a POCO and inheriting JsonSettings class.
Stars: ✭ 59 (-92.69%)
Mutual labels:  settings, configuration
Pluck to hash
Extend ActiveRecord pluck to return array of hashes
Stars: ✭ 275 (-65.92%)
Mutual labels:  rails, rubygems
Configuration
A module to help other modules have settings
Stars: ✭ 135 (-83.27%)
Mutual labels:  settings, configuration
Deep pluck
Allow you to pluck attributes from nested associations without loading a bunch of records.
Stars: ✭ 385 (-52.29%)
Mutual labels:  rails, rubygems
Senparc.co2net
支持 .NET Framework & .NET Core 的公共基础扩展库
Stars: ✭ 289 (-64.19%)
Mutual labels:  settings, configuration
setset
Powerful Incremental Type-driven Settings Engine.
Stars: ✭ 20 (-97.52%)
Mutual labels:  settings, configuration
Mailcatcher
Catches mail and serves it through a dream.
Stars: ✭ 5,512 (+583.02%)
Mutual labels:  rails, rubygems
Django Dynamic Preferences
Dynamic global and instance settings for your django project
Stars: ✭ 238 (-70.51%)
Mutual labels:  settings, configuration
climatecontrol
Python library for loading settings and config data from files and environment variables
Stars: ✭ 20 (-97.52%)
Mutual labels:  settings, configuration
Simple Settings
A simple way to manage your project settings.
Stars: ✭ 165 (-79.55%)
Mutual labels:  settings, configuration
rails-settings-ui
User interface for manage settings in rails application (using rails-settings gem) / Интерфейс для управления настройками в Rails приложении
Stars: ✭ 93 (-88.48%)
Mutual labels:  settings, configuration
Dynaconf
Configuration Management for Python ⚙
Stars: ✭ 2,082 (+157.99%)
Mutual labels:  settings, configuration
Settings.net
⚙️ Settings.Net - An easy to use .NET library for accessing and storing settings and configurations.
Stars: ✭ 114 (-85.87%)
Mutual labels:  settings, configuration
Dry Configurable
A simple mixin to make Ruby classes configurable
Stars: ✭ 280 (-65.3%)
Mutual labels:  settings, configuration
Sail
Sail is a lightweight Rails engine that brings an admin panel for managing configuration settings on a live Rails app
Stars: ✭ 484 (-40.02%)
Mutual labels:  settings, 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 2.5 or newer
  • Rails 4.2 or newer (including Rails 6.1)

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-2021 Georg Ledermann

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].