setting_accessors 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/lib/setting_accessors/accessor.rb +19 -3
- data/lib/setting_accessors/converter.rb +2 -2
- data/lib/setting_accessors/integration.rb +1 -1
- data/lib/setting_accessors/setting_scaffold.rb +2 -2
- data/lib/setting_accessors/version.rb +1 -1
- data/setting_accessors.gemspec +1 -1
- data/test/dummy/app/models/user.rb +2 -0
- data/test/dummy/test/models/user_test.rb +27 -0
- data/test/test_helper.rb +1 -0
- metadata +5 -7
- data/.ruby-gemset +0 -1
- data/.ruby-version +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b57b02b1ed6e6025882eb8f66f00811e31187d0
|
4
|
+
data.tar.gz: 9e9f7ce1783329c95f1cc1d3a22a40c39e605d97
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0c62a32615cf53c34729be73ca94be87826bdfef304e231333cf1cbfad1d0543bf23cc9a10a4ba8633626e92269e750ced273080d26430b195b064cbd4d7bc6
|
7
|
+
data.tar.gz: 29fd6bb464c1a5ea65f9cc0a018a2659a7f96d4521d41e59febb4819f90139c48f7ace193da1833dcdc2e1decffd58d1d95c2682c73748def7f7420125b4554f
|
data/.travis.yml
CHANGED
@@ -28,6 +28,22 @@ class SettingAccessors::Accessor
|
|
28
28
|
result
|
29
29
|
end
|
30
30
|
|
31
|
+
#
|
32
|
+
# Like #fetch, but it will store the default value as a temporary setting
|
33
|
+
# if no actual setting value could be found. This is useful to further work
|
34
|
+
# with default setting values.
|
35
|
+
# The default value is cloned (using #dup to avoid copying object states) before
|
36
|
+
# it is assigned. This will not work for singleton instances like true, false, etc.
|
37
|
+
#
|
38
|
+
def fetch_and_store(key, default = nil)
|
39
|
+
result = self[key]
|
40
|
+
if result.nil? && !has_key?(key)
|
41
|
+
self[key] = default.duplicable? ? default.dup : default
|
42
|
+
else
|
43
|
+
result
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
31
47
|
def has_key?(key)
|
32
48
|
@temp_settings.has_key?(key.to_sym)
|
33
49
|
end
|
@@ -46,7 +62,7 @@ class SettingAccessors::Accessor
|
|
46
62
|
# specified in the setting config file.
|
47
63
|
#
|
48
64
|
def get_or_default(key)
|
49
|
-
|
65
|
+
fetch_and_store(key, SettingAccessors.setting_class.get_or_default(key, @record))
|
50
66
|
end
|
51
67
|
|
52
68
|
#
|
@@ -54,7 +70,7 @@ class SettingAccessors::Accessor
|
|
54
70
|
# If none is found, tries to find a global setting with the same name
|
55
71
|
#
|
56
72
|
def get_or_global(key)
|
57
|
-
|
73
|
+
fetch_and_store(key, SettingAccessors.setting_class.get(key))
|
58
74
|
end
|
59
75
|
|
60
76
|
#
|
@@ -62,7 +78,7 @@ class SettingAccessors::Accessor
|
|
62
78
|
# if none is found, it will return the given value instead.
|
63
79
|
#
|
64
80
|
def get_or_value(key, value)
|
65
|
-
|
81
|
+
fetch_and_store(key, value)
|
66
82
|
end
|
67
83
|
|
68
84
|
def get_with_fallback(key, fallback = nil)
|
@@ -25,7 +25,7 @@ class SettingAccessors::Converter
|
|
25
25
|
#
|
26
26
|
def convert(new_value)
|
27
27
|
#If the value is set to be polymorphic, we don't have to convert anything.
|
28
|
-
return new_value if @value_type == 'polymorphic'
|
28
|
+
return new_value if @value_type.to_s == 'polymorphic'
|
29
29
|
|
30
30
|
#ActiveRecord only converts non-nil values to their database type
|
31
31
|
#during assignment
|
@@ -68,4 +68,4 @@ class SettingAccessors::Converter
|
|
68
68
|
value.to_s
|
69
69
|
end
|
70
70
|
|
71
|
-
end
|
71
|
+
end
|
@@ -194,7 +194,7 @@ module SettingAccessors::SettingScaffold
|
|
194
194
|
# @return [Object] the default value for the current setting
|
195
195
|
#
|
196
196
|
def default_value
|
197
|
-
data['default']
|
197
|
+
data['default'].freeze
|
198
198
|
end
|
199
199
|
|
200
200
|
#
|
@@ -249,4 +249,4 @@ module SettingAccessors::SettingScaffold
|
|
249
249
|
SettingAccessors::Internal.setting_data(name, assignable)
|
250
250
|
end
|
251
251
|
|
252
|
-
end
|
252
|
+
end
|
data/setting_accessors.gemspec
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
class User < ActiveRecord::Base
|
2
2
|
|
3
|
+
setting_accessor :polymorphic_setting, :type => :polymorphic, :default => {}, :fallback => :default
|
4
|
+
|
3
5
|
setting_accessor :locale, :type => :string, :validations => {:presence => true}
|
4
6
|
|
5
7
|
setting_accessor :a_string, :fallback => :default
|
@@ -37,4 +37,31 @@ class UserTest < ActiveSupport::TestCase
|
|
37
37
|
assert !@user.respond_to?(:a_string?)
|
38
38
|
end
|
39
39
|
end
|
40
|
+
|
41
|
+
context 'Polymorphic class-wise settings' do
|
42
|
+
setup do
|
43
|
+
@user = User.create
|
44
|
+
end
|
45
|
+
|
46
|
+
should 'be updated in database if their whole value changes' do
|
47
|
+
@user.polymorphic_setting = {:a => :b}
|
48
|
+
assert @user.save
|
49
|
+
assert_equal User.last, @user
|
50
|
+
assert_equal User.last.polymorphic_setting, {:a => :b}
|
51
|
+
end
|
52
|
+
|
53
|
+
should 'be updated in database if a property of their value changes' do
|
54
|
+
@user.polymorphic_setting[:new_key] = 'new_value'
|
55
|
+
assert @user.save
|
56
|
+
assert_equal User.last, @user
|
57
|
+
assert_equal User.last.polymorphic_setting, {:new_key => 'new_value'}
|
58
|
+
end
|
59
|
+
|
60
|
+
should 'not update other assignable settings' do
|
61
|
+
@user2 = User.create
|
62
|
+
@user2.polymorphic_setting = {:foo => :bar}
|
63
|
+
assert @user2.save
|
64
|
+
assert_equal User.first.polymorphic_setting, {}
|
65
|
+
end
|
66
|
+
end
|
40
67
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: setting_accessors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Exner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -100,14 +100,14 @@ dependencies:
|
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '4'
|
103
|
+
version: '4.1'
|
104
104
|
type: :runtime
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: '4'
|
110
|
+
version: '4.1'
|
111
111
|
description: |-
|
112
112
|
Adds a global key-value-store to Rails applications and allows adding typed columns
|
113
113
|
to model classes without having to change the database layout.
|
@@ -119,8 +119,6 @@ extra_rdoc_files: []
|
|
119
119
|
files:
|
120
120
|
- ".codeclimate.yml"
|
121
121
|
- ".gitignore"
|
122
|
-
- ".ruby-gemset"
|
123
|
-
- ".ruby-version"
|
124
122
|
- ".travis.yml"
|
125
123
|
- Gemfile
|
126
124
|
- LICENSE.txt
|
@@ -217,7 +215,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
217
215
|
version: '0'
|
218
216
|
requirements: []
|
219
217
|
rubyforge_project:
|
220
|
-
rubygems_version: 2.
|
218
|
+
rubygems_version: 2.5.1
|
221
219
|
signing_key:
|
222
220
|
specification_version: 4
|
223
221
|
summary: A global key-value-store and virtual model columns
|
data/.ruby-gemset
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
setting_accessors
|
data/.ruby-version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
ruby-2.1.1
|