Rails class_names helper
class_names
is a neat helper method for CSS class names.
It merges the class names list into a string, supporting optional conditions. Very similar to the classNames
JS function.
The example from the PR demonstrates the way a new helper improves readability for a conditional class names generation in a view:
Before:
<div class="<%= item.for_sale? ? 'active' : '' %>">
After:
<div class="<%= class_names(active: item.for_sale?) %>">
class_names
is an alias to the more generic token_list
that must be a semantic generalization for non-CSS use cases.
References:
- Rails API docs:
ActionView::Helpers::TagHelper#class_names
Somewhat related:
- Comparing Rails routing configuration with git
- Stubbing environment variables in RSpec
- Fixing ActiveRecord::UnknownMigrationVersionError
- How to list all validators in a Rails model
- Reapplying Rails DB migration
- Customizing enum fields order for ActiveRecord
- Hot to change password manually with Rails and Devise