PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/app/models/user.rb

https://github.com/nnandosouza/catarse
Ruby | 198 lines | 147 code | 13 blank | 38 comment | 18 complexity | 068979bbf48696659cb91433a8e7c271 MD5 | raw file
  1. # coding: utf-8
  2. class User < ActiveRecord::Base
  3. include ActionView::Helpers::NumberHelper
  4. include ActionView::Helpers::TextHelper
  5. include Rails.application.routes.url_helpers
  6. begin
  7. sync_with_mailee :news => :newsletter, :list => "Newsletter"
  8. rescue Exception => e
  9. Rails.logger.error "Error when syncing with mailee: #{e.inspect}"
  10. end
  11. validates_presence_of :provider, :uid, :site
  12. validates_uniqueness_of :uid, :scope => :provider
  13. validates_length_of :bio, :maximum => 140
  14. validates :email, :email => true, :allow_nil => true, :allow_blank => true
  15. has_many :backs, :class_name => "Backer"
  16. has_many :projects
  17. has_many :notifications
  18. has_many :comments
  19. has_many :secondary_users, :class_name => 'User', :foreign_key => :primary_user_id
  20. has_and_belongs_to_many :manages_projects, :join_table => "projects_managers", :class_name => 'Project'
  21. belongs_to :site
  22. belongs_to :primary, :class_name => 'User', :foreign_key => :primary_user_id
  23. scope :primary, :conditions => ["primary_user_id IS NULL"]
  24. scope :backers, :conditions => ["id IN (SELECT DISTINCT user_id FROM backers WHERE confirmed)"]
  25. #before_save :store_primary_user
  26. def admin?
  27. admin
  28. end
  29. def calculate_credits(sum = 0, backs = [], first = true)
  30. # return sum if backs.size == 0 and not first
  31. backs = self.backs.where(:confirmed => true, :requested_refund => false).order("created_at").all if backs == [] and first
  32. back = backs.first
  33. return sum unless back
  34. sum -= back.value if back.credits
  35. if back.project.finished?
  36. unless back.project.successful?
  37. sum += back.value
  38. # puts "#{back.project.name}: +#{back.value}"
  39. end
  40. end
  41. calculate_credits(sum, backs.drop(1), false)
  42. end
  43. def update_credits
  44. self.update_attribute :credits, self.calculate_credits
  45. end
  46. def store_primary_user
  47. return if email.nil? or self.primary_user_id
  48. primary_user = User.primary.where(:email => email).first
  49. if primary_user and primary_user.id != self.id
  50. self.primary_user_id = primary_user.id
  51. end
  52. end
  53. def to_param
  54. return "#{self.id}" unless self.display_name
  55. "#{self.id}-#{self.display_name.parameterize}"
  56. end
  57. def self.find_with_omni_auth(provider, uid)
  58. u = User.find_by_provider_and_uid(provider, uid)
  59. return nil unless u
  60. u.primary.nil? ? u : u.primary
  61. end
  62. def self.create_with_omniauth(site, auth, primary_user_id = nil)
  63. u = create! do |user|
  64. user.provider = auth["provider"]
  65. user.uid = auth["uid"]
  66. user.name = auth["user_info"]["name"]
  67. user.name = auth["user_info"][:name] if user.name.nil?
  68. user.email = auth["user_info"]["email"]
  69. user.email = auth["extra"]["user_hash"]["email"] if auth["extra"] and auth["extra"]["user_hash"] and user.email.nil?
  70. user.nickname = auth["user_info"]["nickname"]
  71. user.bio = auth["user_info"]["description"][0..139] if auth["user_info"]["description"]
  72. user.image_url = auth["user_info"]["image"]
  73. user.site = site
  74. user.locale = I18n.locale.to_s
  75. end
  76. # If we could not associate by email we try to use the parameter
  77. if u.primary.nil? and primary_user_id
  78. u.primary = User.find_by_id(primary_user_id)
  79. end
  80. u.primary.nil? ? u : u.primary
  81. end
  82. def display_name
  83. name || nickname || I18n.t('user.no_name')
  84. end
  85. def display_nickname
  86. if nickname =~ /profile\.php/
  87. name
  88. else
  89. nickname||name
  90. end
  91. end
  92. def short_name
  93. truncate display_name, :length => 26
  94. end
  95. def medium_name
  96. truncate display_name, :length => 42
  97. end
  98. def display_image
  99. gravatar_url || image_url || '/images/user.png'
  100. end
  101. def backer?
  102. backs.confirmed.not_anonymous.count > 0
  103. end
  104. def total_backs
  105. backs.confirmed.not_anonymous.count
  106. end
  107. def backs_text
  108. if total_backs == 2
  109. I18n.t('user.backs_text.two')
  110. elsif total_backs > 1
  111. I18n.t('user.backs_text.many', :total => (total_backs-1))
  112. else
  113. I18n.t('user.backs_text.one')
  114. end
  115. end
  116. def remember_me_hash
  117. Digest::MD5.new.update("#{self.provider}###{self.uid}").to_s
  118. end
  119. def display_credits
  120. number_to_currency credits, :unit => 'R$', :precision => 0, :delimiter => '.'
  121. end
  122. def merge_into!(new_user)
  123. self.primary = new_user
  124. new_user.credits += self.credits
  125. self.credits = 0
  126. self.backs.update_all :user_id => new_user.id
  127. self.projects.update_all :user_id => new_user.id
  128. self.comments.update_all :user_id => new_user.id
  129. self.notifications.update_all :user_id => new_user.id
  130. self.save
  131. new_user.save
  132. end
  133. def as_json(options={})
  134. {
  135. :id => id,
  136. :email => email,
  137. :name => display_name,
  138. :short_name => short_name,
  139. :medium_name => medium_name,
  140. :image => display_image,
  141. :total_backs => total_backs,
  142. :backs_text => backs_text,
  143. :url => user_path(self),
  144. :admin => admin
  145. }
  146. end
  147. protected
  148. # Returns a Gravatar URL associated with the email parameter
  149. def gravatar_url
  150. return unless email
  151. "http://gravatar.com/avatar/#{Digest::MD5.new.update(email)}.jpg?default=#{image_url or "http://catarse.me/images/user.png"}"
  152. end
  153. end
  154. # == Schema Information
  155. #
  156. # Table name: users
  157. #
  158. # id :integer not null, primary key
  159. # primary_user_id :integer
  160. # provider :text not null
  161. # uid :text not null
  162. # email :text
  163. # name :text
  164. # nickname :text
  165. # bio :text
  166. # image_url :text
  167. # newsletter :boolean default(FALSE)
  168. # project_updates :boolean default(FALSE)
  169. # created_at :datetime
  170. # updated_at :datetime
  171. # admin :boolean default(FALSE)
  172. # full_name :text
  173. # address_street :text
  174. # address_number :text
  175. # address_complement :text
  176. # address_neighbourhood :text
  177. # address_city :text
  178. # address_state :text
  179. # address_zip_code :text
  180. # phone_number :text
  181. # credits :decimal(, ) default(0.0)
  182. # site_id :integer default(1), not null
  183. # session_id :text
  184. # locale :text default("pt"), not null
  185. #