User/Person models merge

This commit is contained in:
Stella Rouzi 2014-06-23 19:07:50 +03:00
parent 66b84e3660
commit 23bf55b66c
80 changed files with 1064 additions and 1221 deletions

View file

@ -1,4 +1,7 @@
class User < ActiveRecord::Base
include Gravtastic
gravtastic :size => 32
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
@ -8,18 +11,22 @@ class User < ActiveRecord::Base
:omniauthable, omniauth_providers: [:novell, :google, :facebook]
has_and_belongs_to_many :roles
has_one :person, :inverse_of => :user
has_many :openids
attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :role_ids,
:person_attributes
accepts_nested_attributes_for :person
:name, :email_public, :biography, :nickname, :affiliation
has_many :event_users, :dependent => :destroy
has_many :events, -> { uniq }, :through => :event_users
has_many :registrations, :dependent => :destroy
has_many :votes, :dependent => :destroy
has_many :voted_events, :through => :votes, :source => :events
accepts_nested_attributes_for :roles
before_create :setup_role
before_create :create_person
delegate :last_name, :first_name, :public_name, to: :person
validates :name, presence: true
# Searches for user based on email. Returns found user or new user.
# ====Returns
@ -33,6 +40,7 @@ class User < ActiveRecord::Base
if user.new_record?
user.email = auth.info.email
user.name = auth.info.name
user.password = Devise.friendly_token[0, 20]
user.skip_confirmation!
end
@ -50,33 +58,70 @@ class User < ActiveRecord::Base
end
def setup_role
roles << Role.find_by(name: 'Admin') if User.count == 0
roles << Role.find_by(name: 'Participant') if roles.empty?
roles << Role.where(name: 'Admin') if User.count == 0
roles << Role.where(name: 'Participant') if roles.empty?
end
def popup_details
details = "<b>Sign-in Count</b><br>"
details += "#{self.sign_in_count}<br>"
details += "<b>Current Sign-in</b><br>"
details += "#{self.current_sign_in_at}<br>"
details += "<b>Last Sign-in</b><br>"
details += "#{self.last_sign_in_at}<br>"
details += "<b>Current Sign-in IP</b><br>"
details += "#{self.current_sign_in_ip}<br>"
details += "<b>Last Sign-in IP</b><br>"
details += "#{self.last_sign_in_ip}<br>"
details += "<b>Created at</b><br>"
details += "#{self.created_at}<br>"
def self.prepare(params)
email = params['email']
user = User.where(email: email).first_or_initialize
# If there is a new user, add the necessary attributes
if user.new_record?
user.password = Devise.friendly_token[0,20]
user.skip_confirmation!
user.attributes = params
end
user
end
def registered
registrations = self.registrations
if registrations.count == 0
'None'
else
registrations.map { |r| r.conference.title }.join ', '
end
end
def attended
registrations_attended = self.registrations.where(attended: true)
if registrations_attended.count == 0
'None'
else
registrations_attended.map { |r| r.conference.title }.join ', '
end
end
def confirmed?
!confirmed_at.nil?
end
private
def create_person
# TODO Search people for existing email address, add to their account
build_person(email: email) if person.nil?
true
def attending_conference? conference
Registration.where(:conference_id => conference.id,
:user_id => self.id).count
end
def proposals conference
events.where('conference_id = ? AND event_users.event_role=?', conference.id, 'submitter')
end
def proposal_count conference
proposals(conference).count
end
def biography_word_count
if self.biography.nil?
0
else
self.biography.split.size
end
end
private
def biography_limit
if !self.biography.nil? && self.biography.split.size > 150
errors.add(:abstract, "cannot have more than 150 words")
end
end
end