seminar2/app/models/seminar_review.rb

60 lines
2.5 KiB
Ruby

# encoding: utf-8
require "orbit_form_helper"
class SeminarReview
include Mongoid::Document
include Mongoid::Timestamps
field :review_start_date, :type => Date, :default => Time.now #Already move to seminar_main
field :review_end_date, :type => Date, :default => Time.now #Already move to seminar_main
field :reviewer_id
field :seminar_signup_ids, :type => Array, :default => []
field :remove_seminar_signup_ids, :type => Array, :default => []
field :default_seminar_signup_ids, :type => Array, :default => []
field :default_topics, :type => Array, :default => []
field :default_topics_values, :type => Array, :default => []
belongs_to :seminar_main
after_initialize do
unless self.new_record?
if (self.reviewer_id.class == Array rescue false)
reviewer_ids = self.reviewer_id[1..-1]
reviewer_ids.each do |reviewer_id|
self.class.create(:reviewer_id=>reviewer_id,:seminar_main=>self.seminar_main)
end
self.reviewer_id = self.reviewer_id.first
self.save
end
end
end
before_save do
default_seminar_signup_ids = []
self.default_topics.each_with_index do |topic,i|
fn,id = (topic.split(".") rescue [])
next if fn.nil?
if fn == "seminar_signup_fields"
default_seminar_signup_ids += SeminarSignupValue.where(:seminar_signup_field_id=>id,:val.in=>self.default_topics_values[i].to_a).pluck(:seminar_signup_id)
elsif fn == "seminar_submission_fields"
seminar_signup_contribute_ids = SeminarSubmissionValue.where(:seminar_submission_field_id=>id,:val.in=>self.default_topics_values[i].to_a).pluck(:seminar_signup_contribute)
default_seminar_signup_ids += SeminarSignupContribute.where(:id.in=>seminar_signup_contribute_ids).pluck(:seminar_signup_id)
elsif fn == "seminar_signup_contributes"
default_seminar_signup_ids += SeminarSignupContribute.where(:description.in=>self.default_topics_values[i].to_a).pluck(:seminar_signup_id)
end
end
default_seminar_signup_ids.uniq!
default_seminar_signup_ids = default_seminar_signup_ids.map{|s| s.to_s}
self.default_seminar_signup_ids = default_seminar_signup_ids
end
def review
MemberProfile.find(self.reviewer_id) rescue nil
end
def all_seminar_signup_ids
seminar_signup_ids - remove_seminar_signup_ids + default_seminar_signup_ids
end
def append_default_signup_id(signup_id)
sid = signup_id.to_s
unless self.default_seminar_signup_ids.include?(sid)
self.default_seminar_signup_ids << sid
end
end
end