Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tatuya Kamada
gitlab-ce
Commits
9b5c7a4d
Commit
9b5c7a4d
authored
Jul 14, 2014
by
Ciro Santilli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add star count, view logic, more tests.
parent
ecd771cc
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
95 additions
and
17 deletions
+95
-17
app/controllers/projects_controller.rb
app/controllers/projects_controller.rb
+3
-0
app/models/project.rb
app/models/project.rb
+2
-4
app/models/user.rb
app/models/user.rb
+2
-1
app/models/users_star_project.rb
app/models/users_star_project.rb
+1
-1
app/views/projects/_home_panel.html.haml
app/views/projects/_home_panel.html.haml
+1
-1
db/migrate/20140625115202_create_users_star_projects.rb
db/migrate/20140625115202_create_users_star_projects.rb
+3
-1
db/schema.rb
db/schema.rb
+2
-0
spec/controllers/projects_controller_spec.rb
spec/controllers/projects_controller_spec.rb
+8
-3
spec/models/project_spec.rb
spec/models/project_spec.rb
+46
-6
spec/models/user_spec.rb
spec/models/user_spec.rb
+27
-0
No files found.
app/controllers/projects_controller.rb
View file @
9b5c7a4d
...
...
@@ -60,6 +60,8 @@ class ProjectsController < ApplicationController
@events
=
event_filter
.
apply_filter
(
@events
)
@events
=
@events
.
limit
(
limit
).
offset
(
params
[
:offset
]
||
0
)
@show_star
=
!
(
current_user
and
current_user
.
starred?
(
@project
))
respond_to
do
|
format
|
format
.
html
do
if
@project
.
empty_repo?
...
...
@@ -169,6 +171,7 @@ class ProjectsController < ApplicationController
def
toggle_star
current_user
.
toggle_star
(
@project
)
@project
.
reload
render
json:
{
star_count:
@project
.
star_count
}
end
...
...
app/models/project.rb
View file @
9b5c7a4d
...
...
@@ -22,6 +22,7 @@
# visibility_level :integer default(0), not null
# archived :boolean default(FALSE), not null
# import_status :string(255)
# star_count :integer
#
class
Project
<
ActiveRecord
::
Base
...
...
@@ -109,6 +110,7 @@ class Project < ActiveRecord::Base
validates
:import_url
,
format:
{
with:
URI
::
regexp
(
%w(git http https)
),
message:
"should be a valid url"
},
if: :import?
validates
:star_count
,
numericality:
{
greater_than_or_equal_to:
0
}
validate
:check_limit
,
on: :create
# Scopes
...
...
@@ -577,8 +579,4 @@ class Project < ActiveRecord::Base
def
update_repository_size
update_attribute
(
:repository_size
,
repository
.
size
)
end
def
star_count
starrers
.
count
end
end
app/models/user.rb
View file @
9b5c7a4d
...
...
@@ -516,7 +516,8 @@ class User < ActiveRecord::Base
end
def
toggle_star
(
project
)
user_star_project
=
users_star_projects
.
where
(
project:
project
).
take
user_star_project
=
users_star_projects
.
where
(
project:
project
,
user:
self
).
take
if
user_star_project
user_star_project
.
destroy
else
...
...
app/models/users_star_project.rb
View file @
9b5c7a4d
...
...
@@ -10,7 +10,7 @@
#
class
UsersStarProject
<
ActiveRecord
::
Base
belongs_to
:project
belongs_to
:project
,
counter_cache: :star_count
belongs_to
:user
validates
:user
,
presence:
true
...
...
app/views/projects/_home_panel.html.haml
View file @
9b5c7a4d
...
...
@@ -34,7 +34,7 @@
=
link_to
pluralize
(
number_with_delimiter
(
@repository
.
branch_names
.
count
),
'branch'
),
project_branches_path
(
@project
)
=
link_to
pluralize
(
number_with_delimiter
(
@repository
.
tag_names
.
count
),
'tag'
),
project_tags_path
(
@project
)
%span
.light.prepend-left-20
=
repository_size
%span
.star.js-toggler-container
.on
%span
.star.js-toggler-container
{
class:
@show_star
?
'on'
:
''
}
-
if
current_user
=
render
'link_to_toggle_star'
,
title:
'Star this project.'
,
...
...
db/migrate/20140625115202_create_users_star_projects.rb
View file @
9b5c7a4d
...
...
@@ -5,9 +5,11 @@ class CreateUsersStarProjects < ActiveRecord::Migration
t
.
integer
:user_id
,
null:
false
t
.
timestamps
end
add_index
:users_star_projects
,
:user_id
add_index
:users_star_projects
,
:project_id
add_index
:users_star_projects
,
[
:user_id
,
:project_id
],
unique:
true
add_column
:projects
,
:star_count
,
:integer
,
default:
0
,
null:
false
add_index
:projects
,
:star_count
,
using: :btree
end
end
db/schema.rb
View file @
9b5c7a4d
...
...
@@ -224,11 +224,13 @@ ActiveRecord::Schema.define(version: 20140625115202) do
t
.
boolean
"archived"
,
default:
false
,
null:
false
t
.
string
"import_status"
t
.
float
"repository_size"
,
default:
0.0
t
.
integer
"star_count"
,
default:
0
,
null:
false
end
add_index
"projects"
,
[
"creator_id"
],
name:
"index_projects_on_creator_id"
,
using: :btree
add_index
"projects"
,
[
"last_activity_at"
],
name:
"index_projects_on_last_activity_at"
,
using: :btree
add_index
"projects"
,
[
"namespace_id"
],
name:
"index_projects_on_namespace_id"
,
using: :btree
add_index
"projects"
,
[
"star_count"
],
name:
"index_projects_on_star_count"
,
using: :btree
create_table
"protected_branches"
,
force:
true
do
|
t
|
t
.
integer
"project_id"
,
null:
false
...
...
spec/controllers/projects_controller_spec.rb
View file @
9b5c7a4d
...
...
@@ -43,15 +43,20 @@ describe ProjectsController do
end
describe
"POST #toggle_star"
do
it
"
increases star count
if user is signed in"
do
it
"
toggles star
if user is signed in"
do
sign_in
(
user
)
expect
(
user
.
starred?
(
public_project
)).
to
be_false
post
:toggle_star
,
id:
public_project
.
to_param
expect
(
public_project
.
star_count
).
to
eq
(
1
)
expect
(
user
.
starred?
(
public_project
)).
to
be_true
post
:toggle_star
,
id:
public_project
.
to_param
expect
(
user
.
starred?
(
public_project
)).
to
be_false
end
it
"does nothing if user is not signed in"
do
post
:toggle_star
,
id:
public_project
.
to_param
expect
(
public_project
.
star_count
).
to
eq
(
0
)
expect
(
user
.
starred?
(
public_project
)).
to
be_false
post
:toggle_star
,
id:
public_project
.
to_param
expect
(
user
.
starred?
(
public_project
)).
to
be_false
end
end
end
spec/models/project_spec.rb
View file @
9b5c7a4d
...
...
@@ -241,21 +241,61 @@ describe Project do
it
{
project
.
open_branches
.
map
(
&
:name
).
should_not
include
(
'master'
)
}
end
describe
"#
count_star
"
do
it
"counts stars"
do
describe
"#
star_count
"
do
it
"counts stars
from multiple users
"
do
user1
=
create
:user
user2
=
create
:user
project
=
create
:project
,
:public
expect
(
project
.
star_count
).
to
eq
(
0
)
user1
.
toggle_star
(
project
)
expect
(
project
.
star_count
).
to
eq
(
1
)
expect
(
project
.
reload
.
star_count
).
to
eq
(
1
)
user2
.
toggle_star
(
project
)
expect
(
project
.
star_count
).
to
eq
(
2
)
project
.
reload
expect
(
project
.
reload
.
star_count
).
to
eq
(
2
)
user1
.
toggle_star
(
project
)
expect
(
project
.
star_count
).
to
eq
(
1
)
project
.
reload
expect
(
project
.
reload
.
star_count
).
to
eq
(
1
)
user2
.
toggle_star
(
project
)
expect
(
project
.
star_count
).
to
eq
(
0
)
project
.
reload
expect
(
project
.
reload
.
star_count
).
to
eq
(
0
)
end
it
"counts stars on the right project"
do
user
=
create
:user
project1
=
create
:project
,
:public
project2
=
create
:project
,
:public
expect
(
project1
.
star_count
).
to
eq
(
0
)
expect
(
project2
.
star_count
).
to
eq
(
0
)
user
.
toggle_star
(
project1
)
project1
.
reload
project2
.
reload
expect
(
project1
.
star_count
).
to
eq
(
1
)
expect
(
project2
.
star_count
).
to
eq
(
0
)
user
.
toggle_star
(
project1
)
project1
.
reload
project2
.
reload
expect
(
project1
.
star_count
).
to
eq
(
0
)
expect
(
project2
.
star_count
).
to
eq
(
0
)
user
.
toggle_star
(
project2
)
project1
.
reload
project2
.
reload
expect
(
project1
.
star_count
).
to
eq
(
0
)
expect
(
project2
.
star_count
).
to
eq
(
1
)
user
.
toggle_star
(
project2
)
project1
.
reload
project2
.
reload
expect
(
project1
.
star_count
).
to
eq
(
0
)
expect
(
project2
.
star_count
).
to
eq
(
0
)
end
end
end
spec/models/user_spec.rb
View file @
9b5c7a4d
...
...
@@ -356,6 +356,33 @@ describe User do
end
end
describe
"#starred?"
do
it
"determines if user starred a project"
do
user
=
create
:user
project1
=
create
:project
,
:public
project2
=
create
:project
,
:public
expect
(
user
.
starred?
(
project1
)).
to
be_false
expect
(
user
.
starred?
(
project2
)).
to
be_false
star1
=
UsersStarProject
.
create!
(
project:
project1
,
user:
user
)
expect
(
user
.
starred?
(
project1
)).
to
be_true
expect
(
user
.
starred?
(
project2
)).
to
be_false
star2
=
UsersStarProject
.
create!
(
project:
project2
,
user:
user
)
expect
(
user
.
starred?
(
project1
)).
to
be_true
expect
(
user
.
starred?
(
project2
)).
to
be_true
star1
.
destroy
expect
(
user
.
starred?
(
project1
)).
to
be_false
expect
(
user
.
starred?
(
project2
)).
to
be_true
star2
.
destroy
expect
(
user
.
starred?
(
project1
)).
to
be_false
expect
(
user
.
starred?
(
project2
)).
to
be_false
end
end
describe
"#toggle_star"
do
it
"toggles stars"
do
user
=
create
:user
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment