Class: Member

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/member.rb

Overview

Redmine - project management software Copyright (C) 2006-2014 Jean-Philippe Lang

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_principal_memberships(principal, attributes) ⇒ Object

Creates memberships for principal with the attributes

  • project_ids : one or more project ids

  • role_ids : ids of the roles to give to each membership

Example:

Member.create_principal_memberships(user, :project_ids => [2, 5], :role_ids => [1, 3]

111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/member.rb', line 111

def self.create_principal_memberships(principal, attributes)
  members = []
  if attributes
    project_ids = Array.wrap(attributes[:project_ids] || attributes[:project_id])
    role_ids = attributes[:role_ids]
    project_ids.each do |project_id|
      members << Member.new(:principal => principal, :role_ids => role_ids, :project_id => project_id)
    end
    principal.members << members
  end
  members
end

.find_or_new(project, principal) ⇒ Object

Finds or initilizes a Member for the given project and principal


125
126
127
128
129
130
131
132
# File 'app/models/member.rb', line 125

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

Instance Method Details

#<=>(member) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/member.rb', line 58

def <=>(member)
  a, b = roles.sort.first, member.roles.sort.first
  if a == b
    if principal
      principal <=> member.principal
    else
      1
    end
  elsif a
    a <=> b
  else
    1
  end
end

#base_role_ids=Object


42
# File 'app/models/member.rb', line 42

alias :base_role_ids= :role_ids=

#deletable?Boolean

Returns:

  • (Boolean)

73
74
75
# File 'app/models/member.rb', line 73

def deletable?
  member_roles.detect {|mr| mr.inherited_from}.nil?
end

#destroyObject


77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/member.rb', line 77

def destroy
  if member_roles.reload.present?
    # destroying the last role will destroy another instance
    # of the same Member record, #super would then trigger callbacks twice
    member_roles.destroy_all
    @destroyed = true
    freeze
  else
    super
  end
end

#include?(user) ⇒ Boolean

Returns:

  • (Boolean)

89
90
91
92
93
94
95
# File 'app/models/member.rb', line 89

def include?(user)
  if principal.is_a?(Group)
    !user.nil? && user.groups.include?(principal)
  else
    self.user == user
  end
end

#nameObject


38
39
40
# File 'app/models/member.rb', line 38

def name
  self.user.name
end

#roleObject


32
33
# File 'app/models/member.rb', line 32

def role
end

#role=Object


35
36
# File 'app/models/member.rb', line 35

def role=
end

#role_ids=(arg) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/member.rb', line 43

def role_ids=(arg)
  ids = (arg || []).collect(&:to_i) - [0]
  # Keep inherited roles
  ids += member_roles.select {|mr| !mr.inherited_from.nil?}.collect(&:role_id)

  new_role_ids = ids - role_ids
  # Add new roles
  new_role_ids.each {|id| member_roles << MemberRole.new(:role_id => id, :member => self) }
  # Remove roles (Rails' #role_ids= will not trigger MemberRole#on_destroy)
  member_roles_to_destroy = member_roles.select {|mr| !ids.include?(mr.role_id)}
  if member_roles_to_destroy.any?
    member_roles_to_destroy.each(&:destroy)
  end
end

#set_issue_category_nilObject


97
98
99
100
101
102
103
# File 'app/models/member.rb', line 97

def set_issue_category_nil
  if user_id && project_id
    # remove category based auto assignments for this member
    IssueCategory.where(["project_id = ? AND assigned_to_id = ?", project_id, user_id]).
      update_all("assigned_to_id = NULL")
  end
end