Class: Roleable::RoleValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/models/concerns/roleable.rb

Overview

role management


enum listing possible roles max_role setting the model max role to that of the current user - you never can promote anyone above your own level set_default_role sets the role of a new user

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#userObject

Returns the value of attribute user.



32
33
34
# File 'app/models/concerns/roleable.rb', line 32

def user
  @user
end

Instance Method Details

#max_role_exhaustedObject



47
48
49
50
51
# File 'app/models/concerns/roleable.rb', line 47

def max_role_exhausted
  User.roles[user.role] > User.roles[user.max_role]
rescue
  false
end

#old_role_less_than_newObject



41
42
43
44
45
# File 'app/models/concerns/roleable.rb', line 41

def old_role_less_than_new
  User.roles[user.previous_version.role] < User.roles[user.role]
rescue
  false
end

#validate(record) ⇒ Object



33
34
35
36
37
38
39
# File 'app/models/concerns/roleable.rb', line 33

def validate(record)
  @user = record
  Rails.logger.info ("roles: old %s new %s" % [ User.roles[record.previous_version.role], User.roles[record.role] ] ) rescue "no previous role"
  if max_role_exhausted and old_role_less_than_new
    record.errors[:role] << I18n.t('.assigned_role_not_allowed')
  end
end