Sha256: 72ee17d9a4653af2c8152b533f6bbdc9d168ee45e4e2ca4f7c1ba7ea9a4dd32a

Contents?: true

Size: 984 Bytes

Versions: 1

Compression:

Stored size: 984 Bytes

Contents

# == Schema Information
#
# Table name: organization_users
#
#  id              :integer          not null, primary key
#  organization_id :integer          not null
#  user_id         :integer          not null
#  role            :string(255)      not null
#  created_at      :datetime
#  updated_at      :datetime
#

class OrganizationUser < ActiveRecord::Base
  ROLES = %w(owner member)

  belongs_to :organization
  belongs_to :user

  validates :role, inclusion: { in: ROLES }
  validates :organization, presence: true
  validates :user, presence: true, uniqueness: { scope: :organization }

  validate :one_owner, on: :update

  def destroy
    raise OnlyOrganizationOwner if only_owner?
    super
  end


  private

  def one_owner
    raise OnlyOrganizationOwner if (role != "owner" && one_owner?)
  end

  def only_owner?
   role == "owner" && one_owner?
  end

  def one_owner?
    organization.organization_users.map(&:role).reject {|r| r != "owner"}.length <= 1
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
self_systeem-0.1.0 test/dummy_app/app/models/organization_user.rb