Skip to content

A rails gem providing naming methods for user models

License

Notifications You must be signed in to change notification settings

tomichj/user_naming

Repository files navigation

UserNaming

UserNaming is a simple gem that provides a 'name' column and some methods to standardize displaying user names in rails applications.

This isn't a big gem: it's a migration and a module with 4 or 5 methods that you include in your 'user' class. But, it lets me deal with user's names in the same way in every application I build. Over time, this pays off.

This gem currently supports rails 5 and beyond; adding 4.2 support is trivial, but would also require adding an appraisal configuration. Open for PRs if interested.

Installation

Add this line to your application's Gemfile and run bundle install to install it:

gem 'user_naming'

Then run the UserNaming install generator:

rails generate user_naming:install

If your user model is not User, you can (optionally) specify a user model with the --model flag. For example, if your user model class is Person:

rails generate user_naming:install --model Person

After you've run the install generator, apply the migration that was just generated:

rails db:migrate

The generator does the following:

  • Insert include UserNaming::User into your User model.
  • Create a migration to add a name column to your User table.

Usage

UserNaming supports setting the user's name as one field via name=.

Non-mutating accessors to read the user's name in different formats are provided:

  • name - the full name
  • first_name - always the first word of the name
  • middle name - everything that's not the first or last name
  • last_name - the last word of the name
  • initials - initials, joined without period or any other delimiter
  • first_name_last_initial - first name, last initial with a .

That's it!

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/tomichj/user_naming.

This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

About

A rails gem providing naming methods for user models

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published