Sha256: 44ead21891e589021dd00cff1e154fa95cbe527bc38c44bafd18c98d6656c2e3

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

class <%= class_name %> < ApplicationRecord
  has_secure_token :session_token
  has_secure_password

  validates :email, presence: true, uniqueness: true
  validates :email, format: { with: /\A[^@\s]+@[^@\s]+\z/ }
  validates_length_of :password, minimum: 8, allow_blank: true

  before_validation do
    self.email = email.downcase.strip
  end

  after_update_commit do
    if self.email_previously_changed?
      EmailMailer.with(change: self.email_previous_change).changed.deliver_later
    end
  end

  after_update_commit do
    if self.password_digest_previously_changed?
      PasswordMailer.with(<%= singular_table_name %>: self).changed.deliver_later
    end
  end
<% if options.api? %>
  def signed_session_token
    Rails.application.message_verifier(:session_token).generate(session_token)
  end

  def self.find_signed_session_token(signed_session_token)
    if session_token = Rails.application.message_verifier(:session_token).verified(signed_session_token)
      find_by_session_token(session_token)
    end
  end

  def as_json(options = {})
    super(options.merge(except: [:password_digest, :session_token]))
  end
<% end -%>
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
authentication-zero-1.0.2 lib/generators/authentication/templates/models/model.rb.tt
authentication-zero-1.0.1 lib/generators/authentication/templates/models/model.rb.tt
authentication-zero-1.0.0 lib/generators/authentication/templates/models/model.rb.tt