parent
f88d5132c2
commit
ebcf3c73bd
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue