settingson 1.7.6 → 1.7.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.gitignore +1 -1
- data/.travis.yml +9 -3
- data/Appraisals +10 -2
- data/README.md +25 -6
- data/Rakefile +5 -2
- data/app/models/concerns/settingson/base.rb +12 -7
- data/gemfiles/4.2.gemfile +3 -3
- data/gemfiles/4.2.gemfile.lock +86 -92
- data/gemfiles/5.0.gemfile +3 -3
- data/gemfiles/5.0.gemfile.lock +96 -101
- data/gemfiles/5.1.gemfile +8 -0
- data/gemfiles/5.1.gemfile.lock +168 -0
- data/gemfiles/5.2.gemfile +8 -0
- data/gemfiles/5.2.gemfile.lock +168 -0
- data/lib/generators/settingson/settingson_generator.rb +4 -3
- data/lib/settingson/config.rb +1 -0
- data/lib/settingson/default.rb +11 -0
- data/lib/settingson/store.rb +32 -23
- data/lib/settingson/store/default.rb +25 -0
- data/lib/settingson/store/general.rb +3 -0
- data/lib/settingson/version.rb +1 -1
- data/settingson.gemspec +2 -3
- data/spec/dummy/config/environments/development.rb +2 -0
- data/spec/dummy/config/environments/test.rb +7 -2
- data/spec/dummy/config/initializers/settingson.rb +1 -0
- data/spec/models/settings_spec.rb +12 -0
- data/spec/models/settingson/defaults_spec.rb +36 -0
- data/spec/spec_helper.rb +3 -3
- metadata +14 -20
@@ -0,0 +1,168 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ..
|
3
|
+
specs:
|
4
|
+
settingson (1.7.10)
|
5
|
+
rails (>= 4.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actioncable (5.1.4)
|
11
|
+
actionpack (= 5.1.4)
|
12
|
+
nio4r (~> 2.0)
|
13
|
+
websocket-driver (~> 0.6.1)
|
14
|
+
actionmailer (5.1.4)
|
15
|
+
actionpack (= 5.1.4)
|
16
|
+
actionview (= 5.1.4)
|
17
|
+
activejob (= 5.1.4)
|
18
|
+
mail (~> 2.5, >= 2.5.4)
|
19
|
+
rails-dom-testing (~> 2.0)
|
20
|
+
actionpack (5.1.4)
|
21
|
+
actionview (= 5.1.4)
|
22
|
+
activesupport (= 5.1.4)
|
23
|
+
rack (~> 2.0)
|
24
|
+
rack-test (>= 0.6.3)
|
25
|
+
rails-dom-testing (~> 2.0)
|
26
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
27
|
+
actionview (5.1.4)
|
28
|
+
activesupport (= 5.1.4)
|
29
|
+
builder (~> 3.1)
|
30
|
+
erubi (~> 1.4)
|
31
|
+
rails-dom-testing (~> 2.0)
|
32
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
33
|
+
activejob (5.1.4)
|
34
|
+
activesupport (= 5.1.4)
|
35
|
+
globalid (>= 0.3.6)
|
36
|
+
activemodel (5.1.4)
|
37
|
+
activesupport (= 5.1.4)
|
38
|
+
activerecord (5.1.4)
|
39
|
+
activemodel (= 5.1.4)
|
40
|
+
activesupport (= 5.1.4)
|
41
|
+
arel (~> 8.0)
|
42
|
+
activesupport (5.1.4)
|
43
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
44
|
+
i18n (~> 0.7)
|
45
|
+
minitest (~> 5.1)
|
46
|
+
tzinfo (~> 1.1)
|
47
|
+
appraisal (2.2.0)
|
48
|
+
bundler
|
49
|
+
rake
|
50
|
+
thor (>= 0.14.0)
|
51
|
+
arel (8.0.0)
|
52
|
+
builder (3.2.3)
|
53
|
+
codeclimate-test-reporter (1.0.8)
|
54
|
+
simplecov (<= 0.13)
|
55
|
+
concurrent-ruby (1.0.5)
|
56
|
+
crass (1.0.3)
|
57
|
+
database_cleaner (1.6.2)
|
58
|
+
diff-lcs (1.3)
|
59
|
+
docile (1.1.5)
|
60
|
+
erubi (1.7.0)
|
61
|
+
faker (1.8.4)
|
62
|
+
i18n (~> 0.5)
|
63
|
+
globalid (0.4.1)
|
64
|
+
activesupport (>= 4.2.0)
|
65
|
+
i18n (0.9.1)
|
66
|
+
concurrent-ruby (~> 1.0)
|
67
|
+
json (2.1.0)
|
68
|
+
loofah (2.1.1)
|
69
|
+
crass (~> 1.0.2)
|
70
|
+
nokogiri (>= 1.5.9)
|
71
|
+
mail (2.7.0)
|
72
|
+
mini_mime (>= 0.1.1)
|
73
|
+
method_source (0.9.0)
|
74
|
+
mini_mime (1.0.0)
|
75
|
+
mini_portile2 (2.3.0)
|
76
|
+
minitest (5.11.3)
|
77
|
+
nio4r (2.1.0)
|
78
|
+
nokogiri (1.8.1)
|
79
|
+
mini_portile2 (~> 2.3.0)
|
80
|
+
rack (2.0.3)
|
81
|
+
rack-test (0.8.2)
|
82
|
+
rack (>= 1.0, < 3)
|
83
|
+
rails (5.1.4)
|
84
|
+
actioncable (= 5.1.4)
|
85
|
+
actionmailer (= 5.1.4)
|
86
|
+
actionpack (= 5.1.4)
|
87
|
+
actionview (= 5.1.4)
|
88
|
+
activejob (= 5.1.4)
|
89
|
+
activemodel (= 5.1.4)
|
90
|
+
activerecord (= 5.1.4)
|
91
|
+
activesupport (= 5.1.4)
|
92
|
+
bundler (>= 1.3.0)
|
93
|
+
railties (= 5.1.4)
|
94
|
+
sprockets-rails (>= 2.0.0)
|
95
|
+
rails-dom-testing (2.0.3)
|
96
|
+
activesupport (>= 4.2.0)
|
97
|
+
nokogiri (>= 1.6)
|
98
|
+
rails-dummy (0.0.4)
|
99
|
+
rails
|
100
|
+
rails-html-sanitizer (1.0.3)
|
101
|
+
loofah (~> 2.0)
|
102
|
+
railties (5.1.4)
|
103
|
+
actionpack (= 5.1.4)
|
104
|
+
activesupport (= 5.1.4)
|
105
|
+
method_source
|
106
|
+
rake (>= 0.8.7)
|
107
|
+
thor (>= 0.18.1, < 2.0)
|
108
|
+
rake (12.3.2)
|
109
|
+
rspec (3.7.0)
|
110
|
+
rspec-core (~> 3.7.0)
|
111
|
+
rspec-expectations (~> 3.7.0)
|
112
|
+
rspec-mocks (~> 3.7.0)
|
113
|
+
rspec-core (3.7.0)
|
114
|
+
rspec-support (~> 3.7.0)
|
115
|
+
rspec-expectations (3.7.0)
|
116
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
117
|
+
rspec-support (~> 3.7.0)
|
118
|
+
rspec-mocks (3.7.0)
|
119
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
120
|
+
rspec-support (~> 3.7.0)
|
121
|
+
rspec-rails (3.7.2)
|
122
|
+
actionpack (>= 3.0)
|
123
|
+
activesupport (>= 3.0)
|
124
|
+
railties (>= 3.0)
|
125
|
+
rspec-core (~> 3.7.0)
|
126
|
+
rspec-expectations (~> 3.7.0)
|
127
|
+
rspec-mocks (~> 3.7.0)
|
128
|
+
rspec-support (~> 3.7.0)
|
129
|
+
rspec-support (3.7.0)
|
130
|
+
simplecov (0.13.0)
|
131
|
+
docile (~> 1.1.0)
|
132
|
+
json (>= 1.8, < 3)
|
133
|
+
simplecov-html (~> 0.10.0)
|
134
|
+
simplecov-html (0.10.2)
|
135
|
+
sprockets (3.7.1)
|
136
|
+
concurrent-ruby (~> 1.0)
|
137
|
+
rack (> 1, < 3)
|
138
|
+
sprockets-rails (3.2.1)
|
139
|
+
actionpack (>= 4.0)
|
140
|
+
activesupport (>= 4.0)
|
141
|
+
sprockets (>= 3.0.0)
|
142
|
+
sqlite3 (1.3.13)
|
143
|
+
thor (0.20.0)
|
144
|
+
thread_safe (0.3.6)
|
145
|
+
tzinfo (1.2.4)
|
146
|
+
thread_safe (~> 0.1)
|
147
|
+
websocket-driver (0.6.5)
|
148
|
+
websocket-extensions (>= 0.1.0)
|
149
|
+
websocket-extensions (0.1.3)
|
150
|
+
|
151
|
+
PLATFORMS
|
152
|
+
ruby
|
153
|
+
|
154
|
+
DEPENDENCIES
|
155
|
+
appraisal
|
156
|
+
bundler (>= 1.6)
|
157
|
+
codeclimate-test-reporter
|
158
|
+
database_cleaner
|
159
|
+
faker
|
160
|
+
rails (~> 5.1)
|
161
|
+
rails-dummy
|
162
|
+
rspec (~> 3.5)
|
163
|
+
rspec-rails (~> 3.5)
|
164
|
+
settingson!
|
165
|
+
sqlite3
|
166
|
+
|
167
|
+
BUNDLED WITH
|
168
|
+
1.17.2
|
@@ -28,8 +28,9 @@ class SettingsonGenerator < Rails::Generators::NamedBase
|
|
28
28
|
|
29
29
|
private
|
30
30
|
def settingson_inject_lines(name)
|
31
|
-
|
32
|
-
|
31
|
+
file = Rails.root.join("app/models/#{name.downcase}.rb")
|
32
|
+
if File.readlines(file).grep(/\A\s*include Settingson::Base\z/).blank?
|
33
|
+
inject_into_class file, name.camelize, "\tinclude Settingson::Base\n"
|
33
34
|
end
|
34
35
|
end
|
35
|
-
end
|
36
|
+
end
|
data/lib/settingson/config.rb
CHANGED
@@ -0,0 +1,11 @@
|
|
1
|
+
class Settingson::Default
|
2
|
+
require 'settingson/default/store'
|
3
|
+
|
4
|
+
def initialize(klass:)
|
5
|
+
@__klass = klass
|
6
|
+
end # initialize
|
7
|
+
|
8
|
+
def method_missing(symbol, *args)
|
9
|
+
Settingson::Default::Store.new( klass: @__klass ).send(symbol, *args)
|
10
|
+
end # method_missing
|
11
|
+
end
|
data/lib/settingson/store.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
class Settingson::Store
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
require 'settingson/store/default'
|
4
|
+
require 'settingson/store/general'
|
5
|
+
|
6
6
|
def initialize(klass:, path: nil)
|
7
|
-
@__klass
|
8
|
-
@__path
|
7
|
+
@__klass = klass
|
8
|
+
@__path = path
|
9
|
+
@__config = klass.configure
|
9
10
|
end
|
10
11
|
|
11
12
|
def to_s
|
@@ -34,8 +35,8 @@ class Settingson::Store
|
|
34
35
|
|
35
36
|
def method_missing(symbol, *args)
|
36
37
|
__debug
|
37
|
-
__debug("from\n\t#{caller[1..@
|
38
|
-
@
|
38
|
+
__debug("from\n\t#{caller[1..@__config.trace].join("\n\t")}") if
|
39
|
+
@__config.trace > 0
|
39
40
|
|
40
41
|
__references_action(symbol, *args) or __rescue_action(symbol.to_s, *args)
|
41
42
|
# __rescue_action(symbol.to_s, *args)
|
@@ -44,11 +45,12 @@ class Settingson::Store
|
|
44
45
|
protected
|
45
46
|
# TODO: move all methods to support class
|
46
47
|
def __debug(message="")
|
48
|
+
return unless @__config.debug
|
47
49
|
message = sprintf("%s#%20s: %s",
|
48
50
|
self.class.name,
|
49
51
|
caller_locations.first.label,
|
50
52
|
message)
|
51
|
-
Rails.logger.debug(message)
|
53
|
+
Rails.logger.debug(message)
|
52
54
|
end
|
53
55
|
|
54
56
|
def __references_action(symbol, *args)
|
@@ -56,7 +58,7 @@ class Settingson::Store
|
|
56
58
|
# return nil
|
57
59
|
# return nil unless ['model_name', 'to_model'].include?(symbol.to_s)
|
58
60
|
if @__klass and @__klass.respond_to?(symbol)
|
59
|
-
__debug("#{@__klass
|
61
|
+
__debug("#{@__klass} know what to do with #{symbol}")
|
60
62
|
@__klass.send(symbol, *args)
|
61
63
|
end
|
62
64
|
end
|
@@ -91,15 +93,17 @@ class Settingson::Store
|
|
91
93
|
else
|
92
94
|
@__klass.create!(key: @__path, value: value)
|
93
95
|
end
|
94
|
-
|
96
|
+
|
97
|
+
Rails.cache.write(__cache_key(@__path), value) if @__config.cache.enabled
|
95
98
|
value
|
96
99
|
end
|
97
100
|
|
98
101
|
def __get(key)
|
99
102
|
__update_search_path(key)
|
100
|
-
result =
|
103
|
+
result = __look_up_value(@__path)
|
101
104
|
|
102
|
-
if result.is_a?(ActiveRecord::RecordNotFound)
|
105
|
+
if result.is_a?(ActiveRecord::RecordNotFound) or
|
106
|
+
result.is_a?(Settingson::Store::Default)
|
103
107
|
__debug("return self with path: #{@__path}")
|
104
108
|
self
|
105
109
|
else
|
@@ -146,37 +150,42 @@ class Settingson::Store
|
|
146
150
|
key.try(:to_key).try(:join, '_') || key.id
|
147
151
|
end
|
148
152
|
|
153
|
+
def _search_path(key)
|
154
|
+
[@__path, key].compact.join('.')
|
155
|
+
end
|
156
|
+
|
149
157
|
def __update_search_path(key)
|
150
|
-
@__path =
|
158
|
+
@__path = _search_path(key)
|
151
159
|
end
|
152
160
|
|
153
|
-
def
|
154
|
-
result =
|
161
|
+
def __look_up_value(key)
|
162
|
+
result = @__config.cache.enabled ? __from_cache(key) : __from_db(key)
|
155
163
|
|
156
|
-
if result.is_a?(ActiveRecord::RecordNotFound)
|
157
|
-
|
164
|
+
if result.is_a?(ActiveRecord::RecordNotFound)
|
165
|
+
__debug("looking in #{@__klass.name}.defaults[#{key}]")
|
166
|
+
@__klass.defaults[key]
|
158
167
|
else
|
159
168
|
result
|
160
169
|
end
|
161
170
|
end
|
162
171
|
|
163
172
|
def __cache_key(key)
|
164
|
-
[ @
|
173
|
+
[ @__config.cache.namespace, key ].join('/')
|
165
174
|
end
|
166
175
|
|
167
|
-
def
|
176
|
+
def __from_cache(key)
|
168
177
|
__debug("looking in cache '#{__cache_key(key)}'")
|
169
178
|
Rails.cache.fetch(
|
170
179
|
__cache_key(key),
|
171
|
-
expires_in: @
|
172
|
-
race_condition_ttl: @
|
180
|
+
expires_in: @__config.cache.expires,
|
181
|
+
race_condition_ttl: @__config.cache.race_condition_ttl
|
173
182
|
) do
|
174
183
|
__debug("ask DB '#{key}'")
|
175
|
-
|
184
|
+
__from_db(key)
|
176
185
|
end
|
177
186
|
end
|
178
187
|
|
179
|
-
def
|
188
|
+
def __from_db(key)
|
180
189
|
@__klass.find_by!(key: key).value
|
181
190
|
rescue ActiveRecord::RecordNotFound
|
182
191
|
__debug("not found")
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Settingson::Store::Default < Settingson::Store
|
2
|
+
|
3
|
+
@@__defaults = {}
|
4
|
+
|
5
|
+
def to_h
|
6
|
+
@@__defaults
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_ary
|
10
|
+
@@__defaults.to_a
|
11
|
+
end
|
12
|
+
|
13
|
+
protected
|
14
|
+
def __set(key, value)
|
15
|
+
@@__defaults[_search_path(key)] = value
|
16
|
+
@__path = nil
|
17
|
+
value
|
18
|
+
end
|
19
|
+
|
20
|
+
def __get(key)
|
21
|
+
__update_search_path(key)
|
22
|
+
@@__defaults[@__path] || self
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/lib/settingson/version.rb
CHANGED
data/settingson.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Settingson::VERSION
|
9
9
|
spec.authors = ["dan"]
|
10
10
|
spec.email = ["daan.forever@gmail.com"]
|
11
|
-
spec.summary = %q{Settings management for Ruby on Rails
|
12
|
-
spec.description = %q{Settings management for Ruby on Rails
|
11
|
+
spec.summary = %q{Settings management for Ruby on Rails applications (with ActiveRecord) }
|
12
|
+
spec.description = %q{Settings management for Ruby on Rails applications (with ActiveRecord) }
|
13
13
|
spec.homepage = "https://github.com/daanforever/settingson"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -26,7 +26,6 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.add_development_dependency "rspec", "~> 3.5"
|
27
27
|
spec.add_development_dependency "rspec-rails", "~> 3.5"
|
28
28
|
spec.add_development_dependency "sqlite3"
|
29
|
-
spec.add_development_dependency "spring-commands-rspec"
|
30
29
|
spec.add_development_dependency "database_cleaner"
|
31
30
|
spec.add_development_dependency "faker"
|
32
31
|
# spec.add_development_dependency "rake"
|
@@ -13,8 +13,13 @@ Rails.application.configure do
|
|
13
13
|
config.eager_load = false
|
14
14
|
|
15
15
|
# Configure static asset server for tests with Cache-Control for performance.
|
16
|
-
|
17
|
-
|
16
|
+
if Rails::VERSION::MAJOR < 5
|
17
|
+
config.serve_static_files = true
|
18
|
+
config.static_cache_control = 'public, max-age=3600'
|
19
|
+
else
|
20
|
+
config.public_file_server.enabled = true
|
21
|
+
config.public_file_server.headers = { 'Cache-Control' => 'public, max-age=3600' }
|
22
|
+
end
|
18
23
|
|
19
24
|
# Show full error reports and disable caching.
|
20
25
|
config.consider_all_requests_local = true
|
@@ -18,6 +18,18 @@ describe Settings do
|
|
18
18
|
Settings.defaults{|s| s.some.key = word}
|
19
19
|
expect( Settings.some.key ).to eq(word)
|
20
20
|
end
|
21
|
+
|
22
|
+
it 'save and returns correct value for multiple default values' do
|
23
|
+
word1 = Faker::Lorem.word
|
24
|
+
word2 = Faker::Lorem.word
|
25
|
+
Settings.defaults do |s|
|
26
|
+
s.some.key1 = word1
|
27
|
+
s.some.key2 = word2
|
28
|
+
end
|
29
|
+
expect( Settings.some.key1 ).to eq(word1)
|
30
|
+
expect( Settings.some.key2 ).to eq(word2)
|
31
|
+
end
|
32
|
+
|
21
33
|
end
|
22
34
|
|
23
35
|
describe '::from_hash' do
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Settingson::Store::Default do
|
4
|
+
|
5
|
+
it 'not raises errors with empty block' do
|
6
|
+
expect{ Settings.defaults {} }.to_not raise_error
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'returns value for given key' do
|
10
|
+
word = Faker::Lorem.word
|
11
|
+
h = Settings.defaults{|s| s.cached_key = word}
|
12
|
+
expect( h['cached_key'] ).to eq(word)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'returns value for given key #2' do
|
16
|
+
word = Faker::Lorem.word
|
17
|
+
h = Settings.defaults do |s|
|
18
|
+
s.cached_key1 = word
|
19
|
+
s.cached.key2 = word
|
20
|
+
end
|
21
|
+
expect( h['cached.key2'] ).to eq(word)
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#to_h' do
|
25
|
+
it 'returns Hash' do
|
26
|
+
expect( Settings.defaults.to_h ).to be_a(Hash)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#to_ary' do
|
31
|
+
it 'returns Array' do
|
32
|
+
expect( Settings.defaults.to_ary ).to be_a(Array)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|