From dda5226dffe48fd1d65bb2778cc344fbcdce6320 Mon Sep 17 00:00:00 2001 From: Martin Linkhorst Date: Wed, 9 Apr 2014 15:18:26 +0200 Subject: [PATCH] fire callbacks only when setting was stored sucessfully --- app/models/setting.rb | 9 ++++++--- spec/models/setting_spec.rb | 6 ++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/models/setting.rb b/app/models/setting.rb index 335466ed31..6459a1a922 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -155,9 +155,12 @@ class Setting < ActiveRecord::Base old_value = setting.value setting.value = (v ? v : "") Rails.cache.delete(cache_key(name)) - setting.save - fire_callbacks(name, setting.value, old_value) - setting.value + if setting.save + fire_callbacks(name, setting.value, old_value) + setting.value + else + old_value + end end # Check whether a setting was defined diff --git a/spec/models/setting_spec.rb b/spec/models/setting_spec.rb index 5b2a515ea1..22956528dd 100644 --- a/spec/models/setting_spec.rb +++ b/spec/models/setting_spec.rb @@ -108,6 +108,12 @@ describe Setting do expect(collector).to_not be_empty end + it "calls no callback on invalid setting" do + Setting.any_instance.stub(:valid?).and_return(false) + Setting.notified_events = 'invalid' + expect(collector).to be_empty + end + it "calls multiple callbacks when a setting is set" do Setting.notified_events = [:some_event] Setting.notified_events = [:some_event]