From 45d85f40546bcb971277dff3c801cfc46e35974b Mon Sep 17 00:00:00 2001 From: Navin Date: Mon, 8 Jul 2013 12:51:35 +0200 Subject: [PATCH] If the change doesn't go through, don't log anything --- lib/boost_trust_level.rb | 11 ++++++----- spec/components/boost_trust_level_spec.rb | 4 ++-- spec/controllers/admin/users_controller_spec.rb | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/boost_trust_level.rb b/lib/boost_trust_level.rb index 8691c1a1b06..50785270ea0 100644 --- a/lib/boost_trust_level.rb +++ b/lib/boost_trust_level.rb @@ -11,12 +11,13 @@ class BoostTrustLevel end def save! + success = if @level < @user.trust_level + demote! + else + @user.update_attributes!(trust_level: @level) + end @logger.log_trust_level_change(@user, @level) - if @level < @user.trust_level - demote! - else - @user.update_attributes!(trust_level: @level) - end + success end protected diff --git a/spec/components/boost_trust_level_spec.rb b/spec/components/boost_trust_level_spec.rb index b7fd528f7ef..3974aecbb62 100644 --- a/spec/components/boost_trust_level_spec.rb +++ b/spec/components/boost_trust_level_spec.rb @@ -48,8 +48,8 @@ describe BoostTrustLevel do user.update_attributes(trust_level: TrustLevel.levels[:basic]) end - it "should not demote the user but log the action anyway" do - AdminLogger.any_instance.expects(:log_trust_level_change).with(user, TrustLevel.levels[:newuser]).once + it "should not demote the user and not log the action" do + AdminLogger.any_instance.expects(:log_trust_level_change).with(user, TrustLevel.levels[:newuser]).never boostr = BoostTrustLevel.new(user: user, level: TrustLevel.levels[:newuser], logger: logger) expect { boostr.save! }.to raise_error(Discourse::InvalidAccess, "You attempted to demote #{user.name} to 'newuser'. However their trust level is already 'basic'. #{user.name} will remain at 'basic'") user.trust_level.should == TrustLevel.levels[:basic] diff --git a/spec/controllers/admin/users_controller_spec.rb b/spec/controllers/admin/users_controller_spec.rb index 239a5a88f3a..036e557bb0a 100644 --- a/spec/controllers/admin/users_controller_spec.rb +++ b/spec/controllers/admin/users_controller_spec.rb @@ -144,7 +144,7 @@ describe Admin::UsersController do end it "raises an error when demoting a user below their current trust level" do - AdminLogger.any_instance.expects(:log_trust_level_change).with(@another_user, TrustLevel.levels[:newuser]).once + AdminLogger.any_instance.expects(:log_trust_level_change).with(@another_user, TrustLevel.levels[:newuser]).never @another_user.topics_entered = SiteSetting.basic_requires_topics_entered + 1 @another_user.posts_read_count = SiteSetting.basic_requires_read_posts + 1 @another_user.time_read = SiteSetting.basic_requires_time_spent_mins * 60