Project

General

Profile

Download (1.95 KB) Statistics
| Branch: | Tag: | Revision:
9c0e127b Paul Kelly
class Usergroup < ActiveRecord::Base
9fd7478e Paul Kelly
include Authorization
229d7436 Joseph Magen
audited :allow_mass_assignment => true
90ed6bdf Joseph Mitchell Magen
has_many :usergroup_members, :dependent => :destroy
has_many :users, :through => :usergroup_members, :source => :member, :source_type => 'User'
has_many :usergroups, :through => :usergroup_members, :source => :member, :source_type => 'Usergroup'
9c0e127b Paul Kelly
3f77babd Joseph Mitchell Magen
has_many_hosts :as => :owner
f2c78d4a Joseph Magen
validates :name, :uniqueness => true
017e1049 Ohad Levy
before_destroy EnsureNotUsedBy.new(:hosts, :usergroups)
9c0e127b Paul Kelly
# The text item to see in a select dropdown menu
81e2d3f3 Ohad Levy
alias_attribute :select_title, :to_s
f2c78d4a Joseph Magen
default_scope lambda { order('usergroups.name') }
1ca44bd3 Daniel Lobato
scoped_search :on => :name, :complete_value => :true
017e1049 Ohad Levy
validate :ensure_uniq_name
9c0e127b Paul Kelly
# This methods retrieves all user addresses in a usergroup
# Returns: Array of strings representing the user's email addresses
def recipients
all_users.map(&:mail).flatten.sort.uniq
end

# This methods retrieves all users in a usergroup
# Returns: Array of users
def all_users(group_list=[self], user_list=[])
retrieve_users_and_groups group_list, user_list
user_list.sort.uniq
end

# This methods retrieves all usergroups in a usergroup
9fd7478e Paul Kelly
# Returns: Array of unique usergroups
9c0e127b Paul Kelly
def all_usergroups(group_list=[self], user_list=[])
retrieve_users_and_groups group_list, user_list
group_list.sort.uniq
end

protected
# Recurses down the tree of usergroups and finds the users
# [+group_list+]: Array of Usergroups that have already been processed
# [+users+] : Array of users accumulated at this point
# Returns : Array of non unique users
def retrieve_users_and_groups(group_list, user_list)
for group in usergroups
next if group_list.include? group
group_list << group

group.retrieve_users_and_groups(group_list, user_list)
end
user_list.concat users
end

017e1049 Ohad Levy
def ensure_uniq_name
01352c23 Bryan Kearney
errors.add :name, _("is already used by a user account") if User.where(:login => name).first
9c0e127b Paul Kelly
end

end