clarion/app/models/user.rb

60 lines
2.0 KiB
Ruby
Raw Permalink Normal View History

2014-07-28 14:15:08 +03:00
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
2019-04-28 21:10:54 +03:00
:recoverable, :rememberable, :trackable, :validatable
2014-08-30 17:31:29 +03:00
has_many :personal_profiles, dependent: :destroy
2014-08-30 17:31:29 +03:00
has_many :lectures
has_many :workshops
2015-08-15 08:52:41 +03:00
has_many :propositions, foreign_key: :proposer_id
2019-04-28 21:10:54 +03:00
has_many :events, through: :propositions, source: :proposable, source_type: "Event"
has_many :feedbacks, through: :events
has_many :feedbacks_with_comment, -> { where.not(comment: [nil, '']) }, through: :events
has_many :participations, foreign_key: :participant_id
has_many :events_participated_in, through: :participations, source: :event
has_many :volunteerships, foreign_key: :volunteer_id
2015-08-05 17:17:04 +03:00
include FeedbackReceiving
def average_rating
return nil unless rated?
ratings_per_event = feedbacks.group(:feedback_receiving_type,
:feedback_receiving_id).average(:rating).values
BigDecimal(ratings_per_event.reduce(&:+)) / BigDecimal(ratings_per_event.size)
end
def find_or_build_personal_profile(conference, params = {})
current_profile = personal_profile(conference)
if current_profile.present?
current_profile.assign_attributes params
current_profile
else
build_personal_profile(conference, params)
end
end
def build_personal_profile(conference, params = {})
if personal_profiles.last.present?
new_personal_profile = personal_profiles.last.try(:dup)
CopyCarrierwaveFile::CopyFileService.new(personal_profiles.last, new_personal_profile, :picture).set_file
else
new_personal_profile = personal_profiles.build
end
new_personal_profile.conference = conference
new_personal_profile.assign_attributes params
new_personal_profile
2015-08-05 17:17:04 +03:00
end
def personal_profile(conference)
personal_profiles.find_by(conference_id: conference.id)
end
2014-08-31 14:57:34 +03:00
2014-09-18 01:07:09 +03:00
default_scope { order id: :desc }
2014-09-17 17:48:46 +03:00
def toggle_admin!
update admin: !admin
end
2014-07-28 14:15:08 +03:00
end