Top Related Projects
Global settings for your Rails application.
Simple Rails app configuration
A Ruby gem to load environment variables from `.env`.
Quick Overview
Rails-settings is a Ruby gem that provides a simple way to add key-value store functionality to ActiveRecord models in Ruby on Rails applications. It allows developers to easily add, retrieve, and manage settings for individual model instances or globally across the application.
Pros
- Easy integration with existing Rails models
- Supports both global and per-instance settings
- Provides type casting for stored values
- Offers a clean and intuitive API
Cons
- Limited to ActiveRecord-based applications
- May introduce additional database queries for frequent setting access
- Requires manual migration creation for setting tables
Code Examples
- Defining settings for a model:
class User < ApplicationRecord
include RailsSettings::Extend
field :newsletter, type: :boolean, default: true
field :font_size, type: :integer, default: 12
field :theme, type: :string, default: 'light'
end
- Accessing and modifying settings:
user = User.first
user.settings.newsletter # => true
user.settings.font_size # => 12
user.settings.theme = 'dark'
user.settings.save!
- Using global settings:
RailsSettings::Setting.site_name = "My Awesome Site"
RailsSettings::Setting.site_name # => "My Awesome Site"
RailsSettings::Setting.all
# => {"site_name"=>"My Awesome Site"}
Getting Started
- Add the gem to your Gemfile:
gem 'rails-settings'
- Run bundle install:
bundle install
- Generate and run the migration:
rails generate rails_settings:migration
rails db:migrate
- Include the module in your model:
class User < ApplicationRecord
include RailsSettings::Extend
# Define your settings fields here
field :example_setting, type: :string, default: 'default value'
end
- Start using settings in your application:
user = User.first
user.settings.example_setting = 'new value'
user.settings.save!
Competitor Comparisons
Global settings for your Rails application.
Pros of rails-settings-cached
- Built-in caching mechanism for improved performance
- Supports default values for settings
- Includes a generator for easy setup
Cons of rails-settings-cached
- Less actively maintained (last update over 2 years ago)
- Fewer configuration options compared to rails-settings
- Limited documentation and examples
Code Comparison
rails-settings-cached:
Setting.admin_password = 'supersecret'
Setting.admin_password # => "supersecret"
Setting.defaults[:app_name] = "My App"
Setting.app_name # => "My App"
rails-settings:
Settings.colors = {
background: '#fff',
foreground: '#000'
}
Settings.colors.background # => "#fff"
Settings.colors[:foreground] # => "#000"
Both libraries provide similar functionality for managing application settings in Rails. rails-settings-cached focuses on performance with built-in caching, while rails-settings offers more flexibility in configuration and data types. rails-settings is more actively maintained, with recent updates and better documentation. However, if caching is a priority for your project, rails-settings-cached might be a better choice. Consider your specific needs and the trade-offs between performance and flexibility when choosing between these two libraries.
Simple Rails app configuration
Pros of Figaro
- Focuses on environment-specific configuration and sensitive data management
- Seamlessly integrates with Heroku and other cloud platforms
- Provides a simple and secure way to manage application secrets
Cons of Figaro
- Limited to managing environment variables
- Doesn't provide a database-backed solution for dynamic settings
- Less suitable for runtime-configurable application settings
Code Comparison
Figaro:
# config/application.yml
GMAIL_USERNAME: "user@example.com"
GMAIL_PASSWORD: "password123"
# Usage
Figaro.env.gmail_username
Rails-settings:
# In a model or controller
Settings.gmail_username = "user@example.com"
Settings.gmail_password = "password123"
# Usage
Settings.gmail_username
Key Differences
Figaro is primarily designed for managing environment-specific configuration and sensitive data through environment variables. It's particularly useful for deploying to platforms like Heroku.
Rails-settings, on the other hand, provides a database-backed solution for managing application settings. It allows for more dynamic, runtime-configurable settings and is better suited for user-specific or frequently changing configurations.
Choose Figaro if you need a simple solution for managing environment variables and application secrets, especially when deploying to cloud platforms. Opt for Rails-settings if you require a more flexible, database-backed solution for managing application settings that can be changed at runtime.
A Ruby gem to load environment variables from `.env`.
Pros of dotenv
- Simple and lightweight, focusing solely on environment variable management
- Language-agnostic, can be used with various programming languages and frameworks
- Easy to integrate into existing projects with minimal setup
Cons of dotenv
- Limited to managing environment variables only, not suitable for complex configuration needs
- Lacks built-in support for different environments (e.g., development, production)
- No built-in encryption or secure storage for sensitive data
Code Comparison
dotenv:
require 'dotenv'
Dotenv.load
database_url = ENV['DATABASE_URL']
rails-settings:
Setting.database_url = 'postgres://localhost/myapp'
database_url = Setting.database_url
Key Differences
- Scope: dotenv focuses on environment variables, while rails-settings provides a more comprehensive settings management solution
- Persistence: dotenv loads variables from files, whereas rails-settings stores settings in the database
- Flexibility: rails-settings offers more advanced features like default values, data types, and scopes
- Integration: dotenv is more universal, while rails-settings is tailored for Ruby on Rails applications
Use Cases
- Choose dotenv for simple environment variable management across different languages and platforms
- Opt for rails-settings when working with Ruby on Rails and requiring more advanced configuration management features
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Settings for Rails
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:
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-2024 Georg Ledermann
This gem is a complete rewrite of rails-settings by Alex Wayne
Top Related Projects
Global settings for your Rails application.
Simple Rails app configuration
A Ruby gem to load environment variables from `.env`.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot