Introduce class_names helper#37918
Merged
eileencodes merged 1 commit intorails:masterfrom Dec 10, 2019
joelhawksley:class_names_helper
Merged
Introduce class_names helper#37918eileencodes merged 1 commit intorails:masterfrom joelhawksley:class_names_helper
eileencodes merged 1 commit intorails:masterfrom
joelhawksley:class_names_helper
Conversation
As a follow-up to #37872, this change introduces a class_names view helper to make it easier to conditionally apply class names in views. Before: <div class="<%= item.for_sale? ? 'active' : '' %>"> After: <div class="<%= class_names(active: item.for_sale?) %>"> We've been using this helper in the GitHub monolith since 2016. Co-authored-by: Aaron Patterson <tenderlove@github.com>
Contributor
|
Thanks for this handy helper. We should add a changelog entry to this method. |
eileencodes
added a commit
that referenced
this pull request
Dec 11, 2019
Add changelog entry for #37918
abhaynikam
reviewed
Dec 16, 2019
| # class_names({ foo: true, bar: false }) | ||
| # # => "foo" | ||
| # class_names(nil, false, 123, "", "foo", { bar: true }) | ||
| # # => "foo bar" |
Contributor
There was a problem hiding this comment.
@joelhawksley The output mentioned in the docs in wrong. I tried it in one of the application and integer are added to the output string.
123.present? will not return false here(https://github.com/rails/rails/pull/37918/files#diff-3d863df5229eeafadb6df53160605a5bR348)
123 foo bar
Closed
inopinatus
added a commit
to inopinatus/rails
that referenced
this pull request
Feb 19, 2020
Taking the ideas introduced in rails#37872 and rails#37918, eliminating the use of a switch-on-class, and generalising the production of tags to any context. Passes an accumulator/result array to minimise allocations.
peteryates
added a commit
to x-govuk/govuk-form-builder
that referenced
this pull request
Oct 26, 2021
This improvement follows on from a recent PR on the components lib[0] where Rails' class_names[1][2] was used to improve the building of lists of HTML classes. Unfortunately it returns a space-separated string instead of an array - and the method it uses to actually build up the classes, `#build_tag_values`, is private, which makes utilsing it a bit of a pain. Instead here we're implementing a minimal version of it called `#build_classes`. It uses more-or-less the same class-building strategy as `#build_tag_values` but is less flexible in terms of args it accepts. [0] x-govuk/govuk-components/pull/264 [1] rails/rails#37918 [2] https://www.bigbinary.com/blog/rails-6-1-introduces-class_names-helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As a follow-up to #37872,
this change introduces a class_names view helper
to make it easier to conditionally apply class names
in views.
Before:
<div class="<%= item.for_sale? ? 'active' : '' %>">After:
<div class="<%= class_names(active: item.for_sale?) %>">We've been using this helper in the GitHub monolith
since 2016.
Co-authored-by: Aaron Patterson tenderlove@github.com