orbit-eremail/app/models/email_er.rb

126 lines
3.5 KiB
Ruby
Raw Permalink Normal View History

2014-02-19 03:22:48 +00:00
# encoding: utf-8
class EmailEr
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
include OrbitCategory::Categorizable
include OrbitCoreLib::Preview
include OrbitModel::LanguageRestrict
include OrbitModel::Status
include OrbitModel::TimeFrame
include Tire::Model::Search
include Tire::Model::Callbacks
field :mail_subject
field :mail_sentdate , :type => DateTime
field :mail_content
field :create_user_id
field :update_user_id
field :is_checked, :type => Boolean, :default => false
field :is_pending, :type => Boolean, :default => true
field :is_rejected, :type => Boolean, :default => false
field :not_checked_reason
field :email_group, :type => Array, default: []
field :other_mailaddress
scope :can_display,where(is_hidden: false)
has_many :email_er_files, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :email_er_files, :allow_destroy => true
validates :mail_subject,presence:{message:'blank is not allowed'}
validates :other_mailaddress,
:length => {:maximum => 1000, :message => I18n.t("er_email.other_mailaddress_note_limit")}
def proc_check(check,not_pass_info = "")
self.is_checked = true
if check =="true"
self.is_rejected = false
elsif check == "false"
self.is_rejected = true
self.not_checked_reason = not_pass_info
end
end
def de_pending
self.is_pending = false
end
def de_pending!
de_pending
self.save!
end
def is_checked?
!self.is_pending && self.is_checked && (self.is_rejected == false)
end
def is_pending?
self.is_pending
end
def is_rejected?
!self.is_pending && self.is_rejected && (self.is_rejected == true)
end
def get_email_group_data(email_group_data)
group_mail = Array.new
self.email_group.each do |egroup|
if email_group_data.include?(egroup) and (egroup == 'e_0' or egroup == 'e_1' or egroup == 'e_2' or egroup == 'e_3')
#group_mail << "#{email_group_data[egroup]["email"]}(#{email_group_data[egroup]["name"]})"
group_mail << "#{email_group_data[egroup]["name"]}"
elsif email_group_data.include?(egroup) and egroup == 'e_4'
self.other_mailaddress.split(",").each do |otmail|
group_mail << "#{otmail}(#{email_group_data[egroup]["name"]})"
end
end
end
group_mail.join("<br />").html_safe
end
def to_preview
preview_object = Preview.new(:expired_at=>DateTime.now+30.minutes,:preview_at_link=>"panel_er_email_back_end_email_er_path",:object_class_type=>"EmailEr")
self.email_er["email_er_files_attributes"].each_with_index do |atr,idx|
preview_object.preview_files.build(:file=>self.email_er["email_er_files_attributes"][idx.to_s],:field_name_for_rebuild=>'email_er_files',:file_in_array=>true)
end unless self.email_er["email_er_files_attributes"].nil?
preview_object.object = self.email_er.except("email_er_files_attributes")
preview_object
end
protected
def self.email_group_data
@email_group_data = {
'e_0'=> {"name"=>I18n.t('er_email.email_group_data_0'), "email"=>"alluser@tea.ntue.edu.tw"},
'e_1'=> {"name"=>I18n.t('er_email.email_group_data_1'), "email"=>"allstu@tea.ntue.edu.tw"},
'e_2'=> {"name"=>I18n.t('er_email.email_group_data_2'), "email"=>"allgrad@tea.ntue.edu.tw"},
'e_3'=> {"name"=>I18n.t('er_email.email_group_data_3'), "email"=>"allad@tea.ntue.edu.tw"},
'e_4'=> {"name"=>I18n.t('er_email.email_group_data_4')}
}
end
end