Skip to content

Commit

Permalink
ensure moderation is reenabled even when block raises exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbrdo committed Oct 17, 2012
1 parent 84d2cbf commit f32467b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
7 changes: 5 additions & 2 deletions lib/has_moderated/common.rb
Expand Up @@ -37,8 +37,11 @@ module InstanceMethods
def without_moderation(do_disable = true)
already_disabled = @moderation_disabled
self.moderation_disabled = true if do_disable
retval = yield(self)
self.moderation_disabled = false if do_disable && !already_disabled
begin
retval = yield(self)
ensure
self.moderation_disabled = false if do_disable && !already_disabled
end
retval
end

Expand Down
10 changes: 7 additions & 3 deletions lib/has_moderated/moderation_model.rb
Expand Up @@ -7,9 +7,13 @@ def self.included(base)
self.moderation_disabled = false

def self.without_moderation(do_disable = true)
already_disabled = self.moderation_disabled
self.moderation_disabled = true if do_disable
retval = yield(self)
self.moderation_disabled = false if do_disable
begin
retval = yield(self)
ensure
self.moderation_disabled = false if do_disable && !already_disabled
end
retval
end
end
Expand All @@ -35,7 +39,7 @@ def apply

def accept_changes(record, save_opts = Hash.new)
if record
HasModerated::Common::try_without_moderation(record) do
Moderation.without_moderation do
# run validations (issue #12)
record.save!(save_opts)
end
Expand Down
1 change: 1 addition & 0 deletions test/dummy/spec/models/task_spec.rb
Expand Up @@ -707,6 +707,7 @@ def self.moderatable_discard(m)
end
Moderation.count.should eq(0)
Task.first.title.should eq("Task 2")
Moderation.moderation_disabled.should be_false
t.title = "Task 3"
t.save
Moderation.count.should eq(1)
Expand Down

0 comments on commit f32467b

Please sign in to comment.