class Role < ActiveRecord::Base
include FlagShihTzu
##
# Associations
belongs_to :user
belongs_to :plan
##
# Define Bit Field Values
# Column access
has_flags 1 => :creator,
2 => :administrator,
3 => :editor,
column: 'access'
# EVALUATE CLASS AND INSTANCE METHODS BELOW
#
# What do they do? do they do it efficiently, and do we need them?
# These functions are from the old project_groups model
##
# returns the user's email unless it is nil
#
# @return [Boolean, String] false if no email exists, the email otherwise
def email
unless user.nil?
return user.email
end
end
##
# define a new user for the project group by email
#
# @param new_email [String] the email of the new user for the project group
# @return [User] the new user
def email=(new_email)
unless User.find_by(email: email).nil? then
user = User.find_by(email: email)
end
self.save!
end
##
# return the access level for the current project group
# 3 if the user is an administrator
# 2 if the user is an editor
# 1 if the user can only read
#
# @return [Integer]
def access_level
if self.administrator? then
return 3
elsif self.editor? then
return 2
else
return 1
end
self.save!
end
##
# define a new access level for the current project group
# if >=3, the user is a project administrator
# if >=2, the user is an editor
#
# @param new_access_level [Integer] the access level to give the user
def access_level=(new_access_level)
new_access_level = new_access_level.to_i
if new_access_level >= 3 then
self.administrator = true
else
self.administrator = false
end
if new_access_level >= 2 then
self.editor = true
else
self.editor = false
end
self.save!
end
end