From 7b7e0032ed94f6671389d899e5175d6ec9128df8 Mon Sep 17 00:00:00 2001 From: Long Nguyen Date: Sat, 14 May 2016 15:40:32 +0700 Subject: Only show todos of projects that user is a member of --- app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 489bff3fa4a..0b1f5f99ccb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -77,7 +77,7 @@ class User < ActiveRecord::Base has_one :abuse_report, dependent: :destroy has_many :spam_logs, dependent: :destroy has_many :builds, dependent: :nullify, class_name: 'Ci::Build' - has_many :todos, dependent: :destroy + has_many :todos, -> { joins("join members on members.source_id = todos.project_id and members.user_id = todos.user_id") }, dependent: :destroy has_many :notification_settings, dependent: :destroy # -- cgit v1.2.1 From ad17741008b5ec874f92016ed48c2c1e6638dab5 Mon Sep 17 00:00:00 2001 From: Long Nguyen Date: Sat, 21 May 2016 00:37:48 +0700 Subject: Remove todos when destroy project member and specs --- app/models/members/project_member.rb | 4 ++- app/models/user.rb | 2 +- spec/models/members/project_member_spec.rb | 43 ++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/app/models/members/project_member.rb b/app/models/members/project_member.rb index 8dae3bb2ef2..0ced9c31581 100644 --- a/app/models/members/project_member.rb +++ b/app/models/members/project_member.rb @@ -3,9 +3,9 @@ class ProjectMember < Member include Gitlab::ShellAdapter + has_many :todos, through: :user belongs_to :project, class_name: 'Project', foreign_key: 'source_id' - # Make sure project member points only to project as it source default_value_for :source_type, SOURCE_TYPE validates_format_of :source_type, with: /\AProject\z/ @@ -15,6 +15,8 @@ class ProjectMember < Member scope :in_projects, ->(projects) { where(source_id: projects.pluck(:id)) } scope :with_user, ->(user) { where(user_id: user.id) } + before_destroy { todos.each(&:destroy) } + class << self # Add users to project teams with passed access option diff --git a/app/models/user.rb b/app/models/user.rb index 70a966491eb..6a09b78455b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -77,7 +77,7 @@ class User < ActiveRecord::Base has_one :abuse_report, dependent: :destroy has_many :spam_logs, dependent: :destroy has_many :builds, dependent: :nullify, class_name: 'Ci::Build' - has_many :todos, -> { joins("join members on members.source_id = todos.project_id and members.user_id = todos.user_id") }, dependent: :destroy + has_many :todos, dependent: :destroy has_many :notification_settings, dependent: :destroy # diff --git a/spec/models/members/project_member_spec.rb b/spec/models/members/project_member_spec.rb index 9f26d9eb5ce..2d2fa91e001 100644 --- a/spec/models/members/project_member_spec.rb +++ b/spec/models/members/project_member_spec.rb @@ -20,6 +20,49 @@ require 'spec_helper' describe ProjectMember, models: true do + describe 'associations' do + it { is_expected.to have_many(:todos).through(:user) } + it { is_expected.to belong_to(:project).class_name('Project').with_foreign_key(:source_id) } + end + + describe 'validations' do + it { is_expected.to allow_value('Project').for(:source_type) } + it { is_expected.not_to allow_value('project').for(:source_type) } + end + + describe 'modules' do + it { is_expected.to include_module(Gitlab::ShellAdapter) } + end + + describe "#destroy" do + let(:owner) { create(:project_member, access_level: ProjectMember::OWNER) } + let(:project) { owner.project } + let(:master) { create(:project_member, project: project) } + + let(:owner_todos) { (0...2).map { create(:todo, user: owner.user, project: project) } } + let(:master_todos) { (0...3).map { create(:todo, user: master.user, project: project) } } + + before do + owner_todos + master_todos + end + + it "destroy itself and delete associated todos" do + expect(owner.todos.size).to eq(2) + expect(master.todos.size).to eq(3) + expect(Todo.count).to eq(5) + + master_todo_ids = master_todos.map(&:id) + master.destroy + + expect(owner.todos.size).to eq(2) + expect(Todo.count).to eq(2) + master_todo_ids.each do |id| + expect(Todo.exists?(id)).to eq(false) + end + end + end + describe :import_team do before do @abilities = Six.new -- cgit v1.2.1 From bfbbb182c14441ad176b6c7b0ec9e9f38da25ae4 Mon Sep 17 00:00:00 2001 From: Long Nguyen Date: Sat, 21 May 2016 17:20:44 +0700 Subject: Fix broken specs --- app/models/members/project_member.rb | 3 +-- spec/models/members/project_member_spec.rb | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/app/models/members/project_member.rb b/app/models/members/project_member.rb index 0ced9c31581..fa04963a1b7 100644 --- a/app/models/members/project_member.rb +++ b/app/models/members/project_member.rb @@ -3,7 +3,6 @@ class ProjectMember < Member include Gitlab::ShellAdapter - has_many :todos, through: :user belongs_to :project, class_name: 'Project', foreign_key: 'source_id' # Make sure project member points only to project as it source @@ -15,7 +14,7 @@ class ProjectMember < Member scope :in_projects, ->(projects) { where(source_id: projects.pluck(:id)) } scope :with_user, ->(user) { where(user_id: user.id) } - before_destroy { todos.each(&:destroy) } + before_destroy { user.todos.where(project_id: source_id).each(&:destroy) } class << self diff --git a/spec/models/members/project_member_spec.rb b/spec/models/members/project_member_spec.rb index 2d2fa91e001..46e98c8807b 100644 --- a/spec/models/members/project_member_spec.rb +++ b/spec/models/members/project_member_spec.rb @@ -21,7 +21,6 @@ require 'spec_helper' describe ProjectMember, models: true do describe 'associations' do - it { is_expected.to have_many(:todos).through(:user) } it { is_expected.to belong_to(:project).class_name('Project').with_foreign_key(:source_id) } end -- cgit v1.2.1 From e55ba527b8a85da0fa4b61d9d17128f459acbfec Mon Sep 17 00:00:00 2001 From: Long Nguyen Date: Sat, 21 May 2016 23:11:26 +0700 Subject: Fix broken specs --- spec/models/members/project_member_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/models/members/project_member_spec.rb b/spec/models/members/project_member_spec.rb index 46e98c8807b..9f13874b532 100644 --- a/spec/models/members/project_member_spec.rb +++ b/spec/models/members/project_member_spec.rb @@ -47,14 +47,14 @@ describe ProjectMember, models: true do end it "destroy itself and delete associated todos" do - expect(owner.todos.size).to eq(2) - expect(master.todos.size).to eq(3) + expect(owner.user.todos.size).to eq(2) + expect(master.user.todos.size).to eq(3) expect(Todo.count).to eq(5) master_todo_ids = master_todos.map(&:id) master.destroy - expect(owner.todos.size).to eq(2) + expect(owner.user.todos.size).to eq(2) expect(Todo.count).to eq(2) master_todo_ids.each do |id| expect(Todo.exists?(id)).to eq(false) -- cgit v1.2.1 From 5af73808048a03a3a5e54f08bad1aeb1c74156cf Mon Sep 17 00:00:00 2001 From: Long Nguyen Date: Sun, 22 May 2016 09:32:59 +0700 Subject: Fix Member spec --- app/models/members/project_member.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/members/project_member.rb b/app/models/members/project_member.rb index fa04963a1b7..5c167db76b1 100644 --- a/app/models/members/project_member.rb +++ b/app/models/members/project_member.rb @@ -14,7 +14,7 @@ class ProjectMember < Member scope :in_projects, ->(projects) { where(source_id: projects.pluck(:id)) } scope :with_user, ->(user) { where(user_id: user.id) } - before_destroy { user.todos.where(project_id: source_id).each(&:destroy) } + before_destroy { user.todos.where(project_id: source_id).each(&:destroy) if user } class << self -- cgit v1.2.1 From aeb5262d427e1b61ded71cc4a20e8e2a7a0089ef Mon Sep 17 00:00:00 2001 From: Long Nguyen Date: Mon, 30 May 2016 22:32:07 +0700 Subject: Add changelog and code refactor --- CHANGELOG | 1 + app/models/members/project_member.rb | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 3e8b0a2af01..91038532e99 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ Please view this file on the master branch, on stable branches it's out of date. v 8.9.0 (unreleased) + - Fix issue todo not remove when leave project !4150 (Long Nguyen) - Allow forking projects with restricted visibility level - Improve note validation to prevent errors when creating invalid note via API - Redesign navigation for project pages diff --git a/app/models/members/project_member.rb b/app/models/members/project_member.rb index 5c167db76b1..46955b430f3 100644 --- a/app/models/members/project_member.rb +++ b/app/models/members/project_member.rb @@ -14,7 +14,7 @@ class ProjectMember < Member scope :in_projects, ->(projects) { where(source_id: projects.pluck(:id)) } scope :with_user, ->(user) { where(user_id: user.id) } - before_destroy { user.todos.where(project_id: source_id).each(&:destroy) if user } + before_destroy :delete_member_todos class << self @@ -103,6 +103,10 @@ class ProjectMember < Member private + def delete_member_todos + user.todos.where(project_id: source_id).destroy_all if user + end + def send_invite notification_service.invite_project_member(self, @raw_invite_token) -- cgit v1.2.1