Class: Member

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

Overview

– copyright ChiliProject is a project management system.

Copyright (C) 2010-2013 the ChiliProject Team

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.

See doc/COPYRIGHT.rdoc for more details. ++

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.edit_membership(id, new_attributes, principal = nil) ⇒ Object

Find or initilize a Member with an id, attributes, and for a Principal


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

def self.edit_membership(id, new_attributes, principal=nil)
  @membership = id.present? ? Member.find(id) : Member.new(:principal => principal)
  @membership.attributes = new_attributes
  @membership
end

Instance Method Details

#<=>(member) ⇒ Object


48
49
50
51
# File 'app/models/member.rb', line 48

def <=>(member)
  a, b = roles.sort.first, member.roles.sort.first
  a == b ? (principal <=> member.principal) : (a <=> b)
end

#base_role_ids=Object


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

alias :base_role_ids= :role_ids=

#before_destroyObject


65
66
67
68
69
70
# File 'app/models/member.rb', line 65

def before_destroy
  if user
    # remove category based auto assignments for this member
    IssueCategory.update_all "assigned_to_id = NULL", ["project_id = ? AND assigned_to_id = ?", project.id, user.id]
  end
end

#deletable?Boolean

Returns:

  • (Boolean)

53
54
55
# File 'app/models/member.rb', line 53

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

#include?(user) ⇒ Boolean

Returns:

  • (Boolean)

57
58
59
60
61
62
63
# File 'app/models/member.rb', line 57

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

#nameObject


27
28
29
# File 'app/models/member.rb', line 27

def name
  self.user.name
end

#role_ids=(arg) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/member.rb', line 32

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) }
  # 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)
    unwatch_from_permission_change
  end
end