rails-settings-cached 0.4.1 → 0.4.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 +22 -11
- data/lib/generators/settings/settings_generator.rb +26 -26
- data/lib/generators/settings/templates/migration.rb +6 -6
- data/lib/generators/settings/templates/model.rb +1 -0
- data/lib/rails-settings-cached.rb +4 -4
- data/lib/rails-settings/cached_settings.rb +22 -18
- data/lib/rails-settings/extend.rb +21 -24
- data/lib/rails-settings/scoped_settings.rb +3 -4
- data/lib/rails-settings/settings.rb +70 -82
- metadata +49 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c9f513e434fb39d1cfefa3ff34bac2b079d6aa9
|
4
|
+
data.tar.gz: 9d1c7c9775ab9108d5604bb1617f2bffde890e80
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 992cc15c9cadd8298dab78168b42d4ddf60d784a3749ecf66b7e0b1535b18487aa7b763ea89771b0e6e61ff805cb831d1a6d3700a545c51d5ba88b0dc6279f4b
|
7
|
+
data.tar.gz: 310666efc9ba502e62da3e7fd06f3282909c984fcee2518838c6ddd4c13b3045a9bd4dd485b8acd5c5beaeeca09789c04e46ef2b8f6d192a4c573e2d7a186a50
|
data/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# Settings Gem
|
2
2
|
|
3
|
-
This is improved from rails-settings,
|
4
|
-
Settings is a plugin that makes managing a table of
|
5
|
-
Think of it like a global Hash stored in
|
6
|
-
like methods for manipulation.
|
7
|
-
to hard code into your rails app.
|
8
|
-
arrays, or any object.
|
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
9
|
|
10
10
|
## Status
|
11
11
|
|
@@ -84,19 +84,24 @@ Setting.foo # returns nil
|
|
84
84
|
```
|
85
85
|
|
86
86
|
Want a list of all the settings?
|
87
|
-
|
88
87
|
```ruby
|
88
|
+
# Rails 4.1.x
|
89
89
|
Setting.get_all
|
90
|
+
# Rails 3.x and 4.0.x
|
91
|
+
Setting.all
|
90
92
|
# returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}
|
91
93
|
```
|
92
94
|
|
93
|
-
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 like this:
|
95
|
+
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:
|
94
96
|
|
95
97
|
```ruby
|
96
98
|
Setting['preferences.color'] = :blue
|
97
99
|
Setting['preferences.size'] = :large
|
98
100
|
Setting['license.key'] = 'ABC-DEF'
|
101
|
+
# Rails 4.1.x
|
99
102
|
Setting.get_all('preferences.')
|
103
|
+
# Rails 3.x and 4.0.x
|
104
|
+
Setting.all('preferences.')
|
100
105
|
# returns { 'preferences.color' => :blue, 'preferences.size' => :large }
|
101
106
|
```
|
102
107
|
|
@@ -137,7 +142,11 @@ Then you can set/get a setting for a given user instance just by doing this:
|
|
137
142
|
user = User.find(123)
|
138
143
|
user.settings.color = :red
|
139
144
|
user.settings.color # returns :red
|
140
|
-
|
145
|
+
# Rails 4.1.x
|
146
|
+
user.settings.get_all
|
147
|
+
# Rails 3.x and 4.0.x
|
148
|
+
user.settings.all
|
149
|
+
# { "color" => :red }
|
141
150
|
```
|
142
151
|
|
143
152
|
I you want to find users having or not having some settings, there are named scopes for this:
|
@@ -168,7 +177,7 @@ class SettingsController < ApplicationController
|
|
168
177
|
# to get all items for render list
|
169
178
|
@settings = Setting.unscoped
|
170
179
|
end
|
171
|
-
|
180
|
+
|
172
181
|
def edit
|
173
182
|
@setting = Setting.unscoped.find(params[:id])
|
174
183
|
end
|
@@ -176,4 +185,6 @@ end
|
|
176
185
|
```
|
177
186
|
|
178
187
|
|
179
|
-
|
188
|
+
Also you may use [rails-settings-ui](https://github.com/accessd/rails-settings-ui) gem
|
189
|
+
for building ready to using interface with validations.
|
190
|
+
|
@@ -1,30 +1,30 @@
|
|
1
1
|
require 'rails/generators/migration'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
argument :name, :type => :string, :default => "my_settings"
|
7
|
-
|
8
|
-
source_root File.expand_path('../templates', __FILE__)
|
9
|
-
|
10
|
-
@@migrations = false
|
11
|
-
|
12
|
-
def self.next_migration_number(dirname) #:nodoc:
|
13
|
-
if ActiveRecord::Base.timestamped_migrations
|
14
|
-
if @@migrations
|
15
|
-
(current_migration_number(dirname) + 1)
|
16
|
-
else
|
17
|
-
@@migrations = true
|
18
|
-
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
19
|
-
end
|
20
|
-
else
|
21
|
-
"%.3d" % (current_migration_number(dirname) + 1)
|
22
|
-
end
|
23
|
-
end
|
3
|
+
class SettingsGenerator < Rails::Generators::NamedBase
|
4
|
+
include Rails::Generators::Migration
|
24
5
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
6
|
+
argument :name, type: :string, default: 'my_settings'
|
7
|
+
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
|
10
|
+
@@migrations = false
|
11
|
+
|
12
|
+
def self.next_migration_number(dirname) #:nodoc:
|
13
|
+
if ActiveRecord::Base.timestamped_migrations
|
14
|
+
if @@migrations
|
15
|
+
(current_migration_number(dirname) + 1)
|
16
|
+
else
|
17
|
+
@@migrations = true
|
18
|
+
Time.now.utc.strftime('%Y%m%d%H%M%S')
|
29
19
|
end
|
30
|
-
|
20
|
+
else
|
21
|
+
format '%.3d', current_migration_number(dirname) + 1
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def settings
|
26
|
+
# generate(:model, name, "--skip-migration")
|
27
|
+
template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb"), force: true
|
28
|
+
migration_template 'migration.rb', 'db/migrate/create_settings.rb'
|
29
|
+
end
|
30
|
+
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
class CreateSettings < ActiveRecord::Migration
|
2
2
|
def self.up
|
3
3
|
create_table :settings do |t|
|
4
|
-
t.string
|
5
|
-
t.text
|
6
|
-
t.integer :thing_id,
|
7
|
-
t.string
|
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
8
|
t.timestamps
|
9
9
|
end
|
10
|
-
|
11
|
-
add_index :settings,
|
10
|
+
|
11
|
+
add_index :settings, %i(thing_type thing_id var), unique: true
|
12
12
|
end
|
13
13
|
|
14
14
|
def self.down
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
require_relative 'rails-settings/settings'
|
2
|
+
require_relative 'rails-settings/scoped_settings'
|
3
|
+
require_relative 'rails-settings/cached_settings'
|
4
|
+
require_relative 'rails-settings/extend'
|
@@ -1,25 +1,29 @@
|
|
1
1
|
module RailsSettings
|
2
2
|
class CachedSettings < Settings
|
3
|
-
|
4
|
-
after_create :rewrite_cache
|
3
|
+
after_commit :rewrite_cache, on: %i(create update)
|
5
4
|
def rewrite_cache
|
6
|
-
Rails.cache.write("
|
5
|
+
Rails.cache.write("rails_settings_cached:#{var}", value)
|
7
6
|
end
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
7
|
+
|
8
|
+
after_commit :expire_cache, on: %i(destroy)
|
9
|
+
def expire_cache
|
10
|
+
Rails.cache.delete("rails_settings_cached:#{var}")
|
11
|
+
end
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def [](var_name)
|
15
|
+
cache_key = "rails_settings_cached:#{var_name}"
|
16
|
+
obj = Rails.cache.read(cache_key)
|
17
|
+
obj = super(var_name) if obj.nil?
|
18
|
+
|
19
|
+
return @@defaults[var_name.to_s] if obj.nil?
|
20
|
+
obj
|
21
|
+
end
|
22
|
+
|
23
|
+
def save_default(key, value)
|
24
|
+
return false unless send(key).nil?
|
25
|
+
send("#{key}=", value)
|
22
26
|
end
|
23
27
|
end
|
24
28
|
end
|
25
|
-
end
|
29
|
+
end
|
@@ -1,37 +1,34 @@
|
|
1
1
|
module RailsSettings
|
2
2
|
module Extend
|
3
3
|
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
included do
|
6
|
-
scope :with_settings, -> {
|
7
|
-
joins("JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
|
8
|
-
settings.thing_type = '#{self.base_class.name}')")
|
9
|
-
.select("DISTINCT #{self.table_name}.*")
|
10
|
-
}
|
11
|
-
|
12
4
|
|
13
|
-
|
14
|
-
|
15
|
-
|
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}.*")
|
16
10
|
}
|
17
|
-
|
18
|
-
scope :
|
19
|
-
joins("
|
20
|
-
|
21
|
-
}
|
22
|
-
|
23
|
-
scope :
|
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
24
|
where('settings.id IS NULL')
|
25
|
-
|
26
|
-
|
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
27
|
}
|
28
|
-
|
29
28
|
end
|
30
|
-
|
29
|
+
|
31
30
|
def settings
|
32
31
|
ScopedSettings.for_thing(self)
|
33
32
|
end
|
34
33
|
end
|
35
34
|
end
|
36
|
-
|
37
|
-
|
@@ -4,10 +4,9 @@ module RailsSettings
|
|
4
4
|
@object = object
|
5
5
|
self
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
def self.thing_scoped
|
9
|
-
unscoped.where(:
|
9
|
+
unscoped.where(thing_type: @object.class.base_class.to_s, thing_id: @object.id)
|
10
10
|
end
|
11
|
-
|
12
11
|
end
|
13
|
-
end
|
12
|
+
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
module RailsSettings
|
2
2
|
class Settings < ActiveRecord::Base
|
3
|
-
|
4
3
|
self.table_name = table_name_prefix + 'settings'
|
5
4
|
|
6
5
|
class SettingNotFound < RuntimeError; end
|
@@ -13,106 +12,95 @@ module RailsSettings
|
|
13
12
|
@@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
|
14
13
|
end
|
15
14
|
|
16
|
-
#get
|
17
|
-
def
|
18
|
-
|
19
|
-
super(method, *args)
|
20
|
-
rescue NoMethodError
|
21
|
-
#set a value for a variable
|
22
|
-
if method_name =~ /=$/
|
23
|
-
var_name = method_name.gsub('=', '')
|
24
|
-
value = args.first
|
25
|
-
self[var_name] = value
|
26
|
-
#retrieve a value
|
27
|
-
else
|
28
|
-
self[method_name]
|
29
|
-
end
|
15
|
+
# get the value field, YAML decoded
|
16
|
+
def value
|
17
|
+
YAML.load(self[:value])
|
30
18
|
end
|
31
19
|
|
32
|
-
#
|
33
|
-
def
|
34
|
-
|
35
|
-
obj = object(var_name)
|
36
|
-
unless obj.nil?
|
37
|
-
obj.destroy
|
38
|
-
true
|
39
|
-
else
|
40
|
-
raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
|
41
|
-
end
|
20
|
+
# set the value field, YAML encoded
|
21
|
+
def value=(new_value)
|
22
|
+
self[:value] = new_value.to_yaml
|
42
23
|
end
|
43
24
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
25
|
+
class << self
|
26
|
+
# get or set a variable with the variable as the called method
|
27
|
+
def method_missing(method, *args)
|
28
|
+
method_name = method.to_s
|
29
|
+
super(method, *args)
|
30
|
+
rescue NoMethodError
|
31
|
+
# set a value for a variable
|
32
|
+
if method_name[-1] == '='
|
33
|
+
var_name = method_name.sub('=', '')
|
34
|
+
value = args.first
|
35
|
+
self[var_name] = value
|
36
|
+
else
|
37
|
+
# retrieve a value
|
38
|
+
self[method_name]
|
39
|
+
end
|
49
40
|
end
|
50
41
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
if sql
|
60
|
-
vars = thing_scoped.where(sql)
|
42
|
+
# destroy the specified settings record
|
43
|
+
def destroy(var_name)
|
44
|
+
var_name = var_name.to_s
|
45
|
+
obj = object(var_name)
|
46
|
+
raise SettingNotFound, "Setting variable \"#{var_name}\" not found" if obj.nil?
|
47
|
+
|
48
|
+
obj.destroy
|
49
|
+
true
|
61
50
|
end
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
51
|
+
|
52
|
+
# retrieve all settings as a hash (optionally starting with a given namespace)
|
53
|
+
def get_all(starting_with = nil)
|
54
|
+
vars = thing_scoped.select('var, value')
|
55
|
+
vars = vars.where("var LIKE '#{starting_with}%'") if starting_with
|
56
|
+
|
57
|
+
result = {}
|
58
|
+
vars.each do |record|
|
59
|
+
result[record.var] = record.value
|
60
|
+
end
|
61
|
+
result.with_indifferent_access
|
73
62
|
end
|
74
|
-
end
|
75
63
|
|
76
|
-
|
77
|
-
|
78
|
-
|
64
|
+
def where(sql = nil)
|
65
|
+
vars = thing_scoped.where(sql) if sql
|
66
|
+
vars
|
67
|
+
end
|
79
68
|
|
80
|
-
|
81
|
-
|
82
|
-
|
69
|
+
# get a setting value by [] notation
|
70
|
+
def [](var_name)
|
71
|
+
object(var_name).try(:value) || @@defaults[var_name.to_s]
|
72
|
+
end
|
83
73
|
|
84
|
-
value
|
85
|
-
|
74
|
+
# set a setting value by [] notation
|
75
|
+
def []=(var_name, value)
|
76
|
+
var_name = var_name.to_s
|
86
77
|
|
87
|
-
|
88
|
-
|
78
|
+
record = object(var_name) || thing_scoped.new(var: var_name)
|
79
|
+
record.value = value
|
80
|
+
record.save!
|
89
81
|
|
90
|
-
|
91
|
-
|
82
|
+
value
|
83
|
+
end
|
92
84
|
|
93
|
-
|
94
|
-
|
85
|
+
def merge!(var_name, hash_value)
|
86
|
+
raise ArgumentError unless hash_value.is_a?(Hash)
|
95
87
|
|
96
|
-
|
97
|
-
|
88
|
+
old_value = self[var_name] || {}
|
89
|
+
raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
|
98
90
|
|
99
|
-
|
100
|
-
|
101
|
-
end
|
91
|
+
new_value = old_value.merge(hash_value)
|
92
|
+
self[var_name] = new_value if new_value != old_value
|
102
93
|
|
103
|
-
|
104
|
-
|
105
|
-
YAML::load(self[:value])
|
106
|
-
end
|
94
|
+
new_value
|
95
|
+
end
|
107
96
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
end
|
97
|
+
def object(var_name)
|
98
|
+
thing_scoped.where(var: var_name.to_s).first
|
99
|
+
end
|
112
100
|
|
113
|
-
|
114
|
-
|
101
|
+
def thing_scoped
|
102
|
+
unscoped.where('thing_type is NULL and thing_id is NULL')
|
103
|
+
end
|
115
104
|
end
|
116
|
-
|
117
105
|
end
|
118
106
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-settings-cached
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Squeegy
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2015-08-27 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rails
|
@@ -19,14 +19,56 @@ dependencies:
|
|
19
19
|
requirements:
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 4.
|
22
|
+
version: 4.2.0
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 4.
|
29
|
+
version: 4.2.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 3.3.0
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 3.3.0
|
44
|
+
- !ruby/object:Gem::Dependency
|
45
|
+
name: rubocop
|
46
|
+
requirement: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
type: :development
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: sqlite3
|
60
|
+
requirement: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 1.3.10
|
65
|
+
type: :development
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: 1.3.10
|
30
72
|
description:
|
31
73
|
email: huacnlee@gmail.com
|
32
74
|
executables: []
|
@@ -61,13 +103,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
103
|
version: '0'
|
62
104
|
requirements: []
|
63
105
|
rubyforge_project:
|
64
|
-
rubygems_version: 2.
|
106
|
+
rubygems_version: 2.4.7
|
65
107
|
signing_key:
|
66
108
|
specification_version: 4
|
67
|
-
summary: This is
|
109
|
+
summary: This is improved from rails-settings, added caching. Settings is a plugin
|
68
110
|
that makes managing a table of global key, value pairs easy. Think of it like a
|
69
111
|
global Hash stored in you database, that uses simple ActiveRecord like methods for
|
70
112
|
manipulation. Keep track of any global setting that you dont want to hard code
|
71
113
|
into your rails app. You can store any kind of object. Strings, numbers, arrays,
|
72
114
|
or any object. Ported to Rails 3!
|
73
115
|
test_files: []
|
116
|
+
has_rdoc:
|