Skip to content

Commit

Permalink
Fix for polymorphic settings not being updated in the database if pro…
Browse files Browse the repository at this point in the history
…perties of a used object value were changed. Bumped version to 0.0.11
  • Loading branch information
stex committed Apr 5, 2016
1 parent 50c1390 commit 2e7e908
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
19 changes: 16 additions & 3 deletions lib/setting_accessors/accessor.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#
# Helper class to make accessing record specific settings easier
#

class SettingAccessors::Accessor

def initialize(record)
Expand All @@ -10,10 +9,24 @@ def initialize(record)
end

#
# Gets a setting's value
# Tries to retrieve the given setting's value from the temp settings
# (already read/written values in this instance). If the setting hasn't been
# used before, its value is retrieved from the database.
#
# If a setting hasn't been read by this record (instance) before, its value
# is stored in the local read set.
#
# TODO: See if this causes problems with read settings not being updated by external changes.
# User1: Read Setting X
# User2: Update Setting X
# User1: Read Setting X -> Gets old value from temp settings.
# This shouldn't be too dangerous as the system state will be refreshed with every request though.
#
def [](key)
has_key?(key) ? @temp_settings[key.to_sym] : SettingAccessors.setting_class.get(key, @record)
return @temp_settings[key.to_sym] if has_key?(key)
value = SettingAccessors.setting_class.get(key, @record)
@temp_settings[key.to_sym] = value unless value.nil?
value
end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/setting_accessors/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SettingAccessors
VERSION = '0.0.10'
VERSION = '0.0.11'
end
61 changes: 45 additions & 16 deletions test/dummy/test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,54 @@ class UserTest < ActiveSupport::TestCase
@user = User.create
end

should 'be updated in database if their whole value changes' do
@user.polymorphic_setting = {:a => :b}
assert @user.save
assert_equal User.last, @user
assert_equal User.last.polymorphic_setting, {:a => :b}
end
context 'when being assigned an initial value' do
should 'be created in database' do
@user.polymorphic_setting = {:a => :b}
assert @user.save
assert_equal User.last, @user
assert_equal User.last.polymorphic_setting, {:a => :b}
end

should 'be updated in database if a property of their value changes' do
@user.polymorphic_setting[:new_key] = 'new_value'
assert @user.save
assert_equal User.last, @user
assert_equal User.last.polymorphic_setting, {:new_key => 'new_value'}
should 'be created in database if one of their properties changes' do
@user.polymorphic_setting[:new_key] = 'new_value'
assert @user.save
assert_equal User.last, @user
assert_equal({:new_key => 'new_value'}, User.last.polymorphic_setting)
end

should 'not change the value of other assignable settings' do
@user2 = User.create
@user2.polymorphic_setting = {:foo => :bar}
assert @user2.save
assert_equal User.first.polymorphic_setting, {}
end
end

should 'not update other assignable settings' do
@user2 = User.create
@user2.polymorphic_setting = {:foo => :bar}
assert @user2.save
assert_equal User.first.polymorphic_setting, {}
context 'when being updated' do
setup do
@user.polymorphic_setting = {:a => :b}
assert @user.save
assert @user.reload
assert_equal({:a => :b}, @user.polymorphic_setting)
assert_equal({:a => :b}, User.last.polymorphic_setting)
end

# Single hash value changed, etc.
should 'be saved if one of their properties changes' do
@user.polymorphic_setting[:a] = :c
assert @user.save
assert @user.reload
assert_equal({:a => :c}, @user.polymorphic_setting)
assert_equal({:a => :c}, User.last.polymorphic_setting)
end

should 'be updated if their whole value changes' do
@user.polymorphic_setting = {:a => :c}
assert @user.save
assert @user.reload
assert_equal({:a => :c}, @user.polymorphic_setting)
assert_equal({:a => :c}, User.last.polymorphic_setting)
end
end
end
end

0 comments on commit 2e7e908

Please sign in to comment.