diff options
author | Ciro Santilli <ciro.santilli@gmail.com> | 2014-07-14 17:17:59 +0400 |
---|---|---|
committer | Ciro Santilli <ciro.santilli@gmail.com> | 2014-07-18 00:47:16 +0400 |
commit | 9b5c7a4d6026265cfeaeac9ad5101f8826515dbe (patch) | |
tree | 18d795d222563b9c672aff1121b11022a32016b7 | |
parent | ecd771ccdfe54b08ea7f80f64667228b7938238d (diff) |
Add star count, view logic, more tests.
-rw-r--r-- | app/controllers/projects_controller.rb | 3 | ||||
-rw-r--r-- | app/models/project.rb | 6 | ||||
-rw-r--r-- | app/models/user.rb | 3 | ||||
-rw-r--r-- | app/models/users_star_project.rb | 2 | ||||
-rw-r--r-- | app/views/projects/_home_panel.html.haml | 2 | ||||
-rw-r--r-- | db/migrate/20140625115202_create_users_star_projects.rb | 4 | ||||
-rw-r--r-- | db/schema.rb | 2 | ||||
-rw-r--r-- | spec/controllers/projects_controller_spec.rb | 11 | ||||
-rw-r--r-- | spec/models/project_spec.rb | 52 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 27 |
10 files changed, 95 insertions, 17 deletions
diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index cf7209b05e6..9c9677a22c8 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -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 diff --git a/app/models/project.rb b/app/models/project.rb index fdd7840aac6..10882ea97cc 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index f5aab99c6a9..39d2d9cce7f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/models/users_star_project.rb b/app/models/users_star_project.rb index 1f5ae37b02b..80e756bd00c 100644 --- a/app/models/users_star_project.rb +++ b/app/models/users_star_project.rb @@ -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 diff --git a/app/views/projects/_home_panel.html.haml b/app/views/projects/_home_panel.html.haml index 48f5a90cf23..419b50a1141 100644 --- a/app/views/projects/_home_panel.html.haml +++ b/app/views/projects/_home_panel.html.haml @@ -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.', diff --git a/db/migrate/20140625115202_create_users_star_projects.rb b/db/migrate/20140625115202_create_users_star_projects.rb index 70475535d54..412f0f6f34b 100644 --- a/db/migrate/20140625115202_create_users_star_projects.rb +++ b/db/migrate/20140625115202_create_users_star_projects.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index fd0f15a7fa6..e4dcdf5bea2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/controllers/projects_controller_spec.rb b/spec/controllers/projects_controller_spec.rb index c7a48898c56..71bc49787cc 100644 --- a/spec/controllers/projects_controller_spec.rb +++ b/spec/controllers/projects_controller_spec.rb @@ -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 diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index c2ebfbd9229..72673a1f812 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index b6ffb6ac24b..ef6b8a94502 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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 |