Sha256: 6b802c26c4eb89d570914e5dc3407ad15def50a367e58c1429c3d3eda6232601

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

require_relative './application_record'

class User < ApplicationRecord
  has_many :microposts, dependent: :destroy
  has_many :active_relationships, class_name:  "Relationship",
                                  foreign_key: "follower_id",
                                  dependent:   :destroy
  has_many :passive_relationships, class_name:  "Relationship",
                                   foreign_key: "followed_id",
                                   dependent:   :destroy
  has_many :following, through: :active_relationships,  source: :followed
  has_many :followers, through: :passive_relationships, source: :follower
  has_one :address

  before_validation :ensure_token

  scope :unverified, -> {where(verified: false)}
  scope :unsocial, -> {where(followings_count: 0, followers_count: 0)}
  scope :created_on, ->(date) {where("date(created_at) = ?", date.to_date)}

  private
    def ensure_token
      self.token = generate_hex(:token) unless token.present?
    end

    def generate_hex(column)
      loop do
        hex = SecureRandom.hex
        break hex unless self.class.where(column => hex).any?
      end
    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_hash_relation-1.4.1 spec/models/user.rb
active_hash_relation-1.4.0 spec/models/user.rb
active_hash_relation-1.2.0 spec/models/user.rb