Advertisement
VladikOtez

Model

May 16th, 2017
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 0.74 KB | None | 0 0
  1. class Card < ApplicationRecord
  2.   validates :original_text, :translated_text,
  3.              presence: true
  4.   validate :check_texts_for_equality
  5.   before_create :set_review_date
  6.   scope :needs_review, -> {where('review_date <= ?', 3.days.ago).order('RANDOM()')}
  7.  
  8.   def check_translation
  9.     self.original_text.downcase ==
  10.         self.translated_text.downcase
  11.   end
  12.   def set_new_review_date
  13.     if check_translation
  14.       self.review_date = Time.now
  15.     end
  16.   end
  17.  
  18.   private
  19.  
  20.   def check_texts_for_equality
  21.     if original_text.downcase.squish ==
  22.       translated_text.downcase.squish
  23.       errors.add(:original_text, message: "U Shell not Pass")
  24.     end
  25.   end
  26.  
  27.   def set_review_date
  28.     self.review_date = 3.days.since
  29.   end
  30.  
  31. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement