Skip to content

Commit

Permalink
Merge hash instead of replacing the original value in user_context
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Oct 15, 2020
1 parent 198c3f4 commit 1f52c6e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/raven/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,13 @@ def annotate_exception(exc, options = {})
# Raven.user_context('id' => 1, 'email' => 'foo@example.com')
def user_context(options = nil)
original_user_context = context.user
context.user = options || {}

if options
context.user.merge!(options)
else
context.user = {}
end

yield if block_given?
context.user
ensure
Expand Down
10 changes: 8 additions & 2 deletions spec/raven/instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ def ivars(object)

describe "#user_context" do
context "without a block" do
it "doesn't override previous data" do
subject.user_context(id: 1)
subject.user_context(email: "test@example.com")

expect(subject.context.user).to eq({ id: 1, email: "test@example.com" })
end
it "empties the user context when called without options" do
subject.context.user = { id: 1 }
expect(subject.user_context).to eq({})
Expand All @@ -257,9 +263,9 @@ def ivars(object)
expect(subject.user_context(nil)).to eq({})
end

it "empties the user context when called with {}" do
it "doesn't empty the user context when called with {}" do
subject.context.user = { id: 1 }
expect(subject.user_context({})).to eq({})
expect(subject.user_context({})).to eq({ id: 1 })
end

it "returns the user context when set" do
Expand Down

0 comments on commit 1f52c6e

Please sign in to comment.