summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-03-22 13:55:00 -0700
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-03-22 13:55:00 -0700
commit43afe46bbd19b1edf60abf3f104fb2b0d29af564 (patch)
treee2a11249408e3403a5a125c50008e77ba1157e57
parent20a12438ab470afe1a5736fc46091a6ef4c30073 (diff)
downloadgitlab-ce-43afe46bbd19b1edf60abf3f104fb2b0d29af564.tar.gz
Refactor contributions events and write tests for calendar
-rw-r--r--app/controllers/users_controller.rb10
-rw-r--r--app/models/event.rb6
-rw-r--r--app/models/user.rb7
-rw-r--r--app/views/users/_projects.html.haml2
-rw-r--r--features/steps/user.rb35
-rw-r--r--features/user.feature9
-rw-r--r--lib/gitlab/contributions_calendar.rb8
7 files changed, 59 insertions, 18 deletions
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index f39c820626f..f9b568b8af9 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -4,10 +4,7 @@ class UsersController < ApplicationController
layout :determine_layout
def show
- @contributed_projects = Project.
- where(id: authorized_projects_ids & @user.contributed_projects_ids).
- in_group_namespace.
- includes(:namespace).
+ @contributed_projects = contributed_projects.joined(@user).
reject(&:forked?)
@projects = @user.personal_projects.
@@ -76,11 +73,12 @@ class UsersController < ApplicationController
def contributed_projects
@contributed_projects = Project.
- where(id: authorized_projects_ids & @user.contributed_projects_ids).reject(&:forked?)
+ where(id: authorized_projects_ids & @user.contributed_projects_ids).
+ includes(:namespace)
end
def contributions_calendar
@contributions_calendar ||= Gitlab::ContributionsCalendar.
- new(contributed_projects, @user)
+ new(contributed_projects.reject(&:forked?), @user)
end
end
diff --git a/app/models/event.rb b/app/models/event.rb
index 2103a48a71b..57f6d5cd4e0 100644
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -55,6 +55,12 @@ class Event < ActiveRecord::Base
order('id DESC').limit(100).
update_all(updated_at: Time.now)
end
+
+ def contributions
+ where("action = ? OR (target_type in (?) AND action in (?))",
+ Event::PUSHED, ["MergeRequest", "Issue"],
+ [Event::CREATED, Event::CLOSED, Event::MERGED])
+ end
end
def proper?
diff --git a/app/models/user.rb b/app/models/user.rb
index ba325132df8..50f664a09a3 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -603,13 +603,10 @@ class User < ActiveRecord::Base
end
def contributed_projects_ids
- Event.where(author_id: self).
+ Event.contributions.where(author_id: self).
where("created_at > ?", Time.now - 1.year).
- where("action = :pushed OR (target_type = 'MergeRequest' AND action = :created)",
- pushed: Event::PUSHED, created: Event::CREATED).
reorder(project_id: :desc).
select(:project_id).
- uniq
- .map(&:project_id)
+ uniq.map(&:project_id)
end
end
diff --git a/app/views/users/_projects.html.haml b/app/views/users/_projects.html.haml
index 6c7779be30e..b7383d5594e 100644
--- a/app/views/users/_projects.html.haml
+++ b/app/views/users/_projects.html.haml
@@ -1,5 +1,5 @@
- if @contributed_projects.present?
- .panel.panel-default
+ .panel.panel-default.contributed-projects
.panel-heading Projects contributed to
= render 'shared/projects_list',
projects: @contributed_projects.sort_by(&:star_count).reverse,
diff --git a/features/steps/user.rb b/features/steps/user.rb
index d6f05ecb2c7..e6086bfc1c9 100644
--- a/features/steps/user.rb
+++ b/features/steps/user.rb
@@ -7,4 +7,39 @@ class Spinach::Features::User < Spinach::FeatureSteps
step 'I should see user "John Doe" page' do
expect(title).to match(/^\s*John Doe/)
end
+
+ step '"John Doe" has contributions' do
+ user = User.find_by(name: 'John Doe')
+ project = contributed_project
+
+ # Issue controbution
+ issue_params = { title: 'Bug in old browser' }
+ Issues::CreateService.new(project, user, issue_params).execute
+
+ # Push code contribution
+ push_params = {
+ project: project,
+ action: Event::PUSHED,
+ author_id: user.id,
+ data: { commit_count: 3 }
+ }
+
+ Event.create(push_params)
+ end
+
+ step 'I should see contributed projects' do
+ within '.contributed-projects' do
+ page.should have_content(@contributed_project.name)
+ end
+ end
+
+ step 'I should see contributions calendar' do
+ within '.calendar' do
+ page.should have_css('.graph-rect.r2.q2')
+ end
+ end
+
+ def contributed_project
+ @contributed_project ||= create(:project, :public)
+ end
end
diff --git a/features/user.feature b/features/user.feature
index a2167935fd2..69618e929c4 100644
--- a/features/user.feature
+++ b/features/user.feature
@@ -67,3 +67,12 @@ Feature: User
And I should see project "Enterprise"
And I should not see project "Internal"
And I should not see project "Community"
+
+ @javascript
+ Scenario: "John Doe" contribution profile
+ Given I sign in as a user
+ And "John Doe" has contributions
+ When I visit user "John Doe" page
+ Then I should see user "John Doe" page
+ And I should see contributed projects
+ And I should see contributions calendar
diff --git a/lib/gitlab/contributions_calendar.rb b/lib/gitlab/contributions_calendar.rb
index 8ca5115d43a..ea41644811f 100644
--- a/lib/gitlab/contributions_calendar.rb
+++ b/lib/gitlab/contributions_calendar.rb
@@ -14,7 +14,7 @@ module Gitlab
date_from = 1.year.ago
date_to = Date.today
- events = Event.where(author_id: user.id).where(action: event_type).
+ events = Event.contributions.where(author_id: user.id).
where("created_at > ?", date_from).where(project_id: projects)
grouped_events = events.to_a.group_by { |event| event.created_at.to_date.to_s }
@@ -41,7 +41,7 @@ module Gitlab
end
def events_by_date(date)
- events = Event.where(author_id: user.id).where(action: event_type).
+ events = Event.contributions.where(author_id: user.id).
where("created_at > ? AND created_at < ?", date.beginning_of_day, date.end_of_day).
where(project_id: projects)
@@ -57,9 +57,5 @@ module Gitlab
def starting_month
Date.today.strftime("%m").to_i
end
-
- def event_type
- [Event::PUSHED, Event::CREATED, Event::CLOSED, Event::MERGED]
- end
end
end