venue_management/app/models/concerns/venue_link_file.rb

110 lines
5.5 KiB
Ruby
Raw Normal View History

2020-11-12 13:51:31 +00:00
module VenueLinkFile
extend ActiveSupport::Concern
module VenueMethod
def get_trans(field)
class_name = self.class.to_s
if class_name == 'VenueManagementMain'
I18n.t("venue_management.#{field}")
else
class_name = class_name.underscore.gsub('venue_management','vm')
I18n.t("#{class_name}.#{field}")
end
end
end
included do
self.include VenueMethod
tmp = self.to_s.underscore.to_sym
VenueManagementFile.send(:belongs_to,tmp, :class_name => self.to_s, :foreign_key => "#{tmp}_id")
VenueManagementLink.send(:belongs_to,tmp, :class_name => self.to_s, :foreign_key => "#{tmp}_id")
VenueManagementReminder.send(:belongs_to,tmp, :class_name => self.to_s, :foreign_key => "#{tmp}_id")
self.has_many :venue_management_files, :autosave => true, :dependent => :destroy
self.accepts_nested_attributes_for :venue_management_files, :allow_destroy => true
self.has_many :venue_management_links, :autosave => true, :dependent => :destroy
self.accepts_nested_attributes_for :venue_management_links, :allow_destroy => true
self.has_many :venue_management_reminders, :autosave => true, :dependent => :destroy
self.accepts_nested_attributes_for :venue_management_reminders, :allow_destroy => true
self.fields.values.each do|v|
if v.type==Date || v.type==DateTime
self.field v.name+'_reminder', type: Boolean,default: false
self.field v.name+'_reminder_day', type: Integer
end
end
self.field :calendar_data,type: Hash,default: {type:{},key:[],page_id: {}}
self.field :calendar_dict,type: Hash,default: {}
def to_calendar_param
venue_main = self.class.to_s == 'VenueManagementMain' ? self : VenueManagementMain.find(self.venue_management_main_id)
self.case_no.to_s+'-'+self.id.to_s+'?method=show_'+self.class.to_s.underscore.gsub('venue_management_','').gsub('main','data')
end
def calendar_id(field)
self.calendar_dict[field]
end
self.has_many :venue_management_emails, :autosave => true, :dependent => :destroy
self.accepts_nested_attributes_for :venue_management_emails, :allow_destroy => true
VenueManagementEmail.send(:belongs_to,tmp, :class_name => self.to_s, :foreign_key => "#{tmp}_id")
self.before_save do
venue_main = self.class.to_s == 'VenueManagementMain' ? self : VenueManagementMain.find(self.venue_management_main_id)
calendar_keys = Array(self.calendar_data['key'])
(self.calendar_dict.keys - calendar_keys).each do |key_deleted|
event = Event.where(:id => self.calendar_dict[key_deleted]).first
event.model_class = nil
event.destroy
self.calendar_dict.delete(key_deleted)
end
if !calendar_keys.blank?
calendar_keys.each do |key|
key_id = self.calendar_id(key)
current_user_id = OrbitHelper.current_user.id rescue nil
in_use_locales = Site.first.in_use_locales
title_tp = {}
note_tp = {}
in_use_locales.each do |locale|
title_tp[locale] = venue_main['title'][locale] + ' ' + self.get_trans(key)
note_tp[locale] = self.case_no.blank? ? {} : I18n.t('vm_inviting.case_no') +': ' + self.case_no
end
update_dict = {key: key,model_class: self.class.to_s,model_id: self.id,update_user_id: current_user_id,calendar_type_id: calendar_data['type'][key],all_day: true,start: self.send(key),end: self.send(key),title_translations: title_tp,note_translations: note_tp,module_key: 'venue_management',model_page_id: calendar_data['page_id'][key]}
if key_id.nil?
self.calendar_dict[key] = Event.create(update_dict.merge(create_user_id: current_user_id)).id
else
2020-12-16 10:25:08 +00:00
calendar = Event.find(key_id) rescue nil
calendar.update_attributes(update_dict) if !calendar.nil?
if calendar.nil?
self.calendar_dict[key] = Event.create(update_dict.merge(create_user_id: current_user_id)).id
end
2020-11-12 13:51:31 +00:00
end
end
end
self.venue_management_emails.each do |venue_mail|
email = venue_mail.email
if !email.nil?
email.destroy
end
end
self.venue_management_emails = []
manager_emails = venue_main.manager_emails
self.fields.values.each do|v|
if v.type==Date || v.type==DateTime
if self.send(v.name+'_reminder')
title = venue_main.title + ' ' + self.case_no.to_s + ' ' + self.get_trans(v.name)
send_time = self.send(v.name)-self.send(v.name+'_reminder_day').day rescue nil
send_time = DateTime.parse(send_time.strftime("%Y/%m/%d 00:00:00 %z")) rescue send_time
if !send_time.nil?
new_email = Email.create(mail_to: manager_emails,
module_app_key:"venue_management",
template:"email/reminder_email.html.erb",
mail_sentdate: send_time,
mail_subject: title,
template_data:{'title'=>title,'send_time'=>self.send(v.name),'locale'=>I18n.locale})
self.venue_management_emails << VenueManagementEmail.new(email_id: new_email.id)
end
end
end
end
end
Email.before_destroy do
email = VenueManagementEmail.where(:email_id=>self.id).first
if !email.nil?
email.destroy
end
end
end
end