rails-settings-cached 2.0.1 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -1
- data/lib/rails-settings/base.rb +8 -5
- data/lib/rails-settings/railtie.rb +1 -1
- data/lib/rails-settings/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f5d17c59cd0c8b1c7c587169c32a7b7003c9067086a7b43e40d716973c8b104
|
4
|
+
data.tar.gz: 9e4b276543867580abcfa9da999fe3e81303459df28db49c76f2e75bef6679e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4be5e67948d944b2daa60c574586e297b638fc17e828a4923dc010ad691402b69bd8b06c823b228a3eaedafbd00fc2796d43c7986cb34a3bc0e5ae401eae8647
|
7
|
+
data.tar.gz: 7021a6b8d97b31f3f28557f51bef405cf5cfba5ed2194ee50d1b56ce599e1ca1aa288498d39c3ceefadd0618781610bd12344cf468b826ee4b7125fc640ce267
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
## Rails Settings Cached
|
2
2
|
|
3
3
|
This a plugin that makes managing a table of
|
4
4
|
global key, value pairs easy. Think of it like a global Hash stored in your database,
|
@@ -130,6 +130,28 @@ irb > Setting.notification_options
|
|
130
130
|
}
|
131
131
|
```
|
132
132
|
|
133
|
+
## Readonly field
|
134
|
+
|
135
|
+
Some times you may need use Setting before Rails initialize, for example `config/devise.rb`
|
136
|
+
|
137
|
+
```rb
|
138
|
+
Devise.setup do |config|
|
139
|
+
if Setting.omniauth_google_client_id.present?
|
140
|
+
config.omniauth :google_oauth2, Setting.omniauth_google_client_id, Setting.omniauth_google_client_secret
|
141
|
+
end
|
142
|
+
end
|
143
|
+
```
|
144
|
+
|
145
|
+
In this case, you must define the `readonly` field:
|
146
|
+
|
147
|
+
```rb
|
148
|
+
class Setting < RailsSettings::Base
|
149
|
+
# cache_prefix { "v1" }
|
150
|
+
field :omniauth_google_client_id, default: ENV["OMNIAUTH_GOOGLE_CLIENT_ID"], readonly: true
|
151
|
+
field :omniauth_google_client_secret, default: ENV["OMNIAUTH_GOOGLE_CLIENT_SECRET"], readonly: true
|
152
|
+
end
|
153
|
+
```
|
154
|
+
|
133
155
|
### Caching flow:
|
134
156
|
|
135
157
|
```
|
@@ -155,6 +177,16 @@ class Setting < RailsSettings::Base
|
|
155
177
|
end
|
156
178
|
```
|
157
179
|
|
180
|
+
In testing, you need add `Setting.clear_cache` for each Test case:
|
181
|
+
|
182
|
+
```rb
|
183
|
+
class ActiveSupport::TestCase
|
184
|
+
teardown do
|
185
|
+
Setting.clear_cache
|
186
|
+
end
|
187
|
+
end
|
188
|
+
```
|
189
|
+
|
158
190
|
-----
|
159
191
|
|
160
192
|
## How to manage Settings in admin interface?
|
data/lib/rails-settings/base.rb
CHANGED
@@ -17,12 +17,16 @@ module RailsSettings
|
|
17
17
|
self[:value] = new_value.to_yaml
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
21
|
-
|
22
|
-
Rails.cache.delete(self.class.cache_key)
|
20
|
+
def clear_cache
|
21
|
+
self.class.clear_cache
|
23
22
|
end
|
24
23
|
|
25
24
|
class << self
|
25
|
+
def clear_cache
|
26
|
+
Thread.current[:rails_settings_all_settings] = nil
|
27
|
+
Rails.cache.delete(self.cache_key)
|
28
|
+
end
|
29
|
+
|
26
30
|
def field(key, **opts)
|
27
31
|
_define_field(key, default: opts[:default], type: opts[:type], readonly: opts[:readonly])
|
28
32
|
end
|
@@ -74,8 +78,7 @@ module RailsSettings
|
|
74
78
|
|
75
79
|
if type == :boolean
|
76
80
|
self.class.define_method("#{key}?") do
|
77
|
-
|
78
|
-
val == "true" || val == "1"
|
81
|
+
self.send(key)
|
79
82
|
end
|
80
83
|
end
|
81
84
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RailsSettings
|
4
4
|
class Railtie < Rails::Railtie
|
5
5
|
initializer "rails_settings.active_record.initialization" do
|
6
|
-
RailsSettings::Base.after_commit :
|
6
|
+
RailsSettings::Base.after_commit :clear_cache, on: %i(create update destroy)
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|