Skip to content

Commit 4cc956a

Browse files
committed
Fix KeyError when using config.background_jobs = false due to not stringifying the keys
1 parent 1c8b13c commit 4cc956a

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Unreleased
44
- [View Diff](https://github.com/westonganger/rails_local_analytics/compare/v0.2.3...master)
5-
- Nothing yet
5+
- [#18](https://github.com/westonganger/rails_local_analytics/pull/18) - Fix KeyError when using `config.background_jobs = false` due to not stringifying the keys
66

77
### v0.2.3 - Dec 17 2024
88
- [View Diff](https://github.com/westonganger/rails_local_analytics/compare/v0.2.2...v0.2.3)

lib/rails_local_analytics.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def self.record_request(request:, custom_attributes: nil)
2929
json_str = JSON.generate(json_hash) # convert to json string so that its compatible with all job backends
3030
RecordRequestJob.perform_later(json_str)
3131
else
32-
RecordRequestJob.new.perform(json_hash)
32+
RecordRequestJob.new.perform(json_hash.deep_stringify_keys)
3333
end
3434
end
3535

spec/model/rails_local_analytics_spec.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@
88
end
99

1010
context "config.background_jobs" do
11-
before do
12-
@prev_background_jobs = described_class.config.background_jobs
13-
end
14-
15-
after do
16-
described_class.config.background_jobs = @prev_background_jobs
17-
end
18-
1911
it "defaults to true" do
2012
expect(described_class.config.background_jobs).to eq(true)
2113
end

spec/request/record_request_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,28 @@
2626
"url_path" => "/",
2727
})
2828
end
29+
30+
it "works with config.background_jobs = false" do
31+
RailsLocalAnalytics.config.background_jobs = false
32+
33+
get root_path
34+
expect(response.status).to eq(200)
35+
36+
expect(TrackedRequestsByDaySite.last.attributes.except("id")).to eq({
37+
"browser_engine" => nil,
38+
"day" => Date.today,
39+
"platform" => nil,
40+
"total" => 1,
41+
"url_hostname" => "www.example.com",
42+
})
43+
44+
expect(TrackedRequestsByDayPage.last.attributes.except("id")).to eq({
45+
"day" => Date.today,
46+
"referrer_hostname" => nil,
47+
"referrer_path" => nil,
48+
"total" => 1,
49+
"url_hostname" => "www.example.com",
50+
"url_path" => "/",
51+
})
52+
end
2953
end

spec/spec_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,12 @@
4848
end
4949
end
5050

51+
DEFAULT_CONFIG ||= RailsLocalAnalytics.config.instance_variables.map{|name| [name, RailsLocalAnalytics.config.instance_variable_get(name)] }.to_h
52+
53+
config.after(:each) do
54+
DEFAULT_CONFIG.each do |k,v|
55+
RailsLocalAnalytics.config.send("#{k}=", v)
56+
end
57+
end
58+
5159
end

0 commit comments

Comments
 (0)