class JamRuby::EventSession < ActiveRecord::Base belongs_to :user, class_name: 'JamRuby::User' belongs_to :band, class_name: 'JamRuby::Band' belongs_to :event validates :event, presence: true validates :pinned_state, :inclusion => {:in => [nil, 'not_started', 'over']} validate :one_of_user_band before_validation :sanitize_active_admin def has_public_mixed_recordings? public_mixed_recordings.length > 0 end def public_mixed_recordings recordings.select { |recording| recording if recording.has_mix? && recording.is_public? } end def recordings recordings=[] sessions.each do |session_history| recordings = recordings + session_history.recordings end recordings.sort! do |x, y| x.candidate_claimed_recording.name <=> y.candidate_claimed_recording.name end recordings end # ideally this is based on some proper association with the event, not such a slushy time grab def sessions if ready_display query = MusicSession.where(fan_access: true).where(created_at: (self.starts_at - 12.hours)..(self.ends_at + 12.hours)) if self.user_id query = query.where(user_id: self.user_id) elsif self.band_id query = query.where(band_id: self.band_id) else raise 'invalid state in event_session_button' end query else [] end end def ready_display self.starts_at && self.ends_at && (self.user_id || self.band_id) end def sanitize_active_admin self.img_url = nil if self.img_url == '' self.user_id = nil if self.user_id == '' self.band_id = nil if self.band_id == '' self.pinned_state = nil if self.pinned_state == '' end def one_of_user_band if band && user errors.add(:user, 'specify band, or user. not both') end end end