osem-fcy/app/models/conference.rb

193 lines
6.3 KiB
Ruby
Raw Normal View History

2014-04-08 21:02:10 +02:00
##
# This class represents a conference
2013-01-07 09:04:09 +01:00
class Conference < ActiveRecord::Base
attr_accessible :title, :short_title, :social_tag, :contact_email, :timezone, :html_export_path,
:start_date, :end_date, :rooms_attributes, :tracks_attributes, :dietary_choices_attributes,
:use_dietary_choices, :use_supporter_levels, :supporter_levels_attributes, :social_events_attributes,
2014-03-03 11:04:47 +01:00
:event_types_attributes, :registration_start_date, :registration_end_date, :logo,
:questions_attributes, :question_ids, :answers_attributes, :answer_ids,
2014-03-03 11:04:47 +01:00
:difficulty_levels_attributes, :use_difficulty_levels,
:use_vpositions, :use_vdays, :vdays_attributes, :vpositions_attributes, :use_volunteers,
2014-05-29 20:23:33 +05:30
:media_id, :media_type, :color, :description,
:registration_description, :ticket_description
has_paper_trail
2013-01-07 09:04:09 +01:00
2014-02-09 18:05:57 +02:00
has_and_belongs_to_many :questions
2013-01-14 08:49:49 +01:00
has_one :email_settings, :dependent => :destroy
2013-01-07 09:04:09 +01:00
has_one :call_for_papers, :dependent => :destroy
has_many :social_events, :dependent => :destroy
has_many :supporter_registrations, :dependent => :destroy
has_many :supporter_levels, :dependent => :destroy
has_many :dietary_choices, :dependent => :destroy
2013-01-07 09:04:09 +01:00
has_many :events, :dependent => :destroy
has_many :event_types, :dependent => :destroy
has_many :tracks, :dependent => :destroy
2014-02-23 16:09:09 +02:00
has_many :difficulty_levels, :dependent => :destroy
2013-01-07 09:04:09 +01:00
has_many :rooms, :dependent => :destroy
has_many :registrations, :dependent => :destroy
has_many :vdays, :dependent => :destroy
has_many :vpositions, :dependent => :destroy
has_many :vchoices, :dependent => :destroy
2013-01-07 09:04:09 +01:00
belongs_to :venue
accepts_nested_attributes_for :rooms, :reject_if => proc {|r| r["name"].blank?}, :allow_destroy => true
accepts_nested_attributes_for :tracks, :reject_if => proc {|r| r["name"].blank?}, :allow_destroy => true
2014-02-23 16:09:09 +02:00
accepts_nested_attributes_for :difficulty_levels, :allow_destroy => true
accepts_nested_attributes_for :social_events, :allow_destroy => true
2013-01-07 09:04:09 +01:00
accepts_nested_attributes_for :venue
accepts_nested_attributes_for :dietary_choices, :allow_destroy => true
accepts_nested_attributes_for :supporter_levels, :allow_destroy => true
2013-01-07 09:04:09 +01:00
accepts_nested_attributes_for :event_types, :allow_destroy => true
2013-01-14 08:49:49 +01:00
accepts_nested_attributes_for :email_settings
2014-02-09 18:05:57 +02:00
accepts_nested_attributes_for :questions, :allow_destroy => true
accepts_nested_attributes_for :vdays, :allow_destroy => true
accepts_nested_attributes_for :vpositions, :allow_destroy => true
2013-01-07 09:04:09 +01:00
2013-01-17 15:56:56 +01:00
has_attached_file :logo,
:styles => {:thumb => "100x100>", :large => "300x300>" }
validates_attachment_content_type :logo,
:content_type => [/jpg/, /jpeg/, /png/, /gif/],
:size => { :in => 0..500.kilobytes }
2013-01-07 09:04:09 +01:00
validates_presence_of :title,
:short_title,
2014-04-05 21:44:30 +05:30
:social_tag,
:start_date,
:end_date
2013-01-07 09:04:09 +01:00
validates_uniqueness_of :short_title
2014-04-23 15:45:55 +02:00
validates_format_of :short_title, :with => /\A[a-zA-Z0-9_-]*\z/
2013-01-07 09:04:09 +01:00
before_create :generate_guid
before_create :create_venue
2013-01-14 08:49:49 +01:00
before_create :create_email_settings
2013-01-07 09:04:09 +01:00
def self.media_types
media_types = {:youtube => 'YouTube', :slideshare => 'SlideShare', :flickr => 'Flickr', :vimeo => 'Vimeo', :speakerdeck => 'Speakerdeck', :instagram => 'Instagram'}
return media_types
end
2014-04-08 21:02:10 +02:00
##
# Checks if the user is registered to the conference
#
# ====Args
# * +user+ -> The user we check for
2014-04-08 21:02:10 +02:00
# ====Returns
# * +nil+ -> If the user doesn't exist
# * +false+ -> If the user is registered
# * +true+ - If the user isn't registered
2013-01-07 09:04:09 +01:00
def user_registered? user
return nil if user.nil?
return nil if user.person.nil?
if self.registrations.where(:person_id => user.person.id).count == 0
2014-04-08 21:02:10 +02:00
logger.debug("User #{user.email} isn't registered to self.title")
return false
2013-01-07 09:04:09 +01:00
else
2014-04-08 21:02:10 +02:00
return true
2013-01-07 09:04:09 +01:00
end
end
2014-04-08 21:02:10 +02:00
##
# Checks if the registration for the conference is currently open
#
# ====Returns
# * +false+ -> If the conference dates are not set or today isn't in the
# registration period.
# * +true+ -> If today is in the registration period.
def registration_open?
today = Date.current
if self.registration_start_date.blank? || self.registration_end_date.blank?
false
end
(registration_start_date..registration_end_date).cover?(today)
end
2014-04-08 21:02:10 +02:00
##
# Checks if the call for papers for the conference is currently open
#
# ====Returns
# * +false+ -> If the CFP is not set or today isn't in the CFP period.
# * +true+ -> If today is in the CFP period.
2013-01-07 09:04:09 +01:00
def cfp_open?
today = Date.current
cfp = self.call_for_papers
if !cfp.nil? && (cfp.start_date.. cfp.end_date).cover?(today)
return true
end
return false
end
2014-05-28 13:40:58 +02:00
##
# Returns an array with the summarized event submissions per week.
# ====Params:
# * +weeks+ -> Integer with number of weeks. This is necessary to compare conferences.
#
# ====Returns
# * +Array+ -> e.g. [0, 3, 3, 5] -> first week 0 events, second week 3 events.
def get_submissions_per_week(weeks)
result = []
if call_for_papers && events
submissions = events.select('id, created_at').group_by { |t| t.week }
start_week = call_for_papers.start_week
sum = 0
(0..weeks - 1).each do |week|
if submissions["#{week + start_week}"]
sum += submissions["#{week + start_week}"].length
end
result.push(sum)
end
else
result = Array.new(weeks, 0)
end
result
end
##
# Checks if the conference is pending.
#
# ====Returns
# * +false+ -> If the conference start date is in the past.
# * +true+ -> If the conference start date is in the future.
def pending?
start_date > Date.today
end
2013-01-07 09:04:09 +01:00
private
2014-04-08 21:02:10 +02:00
##
# Creates a venue and sets self.venue_id to it's id. Used as before_create.
#
2013-01-07 09:04:09 +01:00
def create_venue
self.venue_id = Venue.create.id
true
end
2014-04-08 21:02:10 +02:00
##
# Creates a EmailSettings association proxy. Used as before_create.
#
2013-01-14 08:49:49 +01:00
def create_email_settings
build_email_settings
true
end
2014-04-08 21:02:10 +02:00
##
# Creates a UID for the conference. Used as before_create.
#
2013-01-07 09:04:09 +01:00
def generate_guid
begin
guid = SecureRandom.urlsafe_base64
end while Person.where(:guid => guid).exists?
self.guid = guid
end
end