osem-fcy/app/models/user.rb

130 lines
3.2 KiB
Ruby
Raw Normal View History

2013-01-07 09:04:09 +01:00
class User < ActiveRecord::Base
2014-06-23 19:07:50 +03:00
include Gravtastic
2014-06-24 12:14:53 +03:00
gravtastic size: 32
2014-06-23 19:07:50 +03:00
2013-01-07 09:04:09 +01:00
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
2014-06-18 16:58:30 +03:00
:recoverable, :rememberable, :trackable, :validatable, :confirmable,
:omniauthable, omniauth_providers: [:novell, :google, :facebook]
2013-01-07 09:04:09 +01:00
has_and_belongs_to_many :roles
2014-06-18 16:58:30 +03:00
has_many :openids
2013-01-07 09:04:09 +01:00
2014-06-18 16:58:30 +03:00
attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :role_ids,
2014-06-23 19:07:50 +03:00
:name, :email_public, :biography, :nickname, :affiliation
2014-06-24 12:14:53 +03:00
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
2014-06-23 19:07:50 +03:00
2013-01-07 09:04:09 +01:00
accepts_nested_attributes_for :roles
before_create :setup_role
2013-01-07 09:04:09 +01:00
2014-06-23 19:07:50 +03:00
validates :name, presence: true
2014-05-12 17:53:07 +04:00
2014-06-18 16:58:30 +03:00
# Searches for user based on email. Returns found user or new user.
# ====Returns
# * +User::ActiveRecord_Relation+ -> user
def self.find_for_auth(auth, current_user = nil)
user = current_user
if user.nil? # No current user available, user is not already logged in
user = User.where(email: auth.info.email).first_or_initialize
end
if user.new_record?
user.email = auth.info.email
2014-06-23 19:07:50 +03:00
user.name = auth.info.name
2014-06-18 18:05:28 +03:00
user.password = Devise.friendly_token[0, 20]
2014-06-18 16:58:30 +03:00
user.skip_confirmation!
end
user
end
2013-01-07 09:04:09 +01:00
def role?(role)
2014-06-24 12:14:53 +03:00
Rails.logger.debug('Checking role in user')
2014-05-10 16:09:24 +02:00
!!roles.find_by_name(role.to_s.downcase.camelize)
2013-01-07 09:04:09 +01:00
end
def get_roles
return self.roles
end
def setup_role
2014-06-23 19:07:50 +03:00
roles << Role.where(name: 'Admin') if User.count == 0
roles << Role.where(name: 'Participant') if roles.empty?
2013-01-07 09:04:09 +01:00
end
2014-06-23 19:07:50 +03:00
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?
2014-06-24 12:14:53 +03:00
user.password = Devise.friendly_token[0, 20]
2014-06-23 19:07:50 +03:00
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
2013-01-07 09:04:09 +01:00
end
def confirmed?
!confirmed_at.nil?
end
2014-06-23 19:07:50 +03:00
def attending_conference? conference
2014-06-24 12:14:53 +03:00
Registration.where(conference_id: conference.id,
user_id: self.id).count
2014-06-23 19:07:50 +03:00
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
2014-06-24 12:14:53 +03:00
2014-06-23 19:07:50 +03:00
private
2014-06-24 12:14:53 +03:00
def biography_limit
if !self.biography.nil? && self.biography.split.size > 150
errors.add(:abstract, 'cannot have more than 150 words')
2014-06-23 19:07:50 +03:00
end
2014-06-24 12:14:53 +03:00
end
2013-01-07 09:04:09 +01:00
end