2009-01-08 16:33:27 +01:00
|
|
|
# Groups organize the User.
|
2009-01-06 11:49:19 +01:00
|
|
|
# A Member gets the roles from the Group
|
|
|
|
class Group < ActiveRecord::Base
|
|
|
|
has_many :memberships, :dependent => :destroy
|
|
|
|
has_many :users, :through => :memberships
|
|
|
|
|
|
|
|
validates_length_of :name, :in => 1..25
|
|
|
|
validates_uniqueness_of :name
|
2011-05-11 11:17:02 +02:00
|
|
|
validate :last_admin_on_earth, :on => :update
|
2011-05-15 23:43:23 +02:00
|
|
|
|
|
|
|
before_destroy :check_last_admin_group
|
2011-05-16 00:34:43 +02:00
|
|
|
|
|
|
|
attr_reader :user_tokens
|
2009-01-06 11:49:19 +01:00
|
|
|
|
|
|
|
# Returns true if the given user if is an member of this group.
|
|
|
|
def member?(user)
|
|
|
|
memberships.find_by_user_id(user.id)
|
|
|
|
end
|
|
|
|
|
2009-01-14 12:46:01 +01:00
|
|
|
# Returns all NONmembers and a checks for possible multiple Ordergroup-Memberships
|
2009-01-06 11:49:19 +01:00
|
|
|
def non_members
|
2009-02-01 20:56:23 +01:00
|
|
|
User.all(:order => 'nick').reject { |u| users.include?(u) }
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2011-05-15 23:43:23 +02:00
|
|
|
|
2011-05-16 00:34:43 +02:00
|
|
|
def user_tokens=(ids)
|
|
|
|
self.user_ids = ids.split(",")
|
|
|
|
end
|
|
|
|
|
2011-05-15 23:43:23 +02:00
|
|
|
protected
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Check before destroy a group, if this is the last group with admin role
|
2011-05-15 23:43:23 +02:00
|
|
|
def check_last_admin_group
|
2009-01-20 19:37:15 +01:00
|
|
|
if self.role_admin == true && Group.find_all_by_role_admin(true).size == 1
|
|
|
|
raise "Die letzte Gruppe mit Admin-Rechten darf nicht gelöscht werden"
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2011-05-15 23:43:23 +02:00
|
|
|
|
2009-01-14 12:46:01 +01:00
|
|
|
# validates uniqueness of the Group.name. Checks groups and ordergroups
|
2009-01-06 11:49:19 +01:00
|
|
|
def validate
|
2009-01-14 12:46:01 +01:00
|
|
|
errors.add(:name, "ist schon vergeben") if (group = Group.find_by_name(name) || group = Ordergroup.find_by_name(name)) && self != group
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# add validation check on update
|
2011-05-11 11:17:02 +02:00
|
|
|
# Return an error if this is the last group with admin role and role_admin should set to false
|
|
|
|
def last_admin_on_earth
|
2009-01-20 19:37:15 +01:00
|
|
|
if self.role_admin == false && Group.find_all_by_role_admin(true).size == 1 && self == Group.find(:first, :conditions => "role_admin = 1")
|
|
|
|
errors.add(:role_admin, "Der letzten Gruppe mit Admin-Rechten darf die Admin-Rolle nicht entzogen werden")
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2011-05-07 20:50:39 +02:00
|
|
|
|
2011-05-07 21:55:24 +02:00
|
|
|
|
2011-05-07 20:50:39 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: groups
|
|
|
|
#
|
2011-05-07 21:55:24 +02:00
|
|
|
# id :integer(4) not null, primary key
|
2011-05-07 20:50:39 +02:00
|
|
|
# type :string(255) default(""), not null
|
|
|
|
# name :string(255) default(""), not null
|
|
|
|
# description :string(255)
|
2011-05-07 21:55:24 +02:00
|
|
|
# account_balance :decimal(8, 2) default(0.0), not null
|
2011-05-07 20:50:39 +02:00
|
|
|
# account_updated :datetime
|
|
|
|
# created_on :datetime not null
|
2011-05-07 21:55:24 +02:00
|
|
|
# role_admin :boolean(1) default(FALSE), not null
|
|
|
|
# role_suppliers :boolean(1) default(FALSE), not null
|
|
|
|
# role_article_meta :boolean(1) default(FALSE), not null
|
|
|
|
# role_finance :boolean(1) default(FALSE), not null
|
|
|
|
# role_orders :boolean(1) default(FALSE), not null
|
|
|
|
# weekly_task :boolean(1) default(FALSE)
|
|
|
|
# weekday :integer(4)
|
2011-05-07 20:50:39 +02:00
|
|
|
# task_name :string(255)
|
|
|
|
# task_description :string(255)
|
2011-05-07 21:55:24 +02:00
|
|
|
# task_required_users :integer(4) default(1)
|
2011-05-07 20:50:39 +02:00
|
|
|
# deleted_at :datetime
|
|
|
|
# contact_person :string(255)
|
|
|
|
# contact_phone :string(255)
|
|
|
|
# contact_address :string(255)
|
|
|
|
# stats :text
|
2011-05-07 21:55:24 +02:00
|
|
|
# task_duration :integer(4) default(1)
|
2011-05-07 20:50:39 +02:00
|
|
|
#
|
|
|
|
|