Usernames for ActiveRecord model.
Table of Contents
- To Do
Username works with Rails 5 onwards. You can add it to your
And then execute:
Or install it yourself as:
$ gem install username
If you always want to be up to date fetch the latest from GitHub in your
gem 'username', github: 'jonhue/username'
Now run the generator:
$ rails g username
You can add usernames to an ActiveRecord model:
class User < ApplicationRecord include Username::Model end
Important: Make sure to add a
username string column to the corresponding database table.
Username adds a validator to prevent the use of invalid usernames.
# If a username is valid for an ActiveRecord class User.username_valid? 'test' # If a username is available for an ActiveRecord model User.first.username_available? 'test'
If you are using Devise and you want to allow your users to sign in using either their username or email address, here is how you'd do that:
- Pass the
deviseoption to the
class User < ApplicationRecord include Username::Model include Username::Model::Devise end
- Configure permitted Devise parameters in your
class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? def configure_permitted_parameters attributes = [:username, :email, :password, :remember_me] devise_parameter_sanitizer.permit :sign_up, keys: attributes devise_parameter_sanitizer.permit :account_update, keys: attributes end end
= simple_form_for resource, as: resource_name, url: session_url(resource_name) do |f| = f.input :login, required: true, autofocus: true = f.input :password, required: true
You can configure Username by passing a block to
configure. This can be done in
Username.configure do |config| config.forbidden =  end
forbiddenArray of forbidden usernames. Takes an array of strings. Defaults to
minlengthMinimum length for usernames. Takes an integer. Defaults to
maxlengthMaximum length for usernames. Takes an integer. Defaults to
regexStrings not matching this regular expression are invalid as usernames. Takes a regular expression. Defaults to
global_uniquenessWhether to validate usernames across all models or just the records model. Takes a boolean. Defaults to
Here is the full list of current projects.
To propose your ideas, initiate the discussion by adding a new issue.
We hope that you will consider contributing to Username. Please read this short overview for some information about how to get started:
Give the people some
Username follows Semantic Versioning 2.0 as defined at http://semver.org.
Copyright (c) 2018 Jonas Hübotter
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.