PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/app/models/user.rb

https://github.com/joel/checkin
Ruby | 245 lines | 176 code | 45 blank | 24 comment | 22 complexity | d3631e4c075a3ee8717075a83d7b4ef0 MD5 | raw file
  1. # encoding: utf-8
  2. class User < ActiveRecord::Base
  3. include CheckinLabel
  4. # Include default devise modules. Others available are:
  5. # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  6. devise :database_authenticatable, :registerable,
  7. :recoverable, :rememberable, :trackable, :token_authenticatable
  8. # devise :validatable unless omniauthable?
  9. # Setup accessible (or protected) attributes for your model
  10. # TODO To restore
  11. attr_accessible :email, :password, :password_confirmation, :remember_me, :gender, :firstname, :lastname, :company, :phone, :twitter, :avatar, :bio, :admin,
  12. :checkin_label_msg, :process_done, :username
  13. # # TODO To Remove
  14. # attr_accessible :email, :password, :password_confirmation, :remember_me, :gender, :firstname, :lastname, :company, :phone, :twitter, :avatar,
  15. # :encrypted_password, :reset_password_token, :remember_created_at, :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip, :created_at, :updated_at, :authentication_token, :admin
  16. paginates_per 36
  17. has_many :tokens, :dependent => :destroy
  18. has_many :invitations, :foreign_key => "followed_id", :dependent => :destroy
  19. has_many :relationships, :foreign_key => "follower_id", :dependent => :destroy
  20. has_many :following, :through => :relationships, :source => :followed
  21. has_many :reverse_relationships, :foreign_key => "followed_id", :class_name => "Relationship", :dependent => :destroy
  22. has_many :followers, :through => :reverse_relationships, :source => :follower
  23. has_many :authentications
  24. validates_presence_of :firstname, :lastname, :company, :phone, :email, :username
  25. validates_length_of :email, :within => 6..100 #r@a.wk
  26. # validates_uniqueness_of :email, :case_sensitive => false
  27. # validates_format_of :email, :with => /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i # /^\S+\@(\[?)[a-zA-Z0-9\_\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$/ix # /^\S+@[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,6}$/
  28. validates_format_of :email, :with => /^\S+\@+\S+\.\S{2,4}$/ix
  29. validates_uniqueness_of :username, :case_sensitive => false
  30. # validates_format_of :username, :with => /^\w+$/i, :message => "can only contain letters and numbers."
  31. validates_format_of :username, :with => /^[-A-Za-z0-9@_.]*$/, :message => "can only contain letters and numbers."
  32. # validates_presence_of :gender
  33. # validates_inclusion_of :gender, :in => %w{ Mr Mlle Mme }, :message => "La civilité n'est pas reconnue"
  34. # validates_length_of :firstname, :maximum => 100
  35. # validates_length_of :lastname, :maximum => 100
  36. # validates_presence_of :mobile
  37. # validates_presence_of :email
  38. validates_length_of :email, :within => 6..100 #r@a.wk
  39. validates_uniqueness_of :email, :case_sensitive => false
  40. validates_format_of :email, :with => /^\S+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$/ix
  41. # validates_inclusion_of :gender, :in => %w{ Mr Mlle Mme }, :message => "La civilité n'est pas reconnue"
  42. after_create :set_authentication_token
  43. before_create :normalize_name
  44. before_destroy :clean_token
  45. scope :members, where(:admin => false)
  46. mount_uploader :avatar, AvatarUploader
  47. def password_required?
  48. (authentications.empty? || !password.blank?)
  49. end
  50. def self.user_already_exist?(omniauth)
  51. (user = User.find_by_email(omniauth['info']['email']) rescue nil)
  52. return user if user
  53. (user = User.find_by_username(omniauth['info']['nickname']) rescue nil)
  54. user ||= User.new
  55. return user
  56. end
  57. def apply_omniauth(omniauth)
  58. self.email = omniauth['info']['email'] if email.blank?
  59. # self.username = omniauth['info']['nickname'] if username.blank?
  60. authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'].to_s)
  61. case omniauth['provider']
  62. when 'google'
  63. self.username = omniauth['info']['name'] if self.username.blank?
  64. when 'github'
  65. self.username = omniauth['info']['nickname'] if self.username.blank?
  66. when 'facebook'
  67. self.username = omniauth['info']['nickname'] if self.username.blank?
  68. self.firstname = omniauth['info']['first_name'] if self.firstname.blank?
  69. self.lastname = omniauth['info']['last_name'] if self.lastname.blank?
  70. when 'twitter'
  71. self.username = omniauth['info']['nickname'] if self.username.blank?
  72. self.remote_avatar_url = omniauth['info']['image'] if self.avatar.blank?
  73. end
  74. end
  75. def nb_of_checkin
  76. self.tokens.used.count
  77. end
  78. def nb_checkin_label
  79. self.checkin_label_msg.try(:html_safe)
  80. end
  81. def major?
  82. self == major
  83. end
  84. def major
  85. User.all.sort! { |x,y| y.nb_of_checkin <=> x.nb_of_checkin }[0]
  86. end
  87. def checkin(token_type_id, motivation_id, checkin_owner_id = nil)
  88. # Autorize Admin can perform many checkin for a member (For people occasionally passing, students and others cases)
  89. admin = (User.where(:id => checkin_owner_id.to_i).first.admin? rescue false)
  90. raise I18n.t('users.checkin.already') if checkin? and !admin
  91. token = self.tokens.available.first(:conditions => { :token_type_id => token_type_id })
  92. raise I18n.t('users.checkin.no_credit') unless token
  93. msg = token.checkin(motivation_id, checkin_owner_id)
  94. NotifierMailer.safe_sending { NotifierMailer.checkin_notification(self.id, token.token_type.title, token.motivation.title) }
  95. self.followers.all.each do |follower|
  96. NotifierMailer.safe_sending { NotifierMailer.notify_followers(follower.id, self.id, token.token_type.title, token.motivation.title) }
  97. end
  98. msg
  99. end
  100. def tokens_avalaible?(type_id)
  101. remain_tokens(type_id) > 0
  102. end
  103. def remain_tokens(type_id)
  104. self.tokens.available.count(:conditions => { :token_type_id => type_id })
  105. end
  106. def invitation!(follower)
  107. self.invitations.create(:follower_id => follower.id) unless invitation?(follower)
  108. end
  109. def invitation?(follower)
  110. self.invitations.find_by_follower_id(follower.id)
  111. end
  112. def following?(followed)
  113. self.relationships.find_by_followed_id(followed)
  114. end
  115. def follow!(followed)
  116. self.relationships.create!(:followed_id => followed.id) unless following?(followed)
  117. end
  118. def unfollow!(followed)
  119. self.relationships.find_by_followed_id(followed).destroy if following?(followed)
  120. end
  121. def name
  122. "#{self.firstname} #{self.lastname}"
  123. end
  124. def not_me?(u)
  125. u != self
  126. end
  127. def me?(u)
  128. !not_me?(u)
  129. end
  130. def self.account_already_exist?(current_user)
  131. User.find_by_user_id(current_user.id)
  132. end
  133. def add_tokens(h)
  134. raise I18n.t('users.add_tokens.cost_missing') if h[:price].blank?
  135. h[:number].to_i.times { self.tokens.create(:used => false, :cost => h[:price].to_f/h[:number].to_f, :token_type_id => h[:token_type][:token_type_id], :token_owner_id => h[:token_owner_id]) }
  136. I18n.t('users.add_tokens.credit_added', :number => h[:number].to_i)
  137. end
  138. def checkin?
  139. !self.tokens.used.first(:conditions=>['? between start_at and stop_at',Time.now.utc]).nil?
  140. end
  141. def checkin_label
  142. if self.checkin?
  143. current_token = self.tokens.used.first(:conditions=>['? between start_at and stop_at',Time.now.utc])
  144. I18n.t('users.checkin_label.checkin', :token_type => current_token.token_type.title, :motivation => current_token.motivation.title)
  145. else
  146. I18n.t('users.checkin_label.no_checkin')
  147. end
  148. end
  149. def checkin_owners_label
  150. if self.checkin?
  151. current_token = self.tokens.used.first(:conditions=>['? between start_at and stop_at',Time.now.utc])
  152. msg = I18n.t('users.checkin_owners_label.msg')
  153. current_token.token_owner ? msg.gsub!('OWNER_TOKEN',current_token.token_owner.name) : msg.gsub!('OWNER_TOKEN',"Nobody")
  154. if current_token.checkin_owner
  155. if current_token.checkin_owner == self
  156. msg.gsub!('OWNER_CHECKIN',"HimSelf")
  157. else
  158. msg.gsub!('OWNER_CHECKIN',current_token.checkin_owner.name)
  159. end
  160. else
  161. msg.gsub!('OWNER_CHECKIN',"Nobody")
  162. end
  163. msg
  164. else
  165. I18n.t('users.checkin_label.no_checkin')
  166. end
  167. end
  168. def is_admin?
  169. self.admin
  170. end
  171. def is_guest?
  172. false
  173. end
  174. def show_button_folow?(other)
  175. show = true
  176. # It have already received an invitation
  177. if other.invitations.find_by_follower_id(self.id)
  178. show = false
  179. end
  180. # It's already follow by this person
  181. if self.following?(other) or other.following?(self)
  182. show = false
  183. end
  184. return show
  185. end
  186. private
  187. def clean_token
  188. Token.where(:checkin_owner_id => self.id).all.each do |token|
  189. token.update_attribute(:checkin_owner_id,nil)
  190. end
  191. end
  192. def set_authentication_token
  193. self.reset_authentication_token!
  194. end
  195. def normalize_name
  196. # self.firstname = self.firstname.titlecase
  197. self.firstname = self.firstname.split(/(\W)/).map(&:capitalize).join
  198. self.lastname = self.lastname.upcase
  199. end
  200. end