jdx-rails-settings 0.7.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +284 -0
- data/lib/generators/settings/install_generator.rb +42 -0
- data/lib/generators/settings/templates/app.yml +13 -0
- data/lib/generators/settings/templates/migration.rb +17 -0
- data/lib/generators/settings/templates/model.rb +7 -0
- data/lib/rails-settings-cached.rb +11 -0
- data/lib/rails-settings/base.rb +61 -0
- data/lib/rails-settings/default.rb +40 -0
- data/lib/rails-settings/extend.rb +34 -0
- data/lib/rails-settings/railtie.rb +8 -0
- data/lib/rails-settings/scoped_settings.rb +12 -0
- data/lib/rails-settings/settings.rb +121 -0
- data/lib/rails-settings/version.rb +7 -0
- metadata +147 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6389648edb553abe574732af98fcd81201a8aee7e7573c33de3d166b31004afe
|
4
|
+
data.tar.gz: 4ae03fec50a981e7c7fc981310a9d56f7185ab6128561c2cc7446f77299ad046
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aa24fab669e47700f7d5a3b84bcf27eacc057a10353404e24cb28be077c663346cc827f9ba73c803ceda08251085e3ed2a4d6dcb360f9f356ac2a2c4571e71c2
|
7
|
+
data.tar.gz: 69137ecf802735e28c073f2844ba3cea2ca18ab0b971358b2428d2307661399255a9ebc82d65739c557fbfb693a76280999f612c0b85833e58de3a5fefa7869d
|
data/README.md
ADDED
@@ -0,0 +1,284 @@
|
|
1
|
+
# Rails Settings Cached
|
2
|
+
|
3
|
+
This is improved from [rails-settings](https://github.com/ledermann/rails-settings),
|
4
|
+
added caching for all settings. Settings is a plugin that makes managing a table of
|
5
|
+
global key, value pairs easy. Think of it like a global Hash stored in your database,
|
6
|
+
that uses simple ActiveRecord like methods for manipulation. Keep track of any global
|
7
|
+
setting that you dont want to hard code into your rails app. You can store any kind
|
8
|
+
of object. Strings, numbers, arrays, or any object.
|
9
|
+
|
10
|
+
## Status
|
11
|
+
|
12
|
+
[![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)
|
13
|
+
|
14
|
+
## Setup
|
15
|
+
|
16
|
+
Edit your Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem "rails-settings-cached"
|
20
|
+
```
|
21
|
+
|
22
|
+
Generate your settings:
|
23
|
+
|
24
|
+
```bash
|
25
|
+
$ rails g settings:install
|
26
|
+
```
|
27
|
+
|
28
|
+
If you want custom model name:
|
29
|
+
|
30
|
+
```bash
|
31
|
+
$ rails g settings:install
|
32
|
+
```
|
33
|
+
|
34
|
+
Or use a custom name:
|
35
|
+
|
36
|
+
```bash
|
37
|
+
$ rails g settings:install SiteConfig
|
38
|
+
```
|
39
|
+
|
40
|
+
You will get `app/models/setting.rb`
|
41
|
+
|
42
|
+
```rb
|
43
|
+
class Setting < RailsSettings::Base
|
44
|
+
source Rails.root.join("config/app.yml")
|
45
|
+
# cache_prefix { "v1" }
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
Now just put that migration in the database with:
|
50
|
+
|
51
|
+
```bash
|
52
|
+
rake db:migrate
|
53
|
+
```
|
54
|
+
|
55
|
+
## Usage
|
56
|
+
|
57
|
+
The syntax is easy. First, lets create some settings to keep track of:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
Setting.admin_password = 'supersecret'
|
61
|
+
Setting.date_format = '%m %d, %Y'
|
62
|
+
Setting.cocktails = ['Martini', 'Screwdriver', 'White Russian']
|
63
|
+
Setting.foo = 123
|
64
|
+
Setting.credentials = { :username => 'tom', :password => 'secret' }
|
65
|
+
```
|
66
|
+
|
67
|
+
Now lets read them back:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
Setting.foo # returns 123
|
71
|
+
```
|
72
|
+
|
73
|
+
Changing an existing setting is the same as creating a new setting:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
Setting.foo = 'super duper bar'
|
77
|
+
```
|
78
|
+
|
79
|
+
Decide you dont want to track a particular setting anymore?
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
Setting.destroy :foo
|
83
|
+
Setting.foo # returns nil
|
84
|
+
```
|
85
|
+
|
86
|
+
Want a list of all the settings?
|
87
|
+
```ruby
|
88
|
+
Setting.get_all
|
89
|
+
```
|
90
|
+
|
91
|
+
You need name spaces and want a list of settings for a give name space? Just choose your prefered named space delimiter and use `Setting.get_all` (`Settings.all` for # Rails 3.x and 4.0.x) like this:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
Setting['preferences.color'] = :blue
|
95
|
+
Setting['preferences.size'] = :large
|
96
|
+
Setting['license.key'] = 'ABC-DEF'
|
97
|
+
# Rails 4.1.x
|
98
|
+
Setting.get_all('preferences.')
|
99
|
+
# Rails 3.x and 4.0.x
|
100
|
+
Setting.all('preferences.')
|
101
|
+
# returns { 'preferences.color' => :blue, 'preferences.size' => :large }
|
102
|
+
```
|
103
|
+
|
104
|
+
## Extend a model
|
105
|
+
|
106
|
+
Settings may be bound to any existing ActiveRecord object. Define this association like this:
|
107
|
+
Notice! is not do caching in this version.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
class User < ActiveRecord::Base
|
111
|
+
include RailsSettings::Extend
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
115
|
+
Then you can set/get a setting for a given user instance just by doing this:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
user = User.find(123)
|
119
|
+
user.settings.color = :red
|
120
|
+
user.settings.color # returns :red
|
121
|
+
user.settings.get_all
|
122
|
+
# { "color" => :red }
|
123
|
+
```
|
124
|
+
|
125
|
+
If you want to find users having or not having some settings, there are named scopes for this:
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
User.with_settings
|
129
|
+
# => returns a scope of users having any setting
|
130
|
+
|
131
|
+
User.with_settings_for('color')
|
132
|
+
# => returns a scope of users having a 'color' setting
|
133
|
+
|
134
|
+
User.without_settings
|
135
|
+
# returns a scope of users having no setting at all (means user.settings.get_all == {})
|
136
|
+
|
137
|
+
User.without_settings('color')
|
138
|
+
# returns a scope of users having no 'color' setting (means user.settings.color == nil)
|
139
|
+
```
|
140
|
+
|
141
|
+
## Default settings
|
142
|
+
|
143
|
+
Sometimes you may want define default settings.
|
144
|
+
|
145
|
+
RailsSettings has generate a config YAML file in:
|
146
|
+
|
147
|
+
```yml
|
148
|
+
# config/app.yml
|
149
|
+
defaults: &defaults
|
150
|
+
github_token: "123456"
|
151
|
+
twitter_token: "<%= ENV["TWITTER_TOKEN"] %>"
|
152
|
+
foo:
|
153
|
+
bar: "Foo bar"
|
154
|
+
|
155
|
+
development:
|
156
|
+
<<: *defaults
|
157
|
+
|
158
|
+
test:
|
159
|
+
<<: *defaults
|
160
|
+
|
161
|
+
production:
|
162
|
+
<<: *defaults
|
163
|
+
```
|
164
|
+
|
165
|
+
And you can use by `Setting` model:
|
166
|
+
|
167
|
+
```
|
168
|
+
Setting.github_token
|
169
|
+
=> "123456"
|
170
|
+
Setting.github_token = "654321"
|
171
|
+
# Save into database.
|
172
|
+
Setting.github_token
|
173
|
+
# Read from databae / caching.
|
174
|
+
=> "654321"
|
175
|
+
Setting['foo.bar']
|
176
|
+
=> 'Foo bar'
|
177
|
+
```
|
178
|
+
|
179
|
+
NOTE: YAML setting it also under the cache scope, when you restart Rails application, cache will expire,
|
180
|
+
so when you want change default config, you need restart Rails application server.
|
181
|
+
|
182
|
+
### Caching flow:
|
183
|
+
|
184
|
+
```
|
185
|
+
Setting.foo -> Check Cache -> Exist - Write Cache -> Return
|
186
|
+
|
|
187
|
+
Check DB -> Exist -> Write Cache -> Return
|
188
|
+
|
|
189
|
+
Check Default -> Exist -> Write Cache -> Return
|
190
|
+
|
|
191
|
+
Return nil
|
192
|
+
```
|
193
|
+
|
194
|
+
## Change cache key
|
195
|
+
|
196
|
+
When `config/app.yml` has changed, you may need change the cache prefix to expires caches.
|
197
|
+
|
198
|
+
```ruby
|
199
|
+
class Setting < RailsSettings::Base
|
200
|
+
cache_prefix { 'you-prefix' }
|
201
|
+
...
|
202
|
+
end
|
203
|
+
```
|
204
|
+
|
205
|
+
-----
|
206
|
+
|
207
|
+
## How to create a list, form to manage Settings?
|
208
|
+
|
209
|
+
If you want create an admin interface to editing the Settings, you can try methods in follow:
|
210
|
+
|
211
|
+
config/routes.rb
|
212
|
+
|
213
|
+
```rb
|
214
|
+
namespace :admin do
|
215
|
+
resources :settings
|
216
|
+
end
|
217
|
+
```
|
218
|
+
|
219
|
+
|
220
|
+
app/controllers/admin/settings_controller.rb
|
221
|
+
|
222
|
+
```rb
|
223
|
+
module Admin
|
224
|
+
class SettingsController < ApplicationController
|
225
|
+
before_action :get_setting, only: [:edit, :update]
|
226
|
+
|
227
|
+
def index
|
228
|
+
@settings = Setting.get_all
|
229
|
+
end
|
230
|
+
|
231
|
+
def edit
|
232
|
+
end
|
233
|
+
|
234
|
+
def update
|
235
|
+
if @setting.value != params[:setting][:value]
|
236
|
+
@setting.value = params[:setting][:value]
|
237
|
+
@setting.save
|
238
|
+
redirect_to admin_settings_path, notice: 'Setting has updated.'
|
239
|
+
else
|
240
|
+
redirect_to admin_settings_path
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
def get_setting
|
245
|
+
@setting = Setting.find_by(var: params[:id]) || Setting.new(var: params[:id])
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
```
|
250
|
+
|
251
|
+
app/views/admin/settings/index.html.erb
|
252
|
+
|
253
|
+
```erb
|
254
|
+
<table>
|
255
|
+
<tr>
|
256
|
+
<th>Key</th>
|
257
|
+
<th></th>
|
258
|
+
</tr>
|
259
|
+
<% @settings.each_key do |key| %>
|
260
|
+
<tr>
|
261
|
+
<td><%= key %></td>
|
262
|
+
<td><%= link_to 'edit', edit_admin_setting_path(key) %></td>
|
263
|
+
</tr>
|
264
|
+
<% end %>
|
265
|
+
</table>
|
266
|
+
```
|
267
|
+
|
268
|
+
app/views/admin/settings/edit.html.erb
|
269
|
+
|
270
|
+
```erb
|
271
|
+
<%= form_for(@setting, url: admin_setting_path(@setting.var), method: 'patch') do |f| %>
|
272
|
+
<label><%= @setting.var %></label>
|
273
|
+
<%= f.text_area :value, rows: 10 %>
|
274
|
+
<%= f.submit %>
|
275
|
+
<% end %>
|
276
|
+
```
|
277
|
+
|
278
|
+
Also you may use [rails-settings-ui](https://github.com/accessd/rails-settings-ui) gem
|
279
|
+
for building ready to using interface with validations,
|
280
|
+
or [activeadmin_settings_cached](https://github.com/artofhuman/activeadmin_settings_cached) gem if you use [activeadmin](https://github.com/activeadmin/activeadmin).
|
281
|
+
|
282
|
+
## Use case:
|
283
|
+
|
284
|
+
- [ruby-china/ruby-china](https://github.com/ruby-china/ruby-china)
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
require "rails/generators/migration"
|
3
|
+
|
4
|
+
module Settings
|
5
|
+
class InstallGenerator < Rails::Generators::NamedBase
|
6
|
+
desc "Generate RailsSettings files."
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
|
9
|
+
argument :name, type: :string, default: "setting"
|
10
|
+
|
11
|
+
source_root File.expand_path("../templates", __FILE__)
|
12
|
+
|
13
|
+
@@migrations = false
|
14
|
+
|
15
|
+
def self.next_migration_number(dirname) #:nodoc:
|
16
|
+
if ActiveRecord::Base.timestamped_migrations
|
17
|
+
if @@migrations
|
18
|
+
(current_migration_number(dirname) + 1)
|
19
|
+
else
|
20
|
+
@@migrations = true
|
21
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
22
|
+
end
|
23
|
+
else
|
24
|
+
format "%.3d", current_migration_number(dirname) + 1
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def install_setting
|
29
|
+
template "model.rb", File.join("app/models", class_path, "#{file_name}.rb")
|
30
|
+
template "app.yml", File.join("config", "app.yml")
|
31
|
+
migration_template "migration.rb", "db/migrate/create_settings.rb", migration_version: migration_version
|
32
|
+
end
|
33
|
+
|
34
|
+
def rails5?
|
35
|
+
Rails.version.start_with? "5"
|
36
|
+
end
|
37
|
+
|
38
|
+
def migration_version
|
39
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if rails5?
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class CreateSettings < ActiveRecord::Migration<%= migration_version %>
|
2
|
+
def self.up
|
3
|
+
create_table :settings do |t|
|
4
|
+
t.string :var, null: false
|
5
|
+
t.text :value, null: true
|
6
|
+
t.integer :thing_id, null: true
|
7
|
+
t.string :thing_type, null: true, limit: 30
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
|
11
|
+
add_index :settings, %i(thing_type thing_id var), unique: true
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.down
|
15
|
+
drop_table :settings
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require "sinatra/activerecord"
|
2
|
+
require_relative "rails-settings/settings"
|
3
|
+
require_relative "rails-settings/base"
|
4
|
+
require_relative "rails-settings/scoped_settings"
|
5
|
+
require_relative "rails-settings/default"
|
6
|
+
require_relative "rails-settings/extend"
|
7
|
+
# require_relative "rails-settings/railtie"
|
8
|
+
require_relative "rails-settings/version"
|
9
|
+
|
10
|
+
module RailsSettings
|
11
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module RailsSettings
|
2
|
+
class Base < Settings
|
3
|
+
def rewrite_cache
|
4
|
+
self.class.configuration.cache_store.fetch(cache_key, value)
|
5
|
+
end
|
6
|
+
|
7
|
+
def expire_cache
|
8
|
+
self.class.configuration.cache_store.delete(cache_key)
|
9
|
+
end
|
10
|
+
|
11
|
+
def cache_key
|
12
|
+
self.class.cache_key(var, thing)
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
|
17
|
+
attr_accessor :configuration
|
18
|
+
|
19
|
+
def configure
|
20
|
+
self.configuration ||= Configuration.new
|
21
|
+
yield(configuration)
|
22
|
+
end
|
23
|
+
|
24
|
+
def cache_prefix(&block)
|
25
|
+
@cache_prefix = block
|
26
|
+
end
|
27
|
+
|
28
|
+
def cache_key(var_name, scope_object)
|
29
|
+
scope = ["rails_settings_cached"]
|
30
|
+
scope << @cache_prefix.call if @cache_prefix
|
31
|
+
scope << "#{scope_object.class.name}-#{scope_object.id}" if scope_object
|
32
|
+
scope << var_name.to_s
|
33
|
+
scope.join("/")
|
34
|
+
end
|
35
|
+
|
36
|
+
def [](key)
|
37
|
+
return super(key) unless rails_initialized?
|
38
|
+
val = cache_store.fetch(cache_key(key, @object)) do
|
39
|
+
super(key)
|
40
|
+
end
|
41
|
+
val
|
42
|
+
end
|
43
|
+
|
44
|
+
# set a setting value by [] notation
|
45
|
+
def []=(var_name, value)
|
46
|
+
super
|
47
|
+
cache_store.write(cache_key(var_name, @object), value)
|
48
|
+
value
|
49
|
+
end
|
50
|
+
|
51
|
+
def cache_store
|
52
|
+
@cache_store ||= RailsSettings::Base.configuration.cache_store
|
53
|
+
end
|
54
|
+
|
55
|
+
class Configuration
|
56
|
+
attr_accessor :cache_store
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "digest/md5"
|
2
|
+
|
3
|
+
module RailsSettings
|
4
|
+
class Default < ::Hash
|
5
|
+
class MissingKey < StandardError; end
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def enabled?
|
9
|
+
source_path && File.exist?(source_path)
|
10
|
+
end
|
11
|
+
|
12
|
+
def source(value = nil)
|
13
|
+
@source ||= value
|
14
|
+
end
|
15
|
+
|
16
|
+
def source_path
|
17
|
+
@source || "config/app.yml"
|
18
|
+
end
|
19
|
+
|
20
|
+
def [](key)
|
21
|
+
# foo.bar.dar Nested fetch value
|
22
|
+
return instance[key] if instance.key?(key)
|
23
|
+
keys = key.to_s.split(".")
|
24
|
+
instance.dig(*keys)
|
25
|
+
end
|
26
|
+
|
27
|
+
def instance
|
28
|
+
return @instance if defined? @instance
|
29
|
+
@instance = new
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def initialize
|
34
|
+
content = open(self.class.source_path).read
|
35
|
+
hash = content.empty? ? {} : YAML.load(ERB.new(content).result).to_hash
|
36
|
+
hash = hash[Sinatra::Application.environment.to_s] || {}
|
37
|
+
replace hash
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module RailsSettings
|
2
|
+
module Extend
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
scope :with_settings, lambda {
|
7
|
+
joins("JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
|
8
|
+
settings.thing_type = '#{base_class.name}')")
|
9
|
+
.select("DISTINCT #{table_name}.*")
|
10
|
+
}
|
11
|
+
|
12
|
+
scope :with_settings_for, lambda { |var|
|
13
|
+
joins("JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
|
14
|
+
settings.thing_type = '#{base_class.name}') AND settings.var = '#{var}'")
|
15
|
+
}
|
16
|
+
|
17
|
+
scope :without_settings, lambda {
|
18
|
+
joins("LEFT JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
|
19
|
+
settings.thing_type = '#{base_class.name}')")
|
20
|
+
.where("settings.id IS NULL")
|
21
|
+
}
|
22
|
+
|
23
|
+
scope :without_settings_for, lambda { |var|
|
24
|
+
where("settings.id IS NULL")
|
25
|
+
.joins("LEFT JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
|
26
|
+
settings.thing_type = '#{base_class.name}') AND settings.var = '#{var}'")
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
def settings
|
31
|
+
ScopedSettings.for_thing(self)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
module RailsSettings
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
initializer "rails_settings.active_record.initialization" do
|
4
|
+
RailsSettings::Base.after_commit :rewrite_cache, on: %i(create update)
|
5
|
+
RailsSettings::Base.after_commit :expire_cache, on: %i(destroy)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
module RailsSettings
|
2
|
+
class Settings < ActiveRecord::Base
|
3
|
+
self.table_name = table_name_prefix + "settings"
|
4
|
+
|
5
|
+
class SettingNotFound < RuntimeError; end
|
6
|
+
|
7
|
+
belongs_to :thing, polymorphic: true
|
8
|
+
|
9
|
+
# get the value field, YAML decoded
|
10
|
+
def value
|
11
|
+
YAML.load(self[:value]) if self[:value].present?
|
12
|
+
end
|
13
|
+
|
14
|
+
# set the value field, YAML encoded
|
15
|
+
def value=(new_value)
|
16
|
+
self[:value] = new_value.to_yaml
|
17
|
+
end
|
18
|
+
|
19
|
+
class << self
|
20
|
+
# get or set a variable with the variable as the called method
|
21
|
+
# rubocop:disable Style/MethodMissing
|
22
|
+
def method_missing(method, *args)
|
23
|
+
method_name = method.to_s
|
24
|
+
super(method, *args)
|
25
|
+
rescue NoMethodError
|
26
|
+
# set a value for a variable
|
27
|
+
if method_name[-1] == "="
|
28
|
+
var_name = method_name.sub("=", "")
|
29
|
+
value = args.first
|
30
|
+
self[var_name] = value
|
31
|
+
else
|
32
|
+
# retrieve a value
|
33
|
+
self[method_name]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# destroy the specified settings record
|
38
|
+
def destroy(var_name)
|
39
|
+
var_name = var_name.to_s
|
40
|
+
obj = object(var_name)
|
41
|
+
raise SettingNotFound, "Setting variable \"#{var_name}\" not found" if obj.nil?
|
42
|
+
|
43
|
+
obj.destroy
|
44
|
+
true
|
45
|
+
end
|
46
|
+
|
47
|
+
# retrieve all settings as a hash (optionally starting with a given namespace)
|
48
|
+
def get_all(starting_with = nil)
|
49
|
+
vars = thing_scoped.select("var, value")
|
50
|
+
vars = vars.where("var LIKE '#{starting_with}%'") if starting_with
|
51
|
+
result = {}
|
52
|
+
vars.each { |record| result[record.var] = record.value }
|
53
|
+
result.reverse_merge!(default_settings(starting_with))
|
54
|
+
result.with_indifferent_access
|
55
|
+
end
|
56
|
+
|
57
|
+
def where(sql = nil)
|
58
|
+
vars = thing_scoped.where(sql) if sql
|
59
|
+
vars
|
60
|
+
end
|
61
|
+
|
62
|
+
# get a setting value by [] notation
|
63
|
+
def [](var_name)
|
64
|
+
return Default[var_name] unless rails_initialized?
|
65
|
+
|
66
|
+
val = object(var_name)
|
67
|
+
return val.value if val
|
68
|
+
return Default[var_name] if Default.enabled?
|
69
|
+
end
|
70
|
+
|
71
|
+
# set a setting value by [] notation
|
72
|
+
def []=(var_name, value)
|
73
|
+
var_name = var_name.to_s
|
74
|
+
|
75
|
+
record = object(var_name) || thing_scoped.new(var: var_name)
|
76
|
+
record.value = value
|
77
|
+
record.save!
|
78
|
+
|
79
|
+
value
|
80
|
+
end
|
81
|
+
|
82
|
+
def merge!(var_name, hash_value)
|
83
|
+
raise ArgumentError unless hash_value.is_a?(Hash)
|
84
|
+
|
85
|
+
old_value = self[var_name] || {}
|
86
|
+
raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
|
87
|
+
|
88
|
+
new_value = old_value.merge(hash_value)
|
89
|
+
self[var_name] = new_value if new_value != old_value
|
90
|
+
|
91
|
+
new_value
|
92
|
+
end
|
93
|
+
|
94
|
+
def object(var_name)
|
95
|
+
return nil unless table_exists?
|
96
|
+
thing_scoped.where(var: var_name.to_s).first
|
97
|
+
end
|
98
|
+
|
99
|
+
def thing_scoped
|
100
|
+
unscoped.where("thing_type is NULL and thing_id is NULL")
|
101
|
+
end
|
102
|
+
|
103
|
+
def source(filename)
|
104
|
+
Default.source(filename)
|
105
|
+
end
|
106
|
+
|
107
|
+
def rails_initialized?
|
108
|
+
# Rails.application && Rails.application.initialized?
|
109
|
+
true
|
110
|
+
end
|
111
|
+
|
112
|
+
private
|
113
|
+
|
114
|
+
def default_settings(starting_with = nil)
|
115
|
+
return {} unless Default.enabled?
|
116
|
+
return Default.instance if starting_with.nil?
|
117
|
+
Default.instance.select { |key, _| key.to_s.start_with?(starting_with) }
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jdx-rails-settings
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.7.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason Lee
|
8
|
+
- Squeegy
|
9
|
+
- Georg Ledermann
|
10
|
+
- 100hz
|
11
|
+
- JobAdx
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
date: 2019-07-29 00:00:00.000000000 Z
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
18
|
+
name: sinatra-activerecord
|
19
|
+
requirement: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '0'
|
24
|
+
type: :development
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: sqlite3
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - "~>"
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.3.6
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - "~>"
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 1.3.6
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: rubocop
|
47
|
+
requirement: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - '='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: 0.46.0
|
52
|
+
type: :development
|
53
|
+
prerelease: false
|
54
|
+
version_requirements: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - '='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 0.46.0
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: simplecov
|
61
|
+
requirement: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: rspec
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
type: :development
|
81
|
+
prerelease: false
|
82
|
+
version_requirements: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
- !ruby/object:Gem::Dependency
|
88
|
+
name: codecov
|
89
|
+
requirement: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
type: :development
|
95
|
+
prerelease: false
|
96
|
+
version_requirements: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
description: "\n This is improved from rails-settings, added caching.\n Settings
|
102
|
+
plugin for Rails that makes managing a table of global key,\n value pairs easy.
|
103
|
+
Think of it like a global Hash stored in you database,\n that uses simple ActiveRecord
|
104
|
+
like methods for manipulation.\n\n Keep track of any global setting that you dont
|
105
|
+
want to hard code into your rails app.\n You can store any kind of object. Strings,
|
106
|
+
numbers, arrays, or any object.\n "
|
107
|
+
email: admin@jobadx.com
|
108
|
+
executables: []
|
109
|
+
extensions: []
|
110
|
+
extra_rdoc_files: []
|
111
|
+
files:
|
112
|
+
- README.md
|
113
|
+
- lib/generators/settings/install_generator.rb
|
114
|
+
- lib/generators/settings/templates/app.yml
|
115
|
+
- lib/generators/settings/templates/migration.rb
|
116
|
+
- lib/generators/settings/templates/model.rb
|
117
|
+
- lib/rails-settings-cached.rb
|
118
|
+
- lib/rails-settings/base.rb
|
119
|
+
- lib/rails-settings/default.rb
|
120
|
+
- lib/rails-settings/extend.rb
|
121
|
+
- lib/rails-settings/railtie.rb
|
122
|
+
- lib/rails-settings/scoped_settings.rb
|
123
|
+
- lib/rails-settings/settings.rb
|
124
|
+
- lib/rails-settings/version.rb
|
125
|
+
homepage: https://github.com/bharthur/rails-settings-cached
|
126
|
+
licenses: []
|
127
|
+
metadata: {}
|
128
|
+
post_install_message:
|
129
|
+
rdoc_options: []
|
130
|
+
require_paths:
|
131
|
+
- lib
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '2.3'
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
requirements: []
|
143
|
+
rubygems_version: 3.0.4
|
144
|
+
signing_key:
|
145
|
+
specification_version: 4
|
146
|
+
summary: Settings plugin for Rails that makes managing a table of global keys.
|
147
|
+
test_files: []
|