From 2d6f552599d0e632953372b7a942a5eaab099631 Mon Sep 17 00:00:00 2001 From: Go MAEDA Date: Mon, 14 Mar 2022 14:25:24 +0000 Subject: [PATCH] Replace Member.find_or_new with ActiveRecord's find_or_initialize_by (#36730). git-svn-id: http://svn.redmine.org/redmine/trunk@21456 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/member.rb | 12 +----------- app/models/project.rb | 2 +- test/integration/api_test/issues_test.rb | 2 +- 3 files changed, 3 insertions(+), 13 deletions(-) diff --git a/app/models/member.rb b/app/models/member.rb index 784c98098..5a0692a5d 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -208,7 +208,7 @@ class Member < ActiveRecord::Base project_ids = Array.wrap(attributes[:project_ids] || attributes[:project_id]) role_ids = Array.wrap(attributes[:role_ids]) project_ids.each do |project_id| - member = Member.find_or_new(project_id, principal) + member = Member.find_or_initialize_by(:project_id => project_id, :user_id => principal.id) member.role_ids |= role_ids member.save members << member @@ -217,16 +217,6 @@ class Member < ActiveRecord::Base members end - # Finds or initializes a Member for the given project and principal - def self.find_or_new(project, principal) - project_id = project.is_a?(Project) ? project.id : project - principal_id = principal.is_a?(Principal) ? principal.id : principal - - member = Member.find_by_project_id_and_user_id(project_id, principal_id) - member ||= Member.new(:project_id => project_id, :user_id => principal_id) - member - end - protected def validate_role diff --git a/app/models/project.rb b/app/models/project.rb index 4e15b19c8..90ef0e2e9 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1001,7 +1001,7 @@ class Project < ActiveRecord::Base def add_inherited_member_roles if inherit_members? && parent parent.memberships.each do |parent_member| - member = Member.find_or_new(self.id, parent_member.user_id) + member = Member.find_or_initialize_by(:project_id => self.id, :user_id => parent_member.user_id) parent_member.member_roles.each do |parent_member_role| member.member_roles << MemberRole.new(:role => parent_member_role.role, diff --git a/test/integration/api_test/issues_test.rb b/test/integration/api_test/issues_test.rb index 64ac2d4c8..29b3390d2 100644 --- a/test/integration/api_test/issues_test.rb +++ b/test/integration/api_test/issues_test.rb @@ -437,7 +437,7 @@ class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base issue = Issue.find(1) assert_equal 1, issue.tracker_id # Bug issue.update(:status_id => 2) # Assigned - member = Member.find_or_new(issue.project, User.find_by_login('dlopper')) + member = Member.find_by(:project => issue.project, :user => User.find_by(:login => 'dlopper')) assert_equal [2], member.role_ids # Developer get '/issues/1.xml?include=allowed_statuses', :headers => credentials('dlopper', 'foo') -- 2.39.5