2012-08-25 04:14:51 +00:00
class User < ActiveRecord :: Base
2013-01-26 09:21:41 +00:00
include Gravtastic
2014-02-14 09:12:13 +00:00
gravtastic :size = > 150 , :default = > " "
2013-01-26 09:21:41 +00:00
2012-09-02 11:12:26 +00:00
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
2012-10-14 12:46:19 +00:00
devise :database_authenticatable , :registerable ,
2012-09-02 11:12:26 +00:00
:recoverable , :rememberable , :trackable , :validatable
# Setup accessible (or protected) attributes for your model
2014-02-09 04:06:07 +00:00
attr_accessible :email , :password , :password_confirmation , :remember_me , :name , :admin , :instructor , :member , :emergency_name , :emergency_phone , :current_skills , :desired_skills , :waiver , :emergency_email , :phone , :payment_method , :orientation , :member_level , :certifications , :hidden , :marketing_source , :payee , :accountant , :exit_reason , :twitter_url , :facebook_url , :github_url , :website_url , :email_visible , :phone_visible , :postal_code #TODO: make admin/instructor/member/etc not accessible
2012-08-25 04:14:51 +00:00
2014-04-11 03:10:04 +00:00
belongs_to :oriented_by , :foreign_key = > " oriented_by_id " , :class_name = > " User "
2012-10-14 12:46:19 +00:00
has_many :cards
2013-01-25 10:50:53 +00:00
has_many :user_certifications
has_many :certifications , :through = > :user_certifications
2014-03-03 02:06:39 +00:00
has_many :contracts
2013-02-12 08:58:17 +00:00
has_many :payments
2013-09-24 01:51:22 +00:00
has_many :macs
2014-02-09 12:01:52 +00:00
has_many :resources
2013-01-25 13:01:02 +00:00
2014-02-14 10:48:03 +00:00
scope :volunteer , - > { where ( 'member_level >= 10 AND member_level < 25' ) }
scope :paying , - > { joins ( :payments ) . where ( " payments.date > ? " , ( DateTime . now - 90 . days ) ) . uniq }
2013-08-29 23:28:57 +00:00
validates_format_of [ :twitter_url , :facebook_url , :github_url , :website_url ] , :with = > URI :: regexp ( %w( http https ) ) , :allow_blank = > true
2013-08-29 08:23:21 +00:00
2014-02-13 03:11:21 +00:00
# disable # validates_presence_of :postal_code
2014-02-09 04:06:07 +00:00
2013-02-01 11:44:05 +00:00
after_create :send_new_user_email
2013-05-24 06:25:09 +00:00
def absorb_user ( user_to_absorb )
# copy all attributes except email, password, name, and anything that isn't blank on the destination
user_to_absorb . attributes . each_pair { | k , v |
unless ( v . nil? || k == :id || k == :email || k == :password || k == :name || k == :password_confirmation || k == :hidden || k == 'hidden' || k == :encrypted_password || ! self . attributes [ k ] . blank? )
Rails . logger . info " Updating " + k . to_s + " from " + self [ k ] . to_s
self [ k ] = v
Rails . logger . info " Updated " + k . to_s + " to " + self [ k ] . to_s
end
}
self . save!
user_to_absorb . cards . each { | card |
Rails . logger . info " CARD BEFORE: " + card . inspect
card . user_id = self . id
card . save!
Rails . logger . info " CARD AFTER: " + card . inspect
}
user_to_absorb . user_certifications . each { | user_cert |
Rails . logger . info " CERT BEFORE: " + user_cert . inspect
user_cert . user_id = self . id
user_cert . save!
Rails . logger . info " CERT AFTER: " + user_cert . inspect
}
user_to_absorb . payments . each { | payment |
Rails . logger . info " PAYMENT BEFORE: " + payment . inspect
payment . user_id = self . id
2013-08-29 08:23:21 +00:00
payment . amount = 0 if payment . amount . nil? # Bypass validation on amount
2013-05-24 06:25:09 +00:00
payment . save!
Rails . logger . info " PAYMENT AFTER: " + payment . inspect
}
user_to_absorb . destroy
end
2013-12-02 09:03:40 +00:00
def card_access_enabled
# If the user has at least one card with permission level 1, they have access
self . cards . where ( :card_permissions = > 1 ) . count > 0
end
2013-05-24 06:25:09 +00:00
def name_with_email_and_visibility
if hidden then
" #{ name } ( #{ email } ) HIDDEN "
else
" #{ name } ( #{ email } ) "
end
end
2013-02-12 08:58:17 +00:00
def name_with_payee_and_member_level
if payee . blank? then
" #{ name } - #{ member_level_string } "
else
" #{ payee } for #{ name } - #{ member_level_string } "
end
end
2013-02-01 11:44:05 +00:00
2013-02-12 08:58:17 +00:00
def member_level_string
case self . member_level . to_i
when 0
" None "
when 1
" Unable "
when 10 .. 24
" Volunteer "
when 25 .. 49
" Associate ($25) "
when 50 .. 99
" Basic ($50) "
when 100 .. 999
" Plus ($100) "
2013-01-26 04:11:01 +00:00
end
2013-02-12 08:58:17 +00:00
end
2013-01-26 04:11:01 +00:00
2013-08-24 09:18:37 +00:00
def self . member_levels
2013-08-28 10:18:47 +00:00
{ 25 = > " Associate " , 50 = > " Basic " , 75 = > " Basic " , 100 = > " Plus " }
2013-08-24 09:18:37 +00:00
end
2013-08-28 15:19:01 +00:00
def payment_status
results = payment_status_calculation
return results [ :paid ]
end
2013-02-12 08:58:17 +00:00
def member_status
2013-08-27 07:39:43 +00:00
results = member_status_calculation
return results [ :rank ]
2013-02-12 08:58:17 +00:00
end
def member_status_symbol
2013-08-27 07:09:54 +00:00
results = member_status_calculation
2013-08-28 15:40:36 +00:00
return " <img src='/ #{ results [ :icon ] } #{ results [ :flair ] } -coin.png' title=' #{ results [ :message ] } ' class='member-status-symbol' /> "
2013-08-27 07:09:54 +00:00
end
2013-08-29 08:23:21 +00:00
def last_payment_date
self . payments . maximum ( :date )
end
2013-08-28 15:19:01 +00:00
def delinquency
if self . payments . count > 0
paydate = self . payments . maximum ( :date )
( Date . today - paydate ) . to_i
else
( Date . today - self . created_at . to_date ) . to_i
end
end
2013-10-06 11:09:56 +00:00
def send_email ( from_user , subject , body )
Rails . logger . info UserMailer . email ( self , from_user , subject , body ) . deliver
end
2014-02-23 12:55:00 +00:00
def has_certification? ( cert_slug )
if self . certifications . find_by_slug ( cert_slug )
true
else
false
end
end
2014-03-03 04:48:49 +00:00
def contract_date
self . contracts . first . signed_at unless self . contracts . blank?
end
private
2013-08-27 07:09:54 +00:00
2013-10-06 11:09:56 +00:00
def send_new_user_email
Rails . logger . info UserMailer . new_user_email ( self ) . deliver
end
2013-08-27 07:39:43 +00:00
def member_status_calculation
2013-08-27 06:39:54 +00:00
# Begin output buffer
message = " "
icon = " "
flair = " "
2013-08-27 07:09:54 +00:00
rank = 0
2013-08-27 06:39:54 +00:00
# First status item is level
2013-02-12 08:58:17 +00:00
case self . member_level . to_i
2013-08-27 06:39:54 +00:00
when 0 .. 9
2013-02-12 08:58:17 +00:00
if self . payments . count > 0 then
2013-08-28 15:19:01 +00:00
message = " Former Member ( #{ ( DateTime . now - self . payments . maximum ( :date ) ) . to_i / 30 } months ago) "
2013-08-27 06:39:54 +00:00
icon = :timeout
2013-08-27 07:09:54 +00:00
rank = 1
2013-02-12 08:58:17 +00:00
else
2013-08-27 06:39:54 +00:00
message = " Not a Member "
icon = :no
2013-08-27 07:09:54 +00:00
rank = 0
2013-01-26 04:11:01 +00:00
end
2013-02-12 08:58:17 +00:00
when 10 .. 24
2013-08-27 06:39:54 +00:00
message = " Volunteer "
icon = :heart
2013-08-27 07:39:43 +00:00
rank = 101
2013-08-27 06:39:54 +00:00
when 25 .. 49
message = member_level_string
icon = :copper
2013-08-27 07:09:54 +00:00
rank = 250
2013-08-27 06:39:54 +00:00
when 50 .. 99
message = member_level_string
icon = :silver
2013-08-27 07:09:54 +00:00
rank = 500
2013-08-27 06:39:54 +00:00
when 100 .. 999
message = member_level_string
icon = :gold
2013-08-27 07:09:54 +00:00
rank = 1000
2013-08-27 06:39:54 +00:00
end
2013-08-28 15:19:01 +00:00
payment_results = payment_status_calculation
flair = payment_results [ :flair ]
rank = rank / 10 unless payment_results [ :paid ]
message = payment_results [ :message ] unless payment_results [ :message ] . blank?
return { :message = > message , :icon = > icon , :flair = > flair , :rank = > rank }
end
def payment_status_calculation
flair = " "
message = " "
paid = true
2013-08-27 06:39:54 +00:00
# Second status item is payment status
case self . member_level . to_i
2013-02-12 08:58:17 +00:00
when 25 .. 999
2013-08-27 06:39:54 +00:00
# There are payments
2013-02-12 08:58:17 +00:00
if self . payments . count > 0 then
2013-08-27 06:39:54 +00:00
# They're on time
2013-08-28 15:19:01 +00:00
if self . payments . maximum ( :date ) > ( DateTime . now - 60 . days )
2013-08-27 06:39:54 +00:00
flair = " -paid "
2013-08-28 15:19:01 +00:00
paid = true
2013-02-12 08:58:17 +00:00
else
2013-08-28 15:19:01 +00:00
message = " Last Payment #{ ( DateTime . now - self . payments . maximum ( :date ) ) . to_i / 30 } months ago "
paid = false
2013-02-12 08:58:17 +00:00
end
2013-08-27 07:09:54 +00:00
else
message = " No Payments Recorded "
2013-08-28 15:19:01 +00:00
paid = false
2013-01-26 04:11:01 +00:00
end
2013-01-25 13:01:02 +00:00
end
2013-08-28 15:19:01 +00:00
return { :message = > message , :paid = > paid , :flair = > flair }
2013-01-25 13:01:02 +00:00
end
2013-02-01 11:44:05 +00:00
2012-08-25 04:14:51 +00:00
end