find_or_initialize_by.patch

Go MAEDA, 2022-03-06 03:38

Download (2.52 KB)

View differences:

app/models/member.rb
208 208
      project_ids = Array.wrap(attributes[:project_ids] || attributes[:project_id])
209 209
      role_ids = Array.wrap(attributes[:role_ids])
210 210
      project_ids.each do |project_id|
211
        member = Member.find_or_new(project_id, principal)
211
        member = Member.find_or_initialize_by(:project_id => project_id, :user_id => principal.id)
212 212
        member.role_ids |= role_ids
213 213
        member.save
214 214
        members << member
......
217 217
    members
218 218
  end
219 219

  
220
  # Finds or initializes a Member for the given project and principal
221
  def self.find_or_new(project, principal)
222
    project_id = project.is_a?(Project) ? project.id : project
223
    principal_id = principal.is_a?(Principal) ? principal.id : principal
224

  
225
    member = Member.find_by_project_id_and_user_id(project_id, principal_id)
226
    member ||= Member.new(:project_id => project_id, :user_id => principal_id)
227
    member
228
  end
229

  
230 220
  protected
231 221

  
232 222
  def validate_role
app/models/project.rb
1001 1001
  def add_inherited_member_roles
1002 1002
    if inherit_members? && parent
1003 1003
      parent.memberships.each do |parent_member|
1004
        member = Member.find_or_new(self.id, parent_member.user_id)
1004
        member = Member.find_or_initialize_by(:project_id => self.id, :user_id => parent_member.user_id)
1005 1005
        parent_member.member_roles.each do |parent_member_role|
1006 1006
          member.member_roles <<
1007 1007
            MemberRole.new(:role => parent_member_role.role,
test/integration/api_test/issues_test.rb
437 437
    issue = Issue.find(1)
438 438
    assert_equal 1, issue.tracker_id  # Bug
439 439
    issue.update(:status_id => 2)     # Assigned
440
    member = Member.find_or_new(issue.project, User.find_by_login('dlopper'))
440
    member = Member.find_by(:project => issue.project, :user => User.find_by(:login => 'dlopper'))
441 441
    assert_equal [2], member.role_ids # Developer
442 442

  
443 443
    get '/issues/1.xml?include=allowed_statuses', :headers => credentials('dlopper', 'foo')