Module: Sunrise::Models::User::InstanceMethods

Defined in:
lib/sunrise/models/user.rb

Instance Method Summary collapse

Instance Method Details

#admin?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/sunrise/models/user.rb', line 56

def admin?
  has_role?(:admin)
end

#avatar_small_urlObject



119
120
121
122
123
124
125
# File 'lib/sunrise/models/user.rb', line 119

def avatar_small_url
  if self.avatar
    self.avatar.url(:small)
  else
    "/images/manage/user_pic_small.gif"
  end
end

#current_roleObject



83
84
85
# File 'lib/sunrise/models/user.rb', line 83

def current_role
  self.roles.first
end

#default?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/sunrise/models/user.rb', line 48

def default?
  has_role?(:default)
end

#events_for_current_stateObject



110
111
112
113
114
115
116
117
# File 'lib/sunrise/models/user.rb', line 110

def events_for_current_state
  events = []
  events << 'activate' unless confirmed?
  events << 'unlock' if access_locked?
  # TODO: ban access for active users
  # events << 'suspend' if active_for_authentication?
  events
end

#has_login?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/sunrise/models/user.rb', line 68

def has_login?
  respond_to?(:login)
end

#has_role?(role_name) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/sunrise/models/user.rb', line 60

def has_role?(role_name)
  role_symbols.include?(role_name.to_sym)
end

#moderator?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/sunrise/models/user.rb', line 52

def moderator?
  has_role?(:moderator)
end

#role_symbolsObject



79
80
81
# File 'lib/sunrise/models/user.rb', line 79

def role_symbols
  (roles || []).map {|r| r.to_sym}
end

#role_type_idObject



87
88
89
90
91
# File 'lib/sunrise/models/user.rb', line 87

def role_type_id
  if current_role
    current_role.role_type.id
  end
end

#role_type_id=(value) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/sunrise/models/user.rb', line 93

def role_type_id=(value)
  role_id = value.blank? ? nil : value.to_i
  
  if ::RoleType.legal?(role_id)
    ::RoleType.all.each do |role_type|
      create_or_destroy_role(role_type.id, role_type.id == role_id)
    end
  end
end

#roles_attributes=(value) ⇒ Object



72
73
74
75
76
77
# File 'lib/sunrise/models/user.rb', line 72

def roles_attributes=(value)
  options = value || {}
  options.each do |k, v|
    create_or_destroy_role(k.to_i, v.to_i == 1)
  end
end

#roles_empty?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/sunrise/models/user.rb', line 64

def roles_empty?
  self.roles.empty?
end

#stateObject



103
104
105
106
107
108
# File 'lib/sunrise/models/user.rb', line 103

def state
  return 'active'   if active_for_authentication?
  return 'register' unless confirmed?
  return 'suspend'  if access_locked?
  return 'pending'
end