README.md in rails-settings-cached-2.0.3 vs README.md in rails-settings-cached-2.0.4

- old
+ new

@@ -1,27 +1,28 @@ ## Rails Settings Cached This a plugin that makes managing a table of -global key, value pairs easy. Think of it like a global Hash stored in your database, +а 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 dont want to hard code into your rails app. You can store any kind +setting that you don't want to hard code into your rails app. You can store any kind of object. Strings, numbers, arrays, or any object. > 🚨 BREAK CHANGES WARNING: -> rails-settings-cached 2.x has redesign the API, the new version will compatible with the stored setting values by older version. -> When you wants to upgrade 2.x, you must read the README again, and follow guides to change your Setting model. +> 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 ## Status [![Gem Version](https://badge.fury.io/rb/rails-settings-cached.svg)](https://rubygems.org/gems/rails-settings-cached) [![CI Status](https://travis-ci.org/huacnlee/rails-settings-cached.svg)](http://travis-ci.org/huacnlee/rails-settings-cached) [![Code Climate](https://codeclimate.com/github/huacnlee/rails-settings-cached/badges/gpa.svg)](https://codeclimate.com/github/huacnlee/rails-settings-cached) [![codecov.io](https://codecov.io/github/huacnlee/rails-settings-cached/coverage.svg?branch=master)](https://codecov.io/github/huacnlee/rails-settings-cached?branch=master) ## Setup Edit your Gemfile: ```ruby -gem "rails-settings-cached" +gem "rails-settings-cached", "~> 2.0" ``` Generate your settings: ```bash @@ -57,21 +58,21 @@ sender_email: "foo@bar.com" } end ``` -You must use `field` method to statement the setting keys, otherwice you can't use it. +You must use `field` method to statement the setting keys, otherwise you can't use it. Now just put that migration in the database with: ```bash rake db:migrate ``` ## Usage -The syntax is easy. First, lets create some settings to keep track of: +The syntax is easy. First, let's create some settings to keep track of: ```ruby irb > Setting.host "http://example.com" irb > Setting.host = "https://your-host.com" @@ -130,11 +131,11 @@ } ``` ## Readonly field -Some times you may need use Setting before Rails initialize, for example `config/devise.rb` +Sometimes you may need to use Setting before Rails is initialized, for example `config/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 @@ -160,13 +161,13 @@ 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 Thread.current for avoid hit cache/db. +In each Setting keys call, we will load the cache/db and save in `Thread.current` to avoid hit cache/db. -Each key update will expires the cache, so do not add some frequent update key. +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` @@ -187,13 +188,13 @@ end ``` ----- -## How to manage Settings in admin interface? +## How to manage Settings in the admin interface? -If you want create an admin interface to editing the Settings, you can try methods in follow: +If you want to create an admin interface to editing the Settings, you can try methods in following: config/routes.rb ```rb namespace :admin do @@ -258,8 +259,78 @@ Use YAML format to config the SMTP_html </details> <% end %> ``` -## Use case: +## Backward compatible to support 0.x scoped settings +You may used the scoped setting feature in 0.x version. Before you upgrade rails-settings-cached 2.x, you must follow this guide to backward compatible it. + +For example: + +```rb +class User < ApplicationRecord + include RailsSettings::Extend +end + +@user.settings.color = "red" +@user.settings.foo = 123 +``` + +create `app/models/concerns/scoped_setting.rb` + +```rb +module ScopedSetting + extend ActiveSupport::Concern + + included do + has_many :settings, as: :thing + end + + class_methods do + def scoped_field(name, default: nil) + define_method(name) do + obj = settings.where(var: name).take || settings.new(var: name, value: default) + obj.value + end + + define_method("#{name}=") do |val| + record = settings.where(var: name).take || settings.new(var: name) + record.value = val + record.save! + + val + end + end + end +end +``` + +Now include it for your model: + +```rb +class User < ApplicationRecord + include ScopedSetting + + scoped_field :color, default: "" + scoped_field :foo, default: 0 +end +``` + +Now you must to find project with ".setting." for replace with: + +Same values will fetch from the `settings` table. + +```rb +@user.color = "red" +@user.color # => "red" +@user.foo = 123 +@user.foo # => +``` + +## Use cases: + - [ruby-china/ruby-china](https://github.com/ruby-china/ruby-china) +- [thebluedoc/bluedoc](https://github.com/thebluedoc/bluedoc/blob/master/app/models/setting.rb) +- [tootsuite/mastodon](https://github.com/tootsuite/mastodon) +- [helpyio/helpy](https://github.com/helpyio/helpy) +