summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/event.rb5
-rw-r--r--app/models/user_contributed_projects.rb13
-rw-r--r--spec/models/event_spec.rb8
-rw-r--r--spec/models/user_contributed_projects_spec.rb37
4 files changed, 63 insertions, 0 deletions
diff --git a/app/models/event.rb b/app/models/event.rb
index 75538ba196c..f0cc99a9242 100644
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -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
diff --git a/app/models/user_contributed_projects.rb b/app/models/user_contributed_projects.rb
new file mode 100644
index 00000000000..846d77f3c69
--- /dev/null
+++ b/app/models/user_contributed_projects.rb
@@ -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
diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb
index 67f49348acb..11f6ae74712 100644
--- a/spec/models/event_spec.rb
+++ b/spec/models/event_spec.rb
@@ -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
diff --git a/spec/models/user_contributed_projects_spec.rb b/spec/models/user_contributed_projects_spec.rb
new file mode 100644
index 00000000000..5b8c61750b3
--- /dev/null
+++ b/spec/models/user_contributed_projects_spec.rb
@@ -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