Sha256: 44c3a37dbae41b06f5573dc467b256307d304efd410df9e043378ebe21468e30

Contents?: true

Size: 642 Bytes

Versions: 22

Compression:

Stored size: 642 Bytes

Contents

class User < ApplicationRecord
  # Rolify needs to be called before assigning default role due to a bug in Rolify
  # see: https://github.com/RolifyCommunity/rolify/issues/518#issuecomment-1218705389
  rolify

  after_create :assign_default_role

  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable

  validate :must_have_role, on: :update

  def assign_default_role
    self.add_role(:user) if self.roles.blank?
  end

  def role_names
    roles.distinct.pluck(:name)
  end

  def must_have_role
    if self.roles.blank?
      errors.add(:roles, "User must have at least one role")
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
rails_app_generator-0.3.6 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.3.5 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.3.4 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.3.3 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.3.2 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.3.1 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.3.0 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.2.43 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.2.42 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.2.41 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.2.40 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.2.39 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.2.38 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.2.37 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.2.36 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.2.35 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.2.34 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.2.33 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.2.32 after_templates/addons/rolify/app/models/user.rb
rails_app_generator-0.2.31 after_templates/addons/rolify/app/models/user.rb