Sha256: f422026bbbb00eac99ea3088cffed39be19fc00c1402664c31276a1cd7a6b73e

Contents?: true

Size: 1.95 KB

Versions: 49

Compression:

Stored size: 1.95 KB

Contents

class AddUsernameToUser < ActiveRecord::Migration[6.0]
  class User < ApplicationRecord
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
    devise :database_authenticatable, :trackable, :validatable
    # TODO: If it works, these must be added to another gem one which deal 
    # more with sessions
    # devise :database_authenticatable
    # devise :rememberable
    # devise :trackable
    # devise :validatable
    # devise :timeoutable, timeout_in: 30.minutes 
    # REFERENCES
    has_many :role_users, dependent: :destroy, inverse_of: :user
    has_many :roles, through: :role_users, inverse_of: :users
    # VALIDATIONS
    validates :email, uniqueness: { case_sensitive: false }, presence: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
    validates :password, presence: true, on: :create
    validates :password_confirmation, presence: true, on: :create
    validate :check_password_and_confirmation_equal
    validates_each :admin do |record, attr, value|
      # Don't want admin == false if the current user is the only admin
      record.errors.add(attr, I18n.t("validation.errors.cannot_unadmin_last_admin")) if record.admin_changed? && record.admin_was == true && User.where(admin: true).count == 1
    end
  
    def display_name
      email
    end
  
    def has_role? role
      roles.include? role
    end
  
    protected
  
    def check_password_and_confirmation_equal
      errors.add(:password, I18n.t("validation.errors.password_and_confirm_must_be_the_same")) unless password == password_confirmation
    end
  end
  
  def change
    add_column :users, :username, :string
    add_index :users, :username

    puts "Computing username for existing emails"
    User.reset_column_information
    User.all.each do |u|
      puts "User: #{u.email} into #{u.email.split("@").first}"
      u.username = u.email.split("@").first
      u.save!
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
thecore_ui_commons-2.4.2 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.4.1 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.4.0 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.3.10 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.3.9 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.3.8 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.3.7 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.3.6 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.3.5 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.3.4 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.3.3 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.3.2 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.3.1 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.3.0 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.2.9 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.2.8 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.2.7 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.2.6 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.2.5 db/migrate/20200515070620_add_username_to_user.rb
thecore_ui_commons-2.2.4 db/migrate/20200515070620_add_username_to_user.rb