Project

General

Profile

Patch #42124 ยป refactor-member-role_ids.patch

Go MAEDA, 2025-01-19 10:18

View differences:

app/models/member.rb
64 64
  def role_ids=(arg)
65 65
    ids = (arg || []).collect(&:to_i) - [0]
66 66
    # Keep inherited roles
67 67
    ids |= member_roles.select {|mr| !mr.inherited_from.nil?}.collect(&:role_id)
68 68

  
69 69
    new_role_ids = ids - role_ids
70
    obsolete_role_ids = role_ids - ids
70 71
    # Add new roles
71 72
    new_role_ids.each do |id|
72 73
      member_roles << MemberRole.new(:role_id => id, :member => self)
73 74
    end
74 75
    # Remove roles (Rails' #role_ids= will not trigger MemberRole#on_destroy)
75
    member_roles_to_destroy = member_roles.select {|mr| !ids.include?(mr.role_id)}
76
    if member_roles_to_destroy.any?
77
      member_roles_to_destroy.each(&:destroy)
78
    end
76
    member_roles.where(role_id: obsolete_role_ids).destroy_all
79 77
    member_roles.reload
80 78
    super(ids)
81 79
  end
82 80

  
83 81
  def <=>(member)
84 82
    return nil unless member.is_a?(Member)
    (1-1/1)