# frozen_string_literal # Form object for authenticating <%= class_name %> records. # Automatically generated by the orthodox gem (https://github.com/katanacode/orthodox) # (c) Copyright 2019 Katana Code Ltd. All Rights Reserved. class <%= class_name %>Session include ActiveModel::Model include ActiveModel::Attributes INVALID_CREDENTIALS = "These credentials don't look valid" # ====================================================================================== # = Attributes = # ====================================================================================== ## # The authenticated <%= class_name %> if authentication is successful # # Returns ApplicationRecord attr_accessor :<%= singular_name %> ## # The provided email address String # # Returns String attribute :email, :string ## # The provided password String # # Returns String attribute :password, :string # ====================================================================================== # = Validations = # ====================================================================================== validates :email, presence: true, email_format: { allow_blank: true } validates :password, presence: true validate :record_authenticateable validate :password_matches private # If the record can be loaded from the scope, sets <%= singular_name %>, otherwise # adds errors to :base # def record_authenticateable <%= singular_name %> = <%= singular_name %>_scope.find_by(email: email) if <%= singular_name %>.present? self.<%= singular_name %> = <%= singular_name %> else errors.add(:base, INVALID_CREDENTIALS) end end # If the password isn't correct, add errors to :base def password_matches return unless <%= singular_name %> unless <%= singular_name %>.authenticate(password) errors.add(:base, INVALID_CREDENTIALS) end end # The scope to load <%= plural_class_name %> through. Modify this if there are # particular conditions that records should be filtered by. # # Returns ActiveRecord::Relation def <%= singular_name %>_scope <%= class_name %>.all end end