Commit ebcf3c73 authored by Andreas Brandl's avatar Andreas Brandl

Track which projects a user contributed to.

Closes #43460.
parent f88d5132
......@@ -65,6 +65,7 @@ class Event < ActiveRecord::Base
# Callbacks
after_create :reset_project_activity
after_create :set_last_repository_updated_at, if: :push?
after_create :track_user_contributed_projects
# Scopes
scope :recent, -> { reorder(id: :desc) }
......@@ -389,4 +390,8 @@ class Event < ActiveRecord::Base
Project.unscoped.where(id: project_id)
.update_all(last_repository_updated_at: created_at)
end
def track_user_contributed_projects
UserContributedProjects.track(self)
end
end
class UserContributedProjects < ActiveRecord::Base
belongs_to :user
belongs_to :project
validates :project, presence: true
validates :user, presence: true
def self.track(event)
find_or_create_by!(project: event.project, user: event.author)
rescue ActiveRecord::RecordNotUnique
retry
end
end
......@@ -49,6 +49,14 @@ describe Event do
end
end
end
describe 'after_create :track_user_contributed_projects' do
it 'passes event to UserContributedProjects.track' do
event = build(:push_event, project: project, author: project.owner)
expect(UserContributedProjects).to receive(:track).with(event)
event.save
end
end
end
describe "Push event" do
......
require 'spec_helper'
describe UserContributedProjects do
describe '.track' do
subject { described_class.track(event) }
let(:event) { build(:event) }
Event::ACTIONS.each do |action|
context "for all actions (event types)" do
let(:event) { build(:event, action: action) }
it 'creates a record' do
expect { subject }.to change { UserContributedProjects.count }.from(0).to(1)
end
end
end
it 'sets project accordingly' do
expect(subject.project).to eq(event.project)
end
it 'sets user accordingly' do
expect(subject.user).to eq(event.author)
end
it 'only creates a record once per user/project' do
expect do
subject
described_class.track(event)
end.to change { UserContributedProjects.count }.from(0).to(1)
end
end
it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_presence_of(:user) }
end
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