summaryrefslogtreecommitdiff
path: root/app/models/user_group_membership.rb
blob: 18a8d482b093a64f755a5b8394fa71ca1d19b89f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class UserGroupMembership < ActiveRecord::Base
  belongs_to :user
  belongs_to :user_group
  
  validates_uniqueness_of :user_id, :scope => :user_group_id
  validates_presence_of :user
  validates_presence_of :user_group
  
  validate :user_belongs_to_the_tenant_of_the_user_group
  
  # State Machine stuff
  default_scope where(:state => 'active')
  state_machine :initial => :active do
  end

  def to_s
    "#{self.user} / #{self.user_group}"
  end

  private
  def user_belongs_to_the_tenant_of_the_user_group
    if !self.user_group.tenant.users.include?(self.user)
      errors.add(:user_id, "not a member of the tenant which this group belongs to")
    end
  end
end