osem-fcy/app/models/event.rb

211 lines
5.4 KiB
Ruby
Raw Normal View History

2013-01-07 09:04:09 +01:00
class Event < ActiveRecord::Base
include ActiveRecord::Transitions
has_paper_trail
2014-06-24 12:14:53 +03:00
attr_accessible :title, :subtitle, :abstract, :description, :event_type_id, :users_attributes,
:user, :proposal_additional_speakers, :track_id, :media_id, :media_type,
:require_registration, :difficulty_level_id
2014-02-23 16:09:09 +02:00
2013-01-07 09:04:09 +01:00
acts_as_commentable
after_create :set_week
2014-06-24 12:14:53 +03:00
has_many :event_users, dependent: :destroy
has_many :event_attachments, dependent: :destroy
has_many :users, through: :event_users
has_many :speakers, through: :event_users, source: :user
2013-08-16 12:46:49 +03:00
has_many :votes
2014-06-24 12:14:53 +03:00
has_many :voters, through: :votes, source: :user
2013-01-07 09:04:09 +01:00
belongs_to :event_type
2013-08-16 12:46:49 +03:00
2013-07-11 07:43:52 +03:00
has_and_belongs_to_many :registrations
2013-01-07 09:04:09 +01:00
belongs_to :track
belongs_to :room
2014-02-23 16:09:09 +02:00
belongs_to :difficulty_level
2013-01-07 09:04:09 +01:00
belongs_to :conference
2014-06-24 12:14:53 +03:00
accepts_nested_attributes_for :event_users, allow_destroy: true
accepts_nested_attributes_for :event_attachments, allow_destroy: true, reject_if: :all_blank
2014-06-23 19:07:50 +03:00
accepts_nested_attributes_for :users
2013-01-07 09:04:09 +01:00
before_create :generate_guid
2013-01-16 08:22:20 +01:00
2013-01-07 09:04:09 +01:00
validate :abstract_limit
validate :biography_exists
2014-06-24 12:14:53 +03:00
validates :title, presence: true
validates :abstract, presence: true
validates :event_type, presence: true
validates :media_type, inclusion: { in: Conference.media_types.values }, allow_blank: true
2013-01-07 09:04:09 +01:00
2014-04-23 15:45:55 +02:00
scope :confirmed, -> { where(state: 'confirmed') }
2013-07-16 18:42:22 +02:00
2014-06-24 12:14:53 +03:00
state_machine initial: :new do
2013-01-07 09:04:09 +01:00
state :new
state :withdrawn
state :unconfirmed
state :confirmed
state :canceled
state :rejected
2014-06-06 10:23:01 +02:00
event :restart do
transitions to: :new, from: [:rejected, :withdrawn, :canceled]
2013-01-07 09:04:09 +01:00
end
event :withdraw do
2014-06-06 10:23:01 +02:00
transitions to: :withdrawn, from: [:new, :unconfirmed, :confirmed]
2013-01-07 09:04:09 +01:00
end
event :accept do
2014-06-06 10:23:01 +02:00
transitions to: :unconfirmed, from: [:new], on_transition: :process_acceptance
2013-01-07 09:04:09 +01:00
end
event :confirm do
2014-06-06 10:23:01 +02:00
transitions to: :confirmed, from: :unconfirmed, on_transition: :process_confirmation
2013-01-07 09:04:09 +01:00
end
event :cancel do
2014-06-06 10:23:01 +02:00
transitions to: :canceled, from: [:unconfirmed, :confirmed]
2013-01-07 09:04:09 +01:00
end
event :reject do
2014-06-06 10:23:01 +02:00
transitions to: :rejected, from: [:new], on_transition: :process_rejection
2013-01-07 09:04:09 +01:00
end
end
2014-06-23 19:07:50 +03:00
def voted?(event, user)
2014-06-24 12:14:53 +03:00
event.votes.where('user_id = ?', user).first
2013-08-16 12:46:49 +03:00
end
2014-06-23 19:07:50 +03:00
2013-08-16 12:46:49 +03:00
def average_rating
@total_rating = 0
2014-06-26 15:43:24 +03:00
votes.each do |vote|
2013-08-16 12:46:49 +03:00
@total_rating = @total_rating + vote.rating
end
2014-06-26 15:43:24 +03:00
@total = votes.size
2014-06-24 12:14:53 +03:00
number_with_precision(@total_rating / @total.to_f, precision: 2, strip_insignificant_zeros: true)
2013-08-16 12:46:49 +03:00
end
2013-01-07 09:04:09 +01:00
def submitter
2014-06-26 15:43:24 +03:00
result = event_users.where(event_role: 'submitter').first
2013-01-07 09:04:09 +01:00
if !result.nil?
2014-06-23 19:07:50 +03:00
result.user
2013-01-07 09:04:09 +01:00
else
2014-06-23 19:07:50 +03:00
user = nil
# Perhaps the event_users haven't been saved, if this is a new proposal
2014-06-26 15:43:24 +03:00
event_users.each do |u|
if u.event_role == 'submitter'
user = u.user
2013-02-06 20:17:54 +01:00
end
end
2014-06-23 19:07:50 +03:00
user
2013-01-07 09:04:09 +01:00
end
end
2014-02-23 16:09:09 +02:00
def as_json(options)
json = super(options)
2013-01-07 09:04:09 +01:00
2014-06-26 15:43:24 +03:00
if room.nil?
json[:room_guid] = nil
else
2014-06-26 15:43:24 +03:00
json[:room_guid] = room.guid
end
2014-06-26 15:43:24 +03:00
if track.nil?
2014-06-24 12:14:53 +03:00
json[:track_color] = '#ffffff'
else
2014-06-26 15:43:24 +03:00
json[:track_color] = track.color
end
2014-02-23 16:09:09 +02:00
2014-06-26 15:43:24 +03:00
if event_type.nil?
json[:length] = 25
else
2014-06-26 15:43:24 +03:00
json[:length] = event_type.length
end
json
end
2014-02-23 16:09:09 +02:00
2013-01-07 09:04:09 +01:00
def transition_possible?(transition)
2014-06-26 15:43:24 +03:00
self.class.state_machine.events_for(current_state).include?(transition)
2013-01-07 09:04:09 +01:00
end
2014-06-06 10:23:01 +02:00
def process_confirmation
2014-06-26 15:43:24 +03:00
if conference.email_settings.send_on_confirmed_without_registration?
if conference.registrations.where(user_id: submitter.id).first.nil?
2013-02-12 19:17:19 +01:00
Mailbot.confirm_reminder_mail(self).deliver
end
end
end
2014-02-23 16:09:09 +02:00
2013-01-07 09:04:09 +01:00
def process_acceptance(options)
2014-05-13 15:28:25 +02:00
if conference.email_settings.send_on_accepted &&
2014-05-13 15:27:43 +02:00
options[:send_mail].blank?
Rails.logger.debug 'Sending event acceptance mail'
Mailbot.acceptance_mail(self).deliver
end
end
2013-01-07 09:04:09 +01:00
def process_rejection(options)
2014-05-13 15:28:25 +02:00
if conference.email_settings.send_on_rejected &&
2014-05-13 15:27:43 +02:00
options[:send_mail].blank?
2014-05-13 15:24:21 +02:00
Rails.logger.debug 'Sending rejected mail'
Mailbot.rejection_mail(self).deliver
end
2013-01-07 09:04:09 +01:00
end
def abstract_word_count
2014-06-26 15:43:24 +03:00
if abstract.nil?
2013-01-07 09:04:09 +01:00
0
else
2014-06-26 15:43:24 +03:00
abstract.split.size
2013-01-07 09:04:09 +01:00
end
end
2014-02-23 16:09:09 +02:00
def week
created_at.strftime('%W').to_i
end
def self.get_state_color(state)
# default azure
result = '#00FFFF'
case state
when 'new' # blue
result = '#0000FF'
when 'withdrawn' # orange
result = '#FF8000'
when 'confirmed' # green
result = '#00FF00'
when 'unconfirmed' # yellow
result = '#FFFF00'
when 'rejected' # red
result = '#FF0000'
when 'canceled' # grey
result = '#848484'
end
result
end
2013-01-07 09:04:09 +01:00
private
def abstract_limit
# If we don't have an event type, there is no need to count anything
return unless event_type
2014-06-26 15:43:24 +03:00
len = abstract.split.size
max_words = event_type.maximum_abstract_length
min_words = event_type.minimum_abstract_length
errors.add(:abstract, "cannot have less than #{min_words} words") if len < min_words
errors.add(:abstract, "cannot have more than #{max_words} words") if len > max_words
2013-01-07 09:04:09 +01:00
end
def biography_exists
2014-06-26 15:43:24 +03:00
errors.add(:user_biography, 'must be filled out') if submitter.biography_word_count == 0
end
2014-02-23 16:09:09 +02:00
2013-01-07 09:04:09 +01:00
def generate_guid
begin
guid = SecureRandom.urlsafe_base64
2014-06-24 12:14:53 +03:00
end while self.class.where(guid: guid).exists?
2013-01-07 09:04:09 +01:00
self.guid = guid
end
def set_week
self.week = created_at.strftime('%W')
save!
end
2013-05-07 17:00:26 +02:00
end