Skip to content

Commit

Permalink
Add person data to the payload rollbar.js config
Browse files Browse the repository at this point in the history
@and-megan I've added a test for this
  • Loading branch information
Jon de Andres committed Jun 14, 2017
1 parent e6c0ba4 commit a453239
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/rollbar/middleware/js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,21 @@ def close_old_response(response)

def config_js_tag(env)
js_config = config[:options].dup
if js_config.try(:[], :payload).try(:[], :person_tracking)
person_data = extract_person_data_from_controller(env)
js_config[:payload][:person] = person_data if person_data
end

add_person_data(js_config, env)

script_tag("var _rollbarConfig = #{js_config.to_json};", env)
end

def add_person_data(js_config, env)
person_data = extract_person_data_from_controller(env)

return if person_data && person_data.empty?

js_config[:payload] ||= {}
js_config[:payload][:person] = person_data if person_data
end

def snippet_js_tag(env)
script_tag(js_snippet, env)
end
Expand Down
51 changes: 51 additions & 0 deletions spec/rollbar/middleware/js_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,57 @@ def self.get
allow(subject).to receive(:add_js).and_raise(StandardError.new)
end
end

context 'with person data' do
let(:body) { [html] }
let(:status) { 200 }
let(:headers) do
{ 'Content-Type' => content_type }
end
let(:env) do
{
'rollbar.person_data' => {
id: 100,
username: 'foo',
email: 'foo@bar.com'
}
}
end
let(:expected_js_options) do
{
:foo => :bar,
:payload => {
:person => {
id: 100,
username: 'foo',
email: 'foo@bar.com'
}
}
}
end

it 'adds the person data to the configuration' do
_, _, response = subject.call(env)
new_body = response.body.join

expect(new_body).to include(expected_js_options.to_json)
end

context 'when the person data is nil' do
let(:env) do
{
'rollbar.person_data' => nil
}
end

it 'works correctly and doesnt add anything about person data' do
_, _, response = subject.call(env)
new_body = response.body.join

expect(new_body).not_to include('person')
end
end
end
end

context 'having the config disabled', :add_js => false do
Expand Down

0 comments on commit a453239

Please sign in to comment.