README.md in rails-settings-cached-2.3.5 vs README.md in rails-settings-cached-2.4.0

- old
+ new

@@ -124,11 +124,10 @@ { sender_email: "notice@rubyonrails.org" } ``` - ### Get defined fields > version 2.3+ ```rb @@ -149,11 +148,11 @@ => { key: "host", type: :string, default: "http://example.com", readonly: true } Setting.get_field("app_name") => { key: "app_name", type: :string, default: "Rails Settings", readonly: false } ``` -## Use Setting in Rails initalizing: +## Use Setting in Rails initializing: In `version 2.3+` we allows you to use Setting before Rails is initialized. For example `config/initializers/devise.rb` @@ -172,14 +171,13 @@ end ``` ## Readonly field -You may also want use Setting in these locations: +You may also want use Setting before Rails initialize: ``` -config/application.rb config/environments/*.rb ``` If you want do that do that, the setting field must has `readonly: true`. @@ -198,28 +196,33 @@ enable_starttls_auto: ENV["mailer_options.enable_starttls_auto"] } end ``` +config/environments/production.rb + ```rb +# You must require_relative directly in Rails 6.1+ in config/environments/production.rb +require_relative "../../app/models/setting" + Rails.application.configure do config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = Setting.mailer_options.deep_symbolize_keys end ``` -### Caching flow: +## Caching flow: ``` Setting.host -> 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 [RequestStore](https://github.com/steveklabnik/request_store) to avoid hit cache/db. +In each Setting keys call, we will load the cache/db and save in [ActiveSupport::CurrentAttributes](https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html) to avoid hit cache/db. Each key update will expire the cache, so do not add some frequent update key. ## Change cache key @@ -240,11 +243,11 @@ Setting.clear_cache end end ``` ------ +--- ## How to manage Settings in the admin interface? If you want to create an admin interface to editing the Settings, you can try methods in following: @@ -254,23 +257,20 @@ namespace :admin do resource :settings end ``` - app/controllers/admin/settings_controller.rb ```rb module Admin class SettingsController < ApplicationController - before_action :get_setting, only: [:edit, :update] - def create setting_params.keys.each do |key| Setting.send("#{key}=", setting_params[key].strip) unless setting_params[key].nil? end - redirect_to settings_path, notice: "Setting was successfully updated." + redirect_to admin_settings_path, notice: "Setting was successfully updated." end private def setting_params params.require(:setting).permit(:host, :user_limits, :admin_emails, @@ -350,8 +350,7 @@ - [siwapp/siwapp](https://github.com/siwapp/siwapp) - 2.x - [aidewoode/black_candy](https://github.com/aidewoode/black_candy) - 2.x - [thebluedoc/bluedoc](https://github.com/thebluedoc/bluedoc/blob/master/app/models/setting.rb) - 2.x - [tootsuite/mastodon](https://github.com/tootsuite/mastodon) - 0.6.x - [helpyio/helpy](https://github.com/helpyio/helpy) - 0.5.x - And more than [1K repositories](https://github.com/huacnlee/rails-settings-cached/network/dependents) used.