Track which projects a user contributed to.

Closes #43460.
This commit is contained in:
Andreas Brandl 2018-02-23 20:33:16 +01:00
parent f88d5132c2
commit ebcf3c73bd
4 changed files with 63 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,13 @@
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

View File

@ -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

View File

@ -0,0 +1,37 @@
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