Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

### v0.2.3 - Dec 17 2024
- [View Diff](https://github.com/westonganger/rails_local_analytics/compare/v0.2.2...v0.2.3)
Expand Down
11 changes: 9 additions & 2 deletions lib/rails_local_analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.record_request(request:, custom_attributes: nil)
json_str = JSON.generate(json_hash) # convert to json string so that its compatible with all job backends
RecordRequestJob.perform_later(json_str)
else
RecordRequestJob.new.perform(json_hash)
RecordRequestJob.new.perform(json_hash.deep_stringify_keys)
end
end

Expand All @@ -44,12 +44,19 @@ def self.config(&block)
end

class Config
@@background_jobs = true
DEFAULTS = {
background_jobs: true,
}.freeze

mattr_reader :background_jobs

def self.background_jobs=(val)
@@background_jobs = !!val
end

DEFAULTS.each do |k,v|
self.send("#{k}=", v)
end
end

end
8 changes: 0 additions & 8 deletions spec/model/rails_local_analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@
end

context "config.background_jobs" do
before do
@prev_background_jobs = described_class.config.background_jobs
end

after do
described_class.config.background_jobs = @prev_background_jobs
end

it "defaults to true" do
expect(described_class.config.background_jobs).to eq(true)
end
Expand Down
24 changes: 24 additions & 0 deletions spec/request/record_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,28 @@
"url_path" => "/",
})
end

it "works with config.background_jobs = false" do
RailsLocalAnalytics.config.background_jobs = false

get root_path
expect(response.status).to eq(200)

expect(TrackedRequestsByDaySite.last.attributes.except("id")).to eq({
"browser_engine" => nil,
"day" => Date.today,
"platform" => nil,
"total" => 1,
"url_hostname" => "www.example.com",
})

expect(TrackedRequestsByDayPage.last.attributes.except("id")).to eq({
"day" => Date.today,
"referrer_hostname" => nil,
"referrer_path" => nil,
"total" => 1,
"url_hostname" => "www.example.com",
"url_path" => "/",
})
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
DatabaseCleaner.cleaning do
example.run
end

RailsLocalAnalytics::Config::DEFAULTS.each do |k,v|
RailsLocalAnalytics.config.send("#{k}=", v)
end
end

require 'rails-controller-testing'
Expand Down
Loading