setting_accessors 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/setting_accessors/accessor.rb +16 -3
- data/lib/setting_accessors/integration.rb +1 -1
- data/lib/setting_accessors/version.rb +1 -1
- data/test/dummy/test/models/user_test.rb +45 -16
- data/test/test_helper.rb +0 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c84a9d183c4d530e76b1c5fa3ef5e361732ac4aa
|
4
|
+
data.tar.gz: 57c5def39a8c796f5d0ecaeb2474a53ff27e541d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa28c3070e77ce1a3abaf62ca71335cd9118b6846e5327885572fa4bbe09143d8b932e9b5544615e11bf20327223df9507e2740d59a4793eefea540277d7d44c
|
7
|
+
data.tar.gz: f849a1ef5b62ed6c9658194fdf3399e1670b32ff3154aaee18729fadb08c256eccf228bc611c34078a14025863a4952ea7451c3f596e0b94a6790db4f66ed38e
|
@@ -1,7 +1,6 @@
|
|
1
1
|
#
|
2
2
|
# Helper class to make accessing record specific settings easier
|
3
3
|
#
|
4
|
-
|
5
4
|
class SettingAccessors::Accessor
|
6
5
|
|
7
6
|
def initialize(record)
|
@@ -10,10 +9,24 @@ class SettingAccessors::Accessor
|
|
10
9
|
end
|
11
10
|
|
12
11
|
#
|
13
|
-
#
|
12
|
+
# Tries to retrieve the given setting's value from the temp settings
|
13
|
+
# (already read/written values in this instance). If the setting hasn't been
|
14
|
+
# used before, its value is retrieved from the database.
|
15
|
+
#
|
16
|
+
# If a setting hasn't been read by this record (instance) before, its value
|
17
|
+
# is stored in the local read set.
|
18
|
+
#
|
19
|
+
# TODO: See if this causes problems with read settings not being updated by external changes.
|
20
|
+
# User1: Read Setting X
|
21
|
+
# User2: Update Setting X
|
22
|
+
# User1: Read Setting X -> Gets old value from temp settings.
|
23
|
+
# This shouldn't be too dangerous as the system state will be refreshed with every request though.
|
14
24
|
#
|
15
25
|
def [](key)
|
16
|
-
|
26
|
+
return @temp_settings[key.to_sym] if has_key?(key)
|
27
|
+
value = SettingAccessors.setting_class.get(key, @record)
|
28
|
+
@temp_settings[key.to_sym] = value unless value.nil?
|
29
|
+
value
|
17
30
|
end
|
18
31
|
|
19
32
|
#
|
@@ -43,25 +43,54 @@ class UserTest < ActiveSupport::TestCase
|
|
43
43
|
@user = User.create
|
44
44
|
end
|
45
45
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
46
|
+
context 'when being assigned an initial value' do
|
47
|
+
should 'be created in database' do
|
48
|
+
@user.polymorphic_setting = {:a => :b}
|
49
|
+
assert @user.save
|
50
|
+
assert_equal User.last, @user
|
51
|
+
assert_equal User.last.polymorphic_setting, {:a => :b}
|
52
|
+
end
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
54
|
+
should 'be created in database if one of their properties changes' do
|
55
|
+
@user.polymorphic_setting[:new_key] = 'new_value'
|
56
|
+
assert @user.save
|
57
|
+
assert_equal User.last, @user
|
58
|
+
assert_equal({:new_key => 'new_value'}, User.last.polymorphic_setting)
|
59
|
+
end
|
60
|
+
|
61
|
+
should 'not change the value of other assignable settings' do
|
62
|
+
@user2 = User.create
|
63
|
+
@user2.polymorphic_setting = {:foo => :bar}
|
64
|
+
assert @user2.save
|
65
|
+
assert_equal User.first.polymorphic_setting, {}
|
66
|
+
end
|
58
67
|
end
|
59
68
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
69
|
+
context 'when being updated' do
|
70
|
+
setup do
|
71
|
+
@user.polymorphic_setting = {:a => :b}
|
72
|
+
assert @user.save
|
73
|
+
assert @user.reload
|
74
|
+
assert_equal({:a => :b}, @user.polymorphic_setting)
|
75
|
+
assert_equal({:a => :b}, User.last.polymorphic_setting)
|
76
|
+
end
|
77
|
+
|
78
|
+
# Single hash value changed, etc.
|
79
|
+
should 'be saved if one of their properties changes' do
|
80
|
+
@user.polymorphic_setting[:a] = :c
|
81
|
+
assert @user.save
|
82
|
+
assert @user.reload
|
83
|
+
assert_equal({:a => :c}, @user.polymorphic_setting)
|
84
|
+
assert_equal({:a => :c}, User.last.polymorphic_setting)
|
85
|
+
end
|
86
|
+
|
87
|
+
should 'be updated if their whole value changes' do
|
88
|
+
@user.polymorphic_setting = {:a => :c}
|
89
|
+
assert @user.save
|
90
|
+
assert @user.reload
|
91
|
+
assert_equal({:a => :c}, @user.polymorphic_setting)
|
92
|
+
assert_equal({:a => :c}, User.last.polymorphic_setting)
|
93
|
+
end
|
65
94
|
end
|
66
95
|
end
|
67
96
|
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.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Exner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|