您的 Rails 应用程序的全局设置。
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.
Requires Ruby 3.2+ and Rails 7.0+. For older Ruby or Rails versions, use the 2.9.x releases.
Edit your Gemfile:
$ bundle add rails-settings-cachedGenerate your settings:
$ rails g settings:install
# Or use a custom name:
$ rails g settings:install AppConfigYou 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:
$ rails db:migrateThe syntax is easy. First, let's create some settings to keep track of:
…version 2.3+
…Since: 2.9.0
You can write your custom field type by under RailsSettings::Fields module.
module RailsSettings
module Fields
class YesNo Setting.custom_item = 'YES'
irb> Setting.custom_item
true
irb> Setting.custom_item = 'NO'
irb> Setting.custom_item
falseversion 2.7.0+
You can use defined_fields method to get all defined fields in Setting.
# Get editable fields and group by scope
editable_fields = Setting.defined_fields
.select { |field| !field[:readonly] }
.group_by { |field| field[:scope] }You can use validates options to special the Rails Validation for fields.
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:
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?
# => trueIn version 2.3+ you can use Setting before Rails is initialized.
For example config/initializers/devise.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
endclass 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 nilIn 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.
Some times you may need to force update cache, now you can use cache_prefix
class Setting
You can use cache_store to change cache storage, default is Rails.cache.
Add config/initializers/rails_settings.rb
RailsSettings.configure do
self.cache_storage = ActiveSupport::Cache::RedisCacheStore.new(url: "redis://localhost:6379")
endBREAK 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
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.
class User < ActiveRecord::Base
serialize :preferences
end
@user = User.new
@user.preferences[:receive_emails] = true
@user.preferences[:public_email] = true
@user.saveAnd more than 1K repositories used.
See CONTRIBUTING.md for how to set up a development environment, run the test suite and submit a pull request.
暂无开放 Issues,或尚未同步最近议题。