Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce class_names helper #37918

Merged
merged 1 commit into from Dec 10, 2019
Merged

Conversation

@joelhawksley
Copy link
Contributor

joelhawksley commented Dec 9, 2019

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

@rails-bot rails-bot bot added the actionview label Dec 9, 2019
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>
@joelhawksley joelhawksley force-pushed the joelhawksley:class_names_helper branch from 79b8adf to f24734a Dec 9, 2019
@eileencodes eileencodes merged commit 774e77c into rails:master Dec 10, 2019
2 checks passed
2 checks passed
build
Details
buildkite/rails Build #65456 passed (1 hour, 9 minutes, 13 seconds)
Details
@joelhawksley joelhawksley deleted the joelhawksley:class_names_helper branch Dec 10, 2019
@bogdanvlviv

This comment has been minimized.

Copy link
Contributor

bogdanvlviv commented Dec 11, 2019

Thanks for this handy helper. We should add a changelog entry to this method.

joelhawksley added a commit to joelhawksley/rails that referenced this pull request Dec 11, 2019
Per request from @bogdanvlviv.
eileencodes added a commit that referenced this pull request Dec 11, 2019
Add changelog entry for #37918
# class_names({ foo: true, bar: false })
# # => "foo"
# class_names(nil, false, 123, "", "foo", { bar: true })
# # => "foo bar"

This comment has been minimized.

Copy link
@abhaynikam

abhaynikam Dec 16, 2019

Contributor

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.