foodsoft/app/models/group.rb

43 lines
1,005 B
Ruby
Raw Normal View History

# encoding: utf-8
# Groups organize the User.
2009-01-06 11:49:19 +01:00
# A Member gets the roles from the Group
class Group < ActiveRecord::Base
include MarkAsDeletedWithName
has_many :memberships, dependent: :destroy
2009-01-06 11:49:19 +01:00
has_many :users, :through => :memberships
2011-06-10 01:36:04 +02:00
validates :name, :presence => true, :length => {:in => 1..25}
attr_reader :user_tokens
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
# Returns all NONmembers and a checks for possible multiple Ordergroup-Memberships
2009-01-06 11:49:19 +01:00
def non_members
User.natural_order.reject { |u| users.include?(u) }
2009-01-06 11:49:19 +01:00
end
def user_tokens=(ids)
self.user_ids = ids.split(",")
end
def deleted?
deleted_at.present?
end
def mark_as_deleted
# TODO: Checks for participating in not closed orders
transaction do
memberships.destroy_all
# @todo what should happen to the users?
super
end
end
2009-01-06 11:49:19 +01:00
end