BigW Consortium Gitlab

group.rb 1.15 KB
Newer Older
1 2
# == Schema Information
#
Dmitriy Zaporozhets committed
3
# Table name: namespaces
4
#
Andrew8xx8 committed
5 6 7
#  id          :integer          not null, primary key
#  name        :string(255)      not null
#  path        :string(255)      not null
Dmitriy Zaporozhets committed
8
#  owner_id    :integer
Andrew8xx8 committed
9 10 11
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#  type        :string(255)
Dmitriy Zaporozhets committed
12
#  description :string(255)      default(""), not null
13 14
#

15
class Group < Namespace
16 17
  has_many :users_groups, dependent: :destroy
  has_many :users, through: :users_groups
Andrey Kumanyaev committed
18

19 20 21
  def human_name
    name
  end
22

23
  def owners
24
    @owners ||= users_groups.owners.map(&:user)
25
  end
26 27 28

  def add_users(user_ids, group_access)
    user_ids.compact.each do |user_id|
29 30
      user = self.users_groups.find_or_initialize_by_user_id(user_id)
      user.update_attributes(group_access: group_access)
31 32 33
    end
  end

34 35 36 37
  def add_user(user, group_access)
    self.users_groups.create(user_id: user.id, group_access: group_access)
  end

38 39 40
  def add_owner(user)
    self.add_user(user, UsersGroup::OWNER)
  end
41

42 43
  def has_owner?(user)
    owners.include?(user)
44 45
  end

46 47 48
  def last_owner?(user)
    has_owner?(user) && owners.size == 1
  end
49

50 51
  def members
    users_groups
52
  end
53
end