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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b57b02b1ed6e6025882eb8f66f00811e31187d0
4
- data.tar.gz: 9e9f7ce1783329c95f1cc1d3a22a40c39e605d97
3
+ metadata.gz: c84a9d183c4d530e76b1c5fa3ef5e361732ac4aa
4
+ data.tar.gz: 57c5def39a8c796f5d0ecaeb2474a53ff27e541d
5
5
  SHA512:
6
- metadata.gz: e0c62a32615cf53c34729be73ca94be87826bdfef304e231333cf1cbfad1d0543bf23cc9a10a4ba8633626e92269e750ced273080d26430b195b064cbd4d7bc6
7
- data.tar.gz: 29fd6bb464c1a5ea65f9cc0a018a2659a7f96d4521d41e59febb4819f90139c48f7ace193da1833dcdc2e1decffd58d1d95c2682c73748def7f7420125b4554f
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
- # Gets a setting's value
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
- has_key?(key) ? @temp_settings[key.to_sym] : SettingAccessors.setting_class.get(key, @record)
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
  #
@@ -95,4 +95,4 @@ module SettingAccessors::Integration
95
95
  def settings
96
96
  @settings_accessor ||= SettingAccessors::Accessor.new(self)
97
97
  end
98
- end
98
+ end
@@ -1,3 +1,3 @@
1
1
  module SettingAccessors
2
- VERSION = '0.0.10'
2
+ VERSION = '0.0.11'
3
3
  end
@@ -43,25 +43,54 @@ class UserTest < ActiveSupport::TestCase
43
43
  @user = User.create
44
44
  end
45
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
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
- 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'}
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
- 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, {}
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
@@ -7,7 +7,6 @@ require 'active_support'
7
7
  require 'minitest'
8
8
  require 'shoulda'
9
9
  require "rails/test_help"
10
- require 'byebug'
11
10
 
12
11
  Rails.backtrace_cleaner.remove_silencers!
13
12
 
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.10
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-03-26 00:00:00.000000000 Z
11
+ date: 2016-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler