Commit 87010983 authored by Andreas Brandl's avatar Andreas Brandl

Treat special cases accordingly.

* Event without project
* Fail early on unexpectedly missing author
parent d4d0740e
...@@ -8,7 +8,21 @@ class UserContributedProjects < ActiveRecord::Base ...@@ -8,7 +8,21 @@ class UserContributedProjects < ActiveRecord::Base
CACHE_EXPIRY_TIME = 1.day CACHE_EXPIRY_TIME = 1.day
def self.track(event) def self.track(event)
attributes = {project_id: event.project_id, user_id: event.author_id} # For events without a project, we simply don't care.
# An example of this is the creation of a snippet (which
# is not related to any project).
return unless event.project
# This is a precaution because the cache lookup
# will work just fine without an author.
#
# However, this should never happen (tm).
raise 'event#author not present unexpectedly' unless event.author
attributes = {
project_id: event.project_id,
user_id: event.author_id
}
cached_exists?(attributes) do cached_exists?(attributes) do
begin begin
......
...@@ -31,6 +31,14 @@ describe UserContributedProjects do ...@@ -31,6 +31,14 @@ describe UserContributedProjects do
described_class.track(event) described_class.track(event)
end.to change { UserContributedProjects.count }.from(0).to(1) end.to change { UserContributedProjects.count }.from(0).to(1)
end end
describe 'with an event without a project' do
let(:event) { build(:event, project: nil) }
it 'ignores the event' do
expect { subject }.not_to change { UserContributedProjects.count }
end
end
end end
it { is_expected.to validate_presence_of(:project) } it { is_expected.to validate_presence_of(:project) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment