百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rails-settings-cached

> 后端框架
开源

您的 Rails 应用程序的全局设置。

1.1K stars0 点赞1 次浏览
访问官网GitHub

工具介绍

您的 Rails 应用程序的全局设置。

Rails Settings Cached

The best solution for store global settings in Rails applications.

This gem will make managing a table of а global key, value pairs easy. Think of it like a global Hash stored in your database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you don't want to hard code into your Rails application.

Installation

Requires Ruby 3.2+ and Rails 7.0+. For older Ruby or Rails versions, use the 2.9.x releases.

Edit your Gemfile:

bash
$ bundle add rails-settings-cached

Generate your settings:

bash
$ rails g settings:install

# Or use a custom name:
$ rails g settings:install AppConfig

You will get app/models/setting.rb

…

You must use the field method to statement the setting keys, otherwise you can't use it.

The scope method allows you to group the keys for admin UI.

Now just put that migration in the database with:

bash
$ rails db:migrate

Usage

The syntax is easy. First, let's create some settings to keep track of:

…

Get defined fields

version 2.3+

…

Custom type for setting

Since: 2.9.0

You can write your custom field type by under RailsSettings::Fields module.

For example

rb
module RailsSettings
  module Fields
    class YesNo  Setting.custom_item = 'YES'
irb> Setting.custom_item
true
irb> Setting.custom_item = 'NO'
irb> Setting.custom_item
false

Get All defined fields

version 2.7.0+

You can use defined_fields method to get all defined fields in Setting.

rb
# Get editable fields and group by scope
editable_fields = Setting.defined_fields
  .select { |field| !field[:readonly] }
  .group_by { |field| field[:scope] }

Validations

You can use validates options to special the Rails Validation for fields.

rb
class Setting  Setting.app_name = ""
ActiveRecord::RecordInvalid: (Validation failed: App name can't be blank)
irb> Setting.app_name = "Rails Settings"
"Rails Settings"
irb> Setting.default_locale = "zh-TW"
ActiveRecord::RecordInvalid: (Validation failed: Default locale is not included in [zh-CN, en, jp])
irb> Setting.default_locale = "en"
"en"

Validate by save / valid? method:

rb

setting = Setting.find_or_initialize_by(var: :app_name)
setting.value = ""
setting.valid?
# => false
setting.errors.full_messages
# => ["App name can't be blank", "App name too short (minimum is 2 characters)"]

setting = Setting.find_or_initialize_by(var: :default_locale)
setting.value = "zh-TW"
setting.save
# => false
setting.errors.full_messages
# => ["Default locale is not included in [zh-CN, en, jp]"]
setting.value = "en"
setting.valid?
# => true

Use Setting in Rails initializing:

In version 2.3+ you can use Setting before Rails is initialized.

For example config/initializers/devise.rb

rb
Devise.setup do |config|
  if Setting.omniauth_google_client_id.present?
    config.omniauth :google_oauth2, Setting.omniauth_google_client_id, Setting.omniauth_google_client_secret
  end
end
rb
class Setting  Check Cache -> Exist - Get value of key for cache -> Return
                   |
                Fetch all key and values from DB -> Write Cache -> Get value of key for cache -> return
                   |
                Return default value or nil

In each Setting keys call, we will load the cache/db and save in ActiveSupport::CurrentAttributes to avoid hit cache/db.

Each key update will expire the cache, so do not add some frequent update key.

Change cache key

Some times you may need to force update cache, now you can use cache_prefix

ruby
class Setting 
  
    

  

  

  

  

  

  

  

Special Cache Storage

You can use cache_store to change cache storage, default is Rails.cache.

Add config/initializers/rails_settings.rb

rb
RailsSettings.configure do
  self.cache_storage = ActiveSupport::Cache::RedisCacheStore.new(url: "redis://localhost:6379")
end

Scoped Settings

BREAK CHANGES WARNING: rails-settings-cached 2.x has redesigned the API, the new version will compatible with the stored setting values by an older version. When you want to upgrade 2.x, you must read the README again, and follow guides to change your Setting model. 0.x stable branch: https://github.com/huacnlee/rails-settings-cached/tree/0.x

  • Backward compatible to support 0.x scoped settings

For new project / new user of rails-settings-cached. The ActiveRecord::AttributeMethods::Serialization is best choice.

This is reason of why rails-settings-cached 2.x removed Scoped Settings feature.

For example:

We wants a preferences setting for user.

rb
class User < ActiveRecord::Base
  serialize :preferences
end

@user = User.new
@user.preferences[:receive_emails] = true
@user.preferences[:public_email] = true
@user.save

Use cases:

  • ruby-china/homeland - master
  • forem/forem - 2.x
  • siwapp/siwapp - 2.x
  • aidewoode/black_candy - 2.x
  • huacnlee/bluedoc - 2.x
  • getzealot/zealot - 2.x
  • kaishuu0123/rebacklogs - 2.x
  • texterify/texterify - 2.x
  • mastodon/mastodon - 0.6.x
  • helpyio/helpy - 0.5.x
  • maybe-finance/maybe - 2.x

And more than 1K repositories used.

Contributing

See CONTRIBUTING.md for how to set up a development environment, run the test suite and submit a pull request.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyconfigconfigurationrailssetting

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架