Application_Form/app/models/application_form_signup.rb

177 lines
6.5 KiB
Ruby

# encoding: utf-8
class ApplicationFormSignup
include Mongoid::Document
include Mongoid::Timestamps
HiddenFields = ['application_form_signup_id', 'password', 'status', 'reviewer_ids_list','_id', 'created_at', 'updated_at','application_form_main_id',"serial_number","final_session","final_sessions","preferred_sessions",'application_form_session_id',"application_form_session_ids","preferred_session","sort_number","abstract_number","presentation_type"]
DefaultEnableFields = ['name','tel','phone','email']
field :sort_number , type: Integer, default: 10000
field :status
field :name # become Last Name for TICC
field :tel, type: String # become First Name for TICC
field :unit, localize: true
field :phone, type: String
field :fax, type: String
field :email, type: String
field :address, localize: true
field :password
field :note, localize: true
field :serial_number
field :final_session #only store old data , not used
field :final_sessions
field :preferred_sessions, type: Array, default: []
field :reviewer_ids_list, type: Array, default: []
def reviewer_ids_list
main = self.application_form_main
main.reviewer_enables.each_with_index.collect do |e,k|
if e
if main.reviewer_type_list[k]=='fixed'
main.reviewer_ids_list[k]
else
super()[k]
end
else
nil
end
end
end
belongs_to :application_form_main
field :application_form_session_id #only store old data , not used
field :application_form_session_ids, type: Array, default: []
has_many :application_form_signup_values, :autosave => true, :dependent => :destroy
has_many :application_form_signup_contributes, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :application_form_signup_values, allow_destroy: true
accepts_nested_attributes_for :application_form_signup_contributes, allow_destroy: true
scope :sort_ordered, ->{ order_by(:sort_number=>1,:created_at=>1) }
before_create do
unit = self.unit_translations.values.select{|v| v.present?}.first
self.unit_translations = I18n.available_locales.map{|l| [l.to_s,unit]}.to_h
if self.serial_number.nil?
s = ApplicationFormMain.find(self.application_form_main)
s.last_serial_number += 1
self.serial_number = s.last_serial_number
s.save(:validate=>false)
end
end
after_destroy do
application_form_main = self.application_form_main
if application_form_main.present?
application_form_reviews = application_form_main.application_form_reviews
id = self.id.to_s
application_form_reviews.each do |sr|
if sr.all_application_form_signup_ids.include?(id)
sr.application_form_signup_ids.delete(id)
sr.remove_application_form_signup_ids.delete(id)
sr.default_application_form_signup_ids.delete(id)
sr.save
end
end
if application_form_main.unassigned_application_form_signup_ids.delete(id)
application_form_main.save
end
end
application_form_session = ApplicationFormSession.where(:id=>self.application_form_session_id).first
if application_form_session
application_form_session.application_form_signup_ids.delete(self.id.to_s)
application_form_session.save
end
end
def display_serial_number
display_format_string(self.serial_number,4)
end
def display_format_string(num, str_length)
return format("%0#{str_length}d", num)
end
def set_preferred_sessions
self.preferred_sessions = self.application_form_signup_contributes.pluck(:preferred_session)
self.save
end
def get_display_fields
application_form = self.application_form_main
if application_form.nil?
return []
else
locale = I18n.locale.to_s
except_fields = ["password", "recaptcha"]
all_fields = []
if application_form.application_form_signup_field_sets.count != 0
all_fields = application_form.application_form_signup_field_sets.map do |set|
field_name = set.field_name
next if set.disabled || except_fields.include?(field_name)
value = self.send(field_name) rescue nil
next if value.nil?
if field_name == "status"
value = I18n.t("application_form.registration_status_#{value}")
elsif self.fields[field_name].options[:localize]
values = self.send("#{field_name}_translations").select{|k,v| v.present?}
value = ""
if values.count == 0
next
elsif values.count == 1
value = values.values.first
else
last_idx = values.count - 1
values.each_with_index do |(l, v), i|
value += (I18n.t(l) + ": " + v )
if i != last_idx
value += "<br>"
end
end
end
end
{
"field_name"=>field_name,
"title"=>set.name[locale],
"value"=> value
}
end.compact
else
field_names = self.class::DefaultEnableFields
all_fields = field_names.map do |field_name|
next if except_fields.include?(field_name)
value = self.send(field_name) rescue nil
next if value.nil?
field_name_trans = I18n.t("application_form_signup.#{field_name}")
if field_name == "status"
I18n.t("application_form.registration_status_#{value}")
elsif self.fields[field_name].options[:localize]
values = self.send("#{field_name}_translations").select{|k,v| v.present?}
value = ""
if values.count == 0
next
elsif values.count == 1
value = values.values.first
else
last_idx = values.count - 1
values.each_with_index do |(l, v), i|
value += (I18n.t(l) + ": " + v )
if i != last_idx
value += "<br>"
end
end
end
end
{
"field_name"=>field_name,
"title"=> field_name_trans,
"value"=> value
}
end.compact
end
application_form.application_form_signup_fields.asc(:_id).each do |rf|
application_form_signup_value = rf.application_form_signup_values.where(:application_form_signup_id=>self.id).last
if application_form_signup_value
value = application_form_signup_value.get_field_value rescue nil
if value
all_fields << value
end
end
end
return all_fields
end
end
end