8000 Remove redundant safe navigation operator by tagliala · Pull Request #8528 · activeadmin/activeadmin · GitHub
[go: up one dir, main page]

Skip to content

Remove redundant safe navigation operator #8528

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

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/helpers/active_admin/display_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def DISPLAY_NAME_FALLBACK.inspect
def display_name(resource)
unless resource.nil?
result = render_in_context(resource, display_name_method_for(resource))
if result.to_s&.strip&.present?
if result.to_s.strip.present?
Copy link
Contributor Author
@tagliala tagliala Nov 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless I'm missing something and in some circumstances to_s can be nil (maybe override to_s, but that is a user problem)

It may happen in AR when you do something like

class MyModel < ApplicationRecord
  def to_s
    name
  end
end

and name is nil.

But this should not be expected and is hiding a problem

This should be implemented with delegate :to_s, to: :name

Feel free to reject this change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this update in #6342 but I don't recall why I used the safe navigation operator. Likely just out of caution without really knowing that nil.to_s will return an empty string so it is redundant as you say. This looks good to me and makes sense to change. Thank you.

ERB::Util.html_escape(result)
else
ERB::Util.html_escape(render_in_context(resource, DISPLAY_NAME_FALLBACK))
Expand Down
0