has_short_name
allows you to abbreviate user's names, hopefully in a
culturally sensitive way.
Add this line to your application's Gemfile:
gem 'has_short_name', github: 'mieko/has_short_name'
And then execute:
$ bundle
Or install it yourself as:
$ gem install has_short_name
class User < ActiveRecord::Base
has_short_name
end
m1 = User.create(name: 'Mike Owens')
# m1 is unique on first name
m1.short_name # => "Mike"
m2 = User.create(name: 'Mike Tyson')
# Notices that "Mike" is no longer unique
m2.short_name # => "Mike T."
# To ease confusion, we'll adjust all "Mikes" to the same level
User.adjust_short_names!
m1.short_name # => "Mike O."
m2.short_name # => "Mike T."
# Let's make it annoying
m3 = User.create(name: 'Mike Mikerson')
User.adjust_short_names!
User.all.pluck(:short_name) => # ['Mike O.', 'Mike T.', 'Mike M.']
You can specify the columns used as a source and destination of a short
name with :from
and and :column
:
class Usuario < BaseTable
# Use the 'nombre' column to generate 'short_nombre'
has_short_name from: :nombre,
column: :short_nombre
end
has_short_name
can be used on more than one set of [:from
, :column
]
tuples, if you find a reason to do so.
Sometimes you don't want a short name generated, for example, when you have a "name" field that can contain a human's name, or a company name. To prevent "Internet Widgets Pty." from being shortened to "Internet" or "Internet P.", you'll want something like:
class User
has_short_name only: -> { human? }
# Alternatively:
# has_short_name only: :human?
# Which is converted to the same
end
In this case, short_name_candidates
will only return a single name
, and
after negotiating short names, it'll always win out, implying
short_name = name
. If the user already has a short name assigned, that will
be the only candidate returned, so the following is pretty useful:
class User
has_short_name only: :should_assign_short_name?
private
def should_assign_short_name
# short_name_explicitly_assigned could be a column on the
# user table, and set to true if a user explicitly chooses
# their short name
human && !short_name_explicitly_assigned
end
end
has_short_name
comes with a list of default rules, which are executed in
order. (See HasShortName::DefaultRuleSet
). These currently are anglo-centric,
but I'd like to expand them. If you need a particular set of rules,
has_short_name
accepts a :rules
configuration option, which overrides the
defaults.
I'd really like contributions to this gem, as I only have a vague set of rules that hold for common, Anglo-centric names. A good set of defaults that hold more globally would be nice. If this gem fucks up your name, or should bail instead of abbreviating it, a patch, or even a descriptions of what the proper behaviour is appreciated.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request