Skip to content

Commit

Permalink
Remove validations from the Reminder model
Browse files Browse the repository at this point in the history
They are now moved to the form objects.
  • Loading branch information
your committed May 13, 2024
1 parent 543565e commit 238917e
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 59 deletions.
29 changes: 0 additions & 29 deletions app/models/reminder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ class Reminder < ApplicationRecord
attribute :sent_one_time_password_at, :datetime
attribute :one_time_password, :string, limit: 6

validates :full_name, on: [:"personal-details"], presence: {message: "Enter full name"}
validates :full_name, length: {maximum: 100, message: "Full name must be 100 characters or less"}

validates :email_address, on: [:"personal-details"], presence: {message: "Enter an email address"}
validates :email_address, format: {with: Rails.application.config.email_regexp, message: "Enter an email address in the correct format, like name@example.com"},
length: {maximum: 256, message: "Email address must be 256 characters or less"}, if: -> { email_address.present? }

validate :otp_validate, on: [:"email-verification"]

before_save :normalise_one_time_password, if: :one_time_password_changed?

scope :email_verified, -> { where(email_verified: true) }
scope :not_yet_sent, -> { where(email_sent_at: nil) }
scope :inside_academic_year, -> { where(itt_academic_year: AcademicYear.current.to_s) }
Expand All @@ -35,22 +24,4 @@ def itt_academic_year
read_attribute(:itt_academic_year)
)
end

def add_invalid_email_error(msg)
errors.add(:email_address, :invalid, message: msg)
end

def normalise_one_time_password
self.one_time_password = one_time_password.gsub(/\D/, "")
end

def otp_validate
return write_attribute(:email_verified, true) if otp.valid?

errors.add(:one_time_password, otp.warning)
end

def otp
@otp ||= OneTimePassword::Validator.new(one_time_password, sent_one_time_password_at)
end
end
30 changes: 0 additions & 30 deletions spec/models/reminder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
require "rails_helper"

RSpec.describe Reminder, type: :model do
context "that has a email address" do
it "validates that the value is in the correct format" do
expect(build(:reminder, email_address: "notan email@address.com")).not_to be_valid
expect(build(:reminder, email_address: "david.tau.2020.gb@example.com")).to be_valid
expect(build(:reminder, email_address: "name@example")).not_to be_valid
end

it "checks that the email address in not longer than 256 characters" do
expect(build(:reminder, email_address: "#{"e" * 256}@example.com")).not_to be_valid
end
end

context "that has a full name" do
it "validates the length of name is 100 characters or less" do
expect(build(:reminder, full_name: "Name " * 50)).not_to be_valid
expect(build(:reminder, full_name: "John")).to be_valid
end
end

context "when saving in the 'personal-details' validation context" do
it "validates the presence of full_name" do
expect(build(:reminder, full_name: nil)).not_to be_valid(:"personal-details")
expect(build(:reminder, full_name: "Miss Sveta Bond-Areemev")).to be_valid(:"personal-details")
end

it "validates the presence of email_address" do
expect(build(:reminder, email_address: nil)).not_to be_valid(:"personal-details")
end
end

describe ".to_be_sent" do
let(:count) { [*1..5].sample }
let(:email_sent_at) { nil }
Expand Down

0 comments on commit 238917e

Please sign in to comment.