委 托

需要提供一个接口来判读用户今天是否已经签到
刚开始在customer_sign_in.rb中写了一个类方法,把customer当作一个参数传进去。
customer_sign_in.rb文件中
 def self.had_not_signin(customer)
    CustomerSigninLog.where(customer_id: customer.id).select {|log| log.created_at > Time.now.at_beginning_of_day } == []
 end

CustomerSignInStateDefine文件中
handler :customer_sign_in_handler do |input|
      CustomerSigninLog.add_signin_log(input[:customer]) if CustomerSignIn.had_not_signin(input[:customer])   //调用had_not_signin接口
      input[:customer].update(points: input[:customer].points + 1)
end


由于判断用户是否签到是判断customer_signin_logs表中是否有当天的签到记录,所以在customer_signin_log.rb中写了判断用户签到的类方法,但是这个方法真正的调用接口放在了customer.rb中。
customer_signin_log.rb文件中
def self._has_signin?(customer_id)
    where(customer_id: customer_id).select {|log| log.created_at > Time.now.at_beginning_of_day }.present?
end

customer.rb文件中
def has_signin?
      CustomerSigninLog._has_signin?(self.id)
end

CustomerSignInStateDefine文件中
handler :customer_sign_in_handler do |input|
      CustomerSigninLog.add(input[:customer]) unless input[:customer].has_signin?
      input[:customer].update(points: input[:customer].points + 1)  //调用has_signin?接口
end

猜你喜欢

转载自hjingfen.iteye.com/blog/2121788
今日推荐