ask/app/models/ask_question.rb

69 lines
2.4 KiB
Ruby
Raw Normal View History

2014-10-02 06:00:35 +00:00
class AskQuestion
include Mongoid::Document
include Mongoid::Timestamps
2014-10-23 07:03:21 +00:00
# include ActiveModel::Validations
2014-10-02 06:00:35 +00:00
include OrbitCategory::Categorizable
# include OrbitTag::Taggable
2014-10-02 06:00:35 +00:00
# 欄位
2022-08-18 03:07:45 +00:00
field :serial_number, type: Integer
field :ip, type: String
2014-10-02 06:00:35 +00:00
field :name, type: String
field :identity, type: String
field :mail, type: String
field :phone, type: String
field :fax, type: String
field :title, type: String
field :content, type: String
2016-11-07 02:08:04 +00:00
field :appointment, type:DateTime
field :sex
2014-10-02 06:00:35 +00:00
field :reply, type: String
field :comment, type: String
2014-10-23 07:03:21 +00:00
field :situation, type: String, default: "is_waiting" #預設待處理
2014-10-02 06:00:35 +00:00
field :send_email, type: Boolean, default: false
field :email_id
field :custom_values, type: Hash,default: {}
2020-04-30 05:18:02 +00:00
field :agree_show,type: Boolean,default: false
2020-07-04 13:36:32 +00:00
field :agree_usage,type: Boolean,default: false
2014-10-23 07:03:21 +00:00
# validates_presence_of :name, :identity, :mail, :title, :content
2022-08-18 03:07:45 +00:00
before_create do
last_serial_number = AskSetting.update_last_serial_number
self.serial_number = last_serial_number
end
2014-10-02 06:00:35 +00:00
def email
mail = Email.find(self.email_id) rescue nil
end
def email_address
email_address = AskAdmin.pluck(:email).select{|s| s.present?}.uniq rescue []
authorizes = Authorization.where(:module_app_id=>ModuleApp.where(:key=>'ask').first.id).to_a rescue []
authorizes.each do |a|
if a.category_id
next if a.category_id != self.category_id
end
if a.user_id
u = a.user
email_address << u.email if u && u.email
elsif a.role_id
email_address = email_address + MemberProfile.where(:role_ids=>a.role_id).pluck(:email).select{|s| s.present?}.uniq
else
a.destroy
end
end
email_address = email_address +[self.mail] if !self.mail.blank?
2022-10-17 09:18:34 +00:00
email_address = email_address.flatten
email_address.uniq
2022-08-18 03:07:45 +00:00
end
def get_serial_number(last_serial_number=nil, display_length=nil)
if display_length.nil? && last_serial_number.nil?
can_update_shared_hash = (defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash)
last_serial_number = (can_update_shared_hash ? OrbitHelper::SharedHash["ask"][:last_serial_number] : AskSetting.pluck(:last_serial_number)[0].to_i)
end
display_length = [last_serial_number.to_s.length + 1, 4].max if display_length.nil?
display_format_string(self.serial_number, display_length)
end
def display_format_string(num, str_length)
return format("%0#{str_length}d", num)
2014-10-02 06:00:35 +00:00
end
end