1 # Groups are used to determine which groups of users have which rights
3 class Group < ActiveRecord::Base
4 has_many :group_permissions
6 has_and_belongs_to_many :users
8 validates_uniqueness_of :name
9 validates_presence_of :name
11 before_destroy :dont_destroy_admins
12 # Don't delete 'admins' from the database
13 def dont_destroy_admins
14 raise "Can't delete admins group" if self.is_the_administrators_group?
17 after_destroy :destroy_dependant_group_permissions
18 # Delete dependant group_permissions.
19 # This code should be executed after_destroy.
20 def destroy_dependant_group_permissions
21 self.group_permissions.each do |group_permission|
22 group_permission.destroy
26 # Returns whether or not the admins group exists
27 def self.admins_group_exists?
28 group = Group.find_by_is_the_administrators_group(true)
29 return (not group.blank?)
32 # Create admins group and add admin user to it.
33 def self.create_admins_group
34 if User.admin_exists? # and Group.admins_group_exists?
37 group.is_the_administrators_group = true
39 # Add the adminstrator to this group:
40 if user = User.find_by_is_the_administrator(true)
41 user.groups.push(group)
44 group.save # save, so true is returned