Class: User

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Paperclip, Mongoid::Spacial::Document, Mongoid::Timestamps
Defined in:
app/models/user.rb

Overview

This is a Device-User-Class extended with ROLES, Avatar-handling, and more

Constant Summary

ROLES =

Roles - Do not change the order and do not remove roles if you already have productive data! Thou it’s safe to append new roles at the end of the string. And it’s safe to rename roles in place

[:guest, :confirmed_user, :author, :moderator, :maintainer, :admin]

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) clear_avatar

virtual attribute needed for the view but is false always.



121
122
123
# File 'app/models/user.rb', line 121

def clear_avatar
  false
end

- (Object) crop_h

Returns the value of attribute crop_h



63
64
65
# File 'app/models/user.rb', line 63

def crop_h
  @crop_h
end

- (Object) crop_w

Returns the value of attribute crop_w



63
64
65
# File 'app/models/user.rb', line 63

def crop_w
  @crop_w
end

- (Object) crop_x

Returns the value of attribute crop_x



63
64
65
# File 'app/models/user.rb', line 63

def crop_x
  @crop_x
end

- (Object) crop_y

Returns the value of attribute crop_y



63
64
65
# File 'app/models/user.rb', line 63

def crop_y
  @crop_y
end

Instance Method Details

- (Boolean) admin?

Returns:

  • (Boolean)


100
101
102
# File 'app/models/user.rb', line 100

def admin?
  User.all.any? ? (self == User.first || role?(:admin)) : true
end

- (Object) apply_omniauth(omniauth)

fetch attributes from the omniauth-record.



131
132
133
134
135
# File 'app/models/user.rb', line 131

def apply_omniauth(omniauth)
  self. = omniauth['user_info']['email'] if .blank?
  apply_trusted_services(omniauth) if self.new_record?
  authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
end

- (Object) articles



29
30
31
# File 'app/models/user.rb', line 29

def articles
  []
end

- (Object) avatar_geometry(style = :original)



86
87
88
# File 'app/models/user.rb', line 86

def avatar_geometry(style = :original)
  paperclip_geometry avatar, style
end

- (Object) avatar_url(mode)

String - the URL of the local avatar or the gravatar

Returns:

  • String - the URL of the local avatar or the gravatar



147
148
149
150
151
152
153
# File 'app/models/user.rb', line 147

def avatar_url(mode)
  if self.use_gravatar
    "http://gravatar.com/avatar/#{gravatar_id}.png?cache=#{self.updated_at.strftime('%Y%m%d%H%M%S')}"
  else
    avatar.url(mode)
  end
end

- (Boolean) cropping?

Returns:

  • (Boolean)


82
83
84
# File 'app/models/user.rb', line 82

def cropping?
  !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end

- (Object) gravatar_profile

Link to the gravatar profile



156
157
158
159
160
# File 'app/models/user.rb', line 156

def gravatar_profile
  if self.use_gravatar
    "http://gravatar.com/#{gravatar_id}"
  end
end

- (Object) invitation



19
20
21
# File 'app/models/user.rb', line 19

def invitation
  @invitation ||= Invitation.criteria.for_ids(self.invitation_id).first
end

- (Object) invitation=(inv)



22
23
24
25
# File 'app/models/user.rb', line 22

def invitation=(inv)
  @invitation = nil
  self.invitation_id = inv.id
end

- (Object) location_token



162
163
164
165
166
# File 'app/models/user.rb', line 162

def location_token
  if self.location[:lat].present? && self.location[:lng].present?
    "%3.4f,%3.4f" % [self.location[:lat], self.location[:lng]]
  end
end

- (Object) location_token=(str)



168
169
170
171
172
173
174
# File 'app/models/user.rb', line 168

def location_token=(str)
  coordinates = str.split(",").map! { |a| a.strip.gsub(/\(|\)/,'') }
  self.location = {
    lat: coordinates[0].to_f,
    lng: coordinates[1].to_f
  }
end

- (Boolean) new_avatar?

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
# File 'app/models/user.rb', line 90

def new_avatar?
  if avatar.updated_at && ((Time::now() - Time::at(self.avatar.updated_at)) < 1.minute)
    self.use_gravatar = false
    save
    true
  else
    false
  end
end

- (Object) role

return user’s role as symbol.



110
111
112
# File 'app/models/user.rb', line 110

def role
  ROLES[roles_mask].to_sym
end

- (Object) role=(role)



104
105
106
107
# File 'app/models/user.rb', line 104

def role=(role)
  self.roles_mask = ROLES.index(role)
  Rails.logger.warn("SET ROLES TO #{self.roles_mask} FOR #{self.inspect}")
end

- (Boolean) role?(role)

Ask if the user has at least a specific role.

  @user.role?('admin')

Returns:

  • (Boolean)


116
117
118
# File 'app/models/user.rb', line 116

def role?(role)
  self.roles_mask >= ROLES.index(role.to_sym)
end

- (Object) update_with_password(params = {})

remove the password and password-confirmation attribute if not needed.



138
139
140
141
142
143
144
# File 'app/models/user.rb', line 138

def update_with_password(params={})
  if params[:password].blank?
    params.delete(:password)
    params.delete(:password_confirmation) if params[:password_confirmation].blank?
  end
  super
end