Files
epaper-rails7/app/models/e_paper_subscriber.rb

26 lines
854 B
Ruby
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
class EPaperSubscriber
include Mongoid::Document
include Mongoid::Timestamps
field :email, type: String
field :subscribed, type: Boolean, :default => true
field :language
field :last_paper_sent
before_save do
self.fix_email
# 修改點:用 throw(:abort) 取代 return false(Rails 5+ block 內不可用 return)
# 修改點:排除自己這筆(self.id.ne=>self.id),避免更新既有紀錄時把自己算成重複
if self.class.where(:id.ne=>self.id, :email=>self.email, :language=>self.language, :subscribed=>true).count != 0
throw(:abort)
end
end
def fix_email(save_flag=false)
# 修改點:加上 .downcase,統一大小寫,避免 ABC@Gmail.com 跟 abc@gmail.com 被當成兩筆
self.email = self.email.to_s.gsub(/[ ]/,'').sub(/;$/, '').downcase
self.save if save_flag
end
end