venue_management/app/models/concerns/venue_link_file.rb

67 lines
3.0 KiB
Ruby

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")
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.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.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
self.venue_management_emails.each do |venue_mail|
email = venue_mail.email
if !email.nil?
email.destroy
end
end
self.venue_management_emails = []
venue_main = self.class.to_s == 'VenueManagementMain' ? self : VenueManagementMain.find(self.venue_management_main_id)
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
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'=>send_time,'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