2014-09-21 12:47:58 +02:00
|
|
|
# Groups organize the User.
|
2009-01-06 11:49:19 +01:00
|
|
|
# A Member gets the roles from the Group
|
2019-01-13 07:05:54 +01:00
|
|
|
class Group < ApplicationRecord
|
2016-06-04 23:36:11 +02:00
|
|
|
include FindEachWithOrder
|
2015-04-17 20:05:46 +02:00
|
|
|
include MarkAsDeletedWithName
|
|
|
|
|
2014-09-21 12:47:58 +02:00
|
|
|
has_many :memberships, dependent: :destroy
|
2018-12-18 15:48:27 +01:00
|
|
|
has_many :users, -> { where(deleted_at: nil) }, through: :memberships
|
2011-06-10 01:36:04 +02:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
validates :name, presence: true, length: { in: 1..25 }
|
|
|
|
validates :name, uniqueness: true
|
2014-09-21 12:47:58 +02:00
|
|
|
|
2011-05-16 00:34:43 +02:00
|
|
|
attr_reader :user_tokens
|
2013-03-16 17:53:24 +01:00
|
|
|
|
|
|
|
scope :undeleted, -> { where(deleted_at: nil) }
|
|
|
|
|
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
|
2014-09-21 12:47:58 +02:00
|
|
|
|
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
|
2014-02-20 15:04:53 +01:00
|
|
|
User.natural_order.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)
|
2023-05-12 13:01:12 +02:00
|
|
|
self.user_ids = ids.split(',')
|
2011-05-16 00:34:43 +02:00
|
|
|
end
|
2013-03-16 17:53:24 +01:00
|
|
|
|
|
|
|
def deleted?
|
|
|
|
deleted_at.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_as_deleted
|
|
|
|
# TODO: Checks for participating in not closed orders
|
|
|
|
transaction do
|
|
|
|
memberships.destroy_all
|
2015-04-17 20:05:46 +02:00
|
|
|
# @todo what should happen to the users?
|
|
|
|
super
|
2013-03-16 17:53:24 +01:00
|
|
|
end
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|