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

User can only have 2 initials in UI #544

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Conversation

voodoo
Copy link

@voodoo voodoo commented Nov 13, 2024

Addresses: "You should create a method that takes user.name.initials as its argument. We are already properly extracting initials, we just want fewer of them. So make a helper like you’ve done that takes in all initials and returns a max of two. If there are more than two, keep the first and last initial and drop any in the middle."

I think this kind of works - but I've not done this type of process in a while so I expect it is not perfect ... yet.

…rns a max of two. If there are more than two, keep the first and last initial and drop any in the middle.
@voodoo
Copy link
Author

voodoo commented Nov 13, 2024

I'm looking to make a 'good' PR. So go over with a fine tuned comb and shout. I already see a couple things I would like to have done better. Not sure if the function should upcase for example.

Copy link
Contributor

@krschacht krschacht left a comment

Choose a reason for hiding this comment

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

Alright, you asked for it so I gave it a full review. :)

Gemfile.lock Outdated
@@ -437,6 +437,7 @@ GEM

PLATFORMS
arm64-darwin-23
arm64-darwin-24
Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t know what this line is doing, do you? I’m not opposed to adding it if it’s helping something, but if it’s just a stray line we should remove it.

Copy link
Author

Choose a reason for hiding this comment

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

That was added automatically. I just assumed cause I'm on a mac m1. I can take it out ... maybe :) I'll look into git-ing it out.

Copy link
Author

Choose a reason for hiding this comment

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

I removed the arm64-darwin-24. I think lol. Thanks for your patience.

@@ -11,5 +11,5 @@
<%= classes %>
"
>
<%= user.name.initials&.upcase %>
<%= only_user_initials(user.name.initials).upcase %>
Copy link
Contributor

Choose a reason for hiding this comment

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

The method name is a bit confusing. I find myself thinking, “but isn’t name.initialis” only the user initials? I don’t expect that to return something other than initials.

A better name might be: “only_two_initials”

Copy link
Author

Choose a reason for hiding this comment

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

only_two_initials makes great sense

@@ -1,4 +1,21 @@
module ApplicationHelper

def only_user_initials(initials, limit: 2)
letters = initials.to_s.downcase.strip.split("")
Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect this whole method can be a lot simpler. Probably this will work:

def …
return initials if initials.nil? || initials.length <= 2
initials[0] + initials[-1]
end

I haven’t tested that so I may be overlooking something, but I think that’s a simple way to grab the first letter and the last letter.

Copy link
Author

Choose a reason for hiding this comment

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

Damn that's tight. Works great

@@ -11,5 +11,5 @@
<%= classes %>
"
>
<%= user.name.initials&.upcase %>
<%= only_user_initials(user.name.initials).upcase %>
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, I think I’d keep the &.upcase rather than removing &, at least the method the way I wrote it returns a nil if it’s passed a nil, which seems like reasonable behavior

Copy link
Author

Choose a reason for hiding this comment

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

&.upcase on the method.

Copy link
Author

Choose a reason for hiding this comment

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

Will do.


test "only at most 2 initials with default limit" do
assert_equal "jz", only_user_initials("jdz")
assert_equal "pl", only_user_initials("Plllll")
Copy link
Contributor

Choose a reason for hiding this comment

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

It’s a bit unexpected that this method would change case. The way I wrote it preserves whatever case was passed in. I think that’s what most users would expect.

Also, this second test is bad because the fact that it returns “pl” does not confirm that it’s returning the first letter and the last letter. There are a lot of “l”s

Copy link
Author

Choose a reason for hiding this comment

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

No longer changing case. Took out a few l's.

Copy link
Author

Choose a reason for hiding this comment

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

True, does not change case now.

test "can have spaces" do
assert_equal "pq", only_user_initials("P vv qQ")
end

Copy link
Contributor

Choose a reason for hiding this comment

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

It’s worth adding a test for the nil case

Copy link
Author

Choose a reason for hiding this comment

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

Added a nil test.

@voodoo
Copy link
Author

voodoo commented Nov 13, 2024

Ok, I added those awesome/needed/helpful changes. I appreciate you doing that.

@voodoo voodoo closed this Nov 13, 2024
@voodoo
Copy link
Author

voodoo commented Nov 13, 2024

DAMN! Did no mean to CLOSE the PR.

@voodoo voodoo reopened this Nov 13, 2024
@voodoo
Copy link
Author

voodoo commented Nov 13, 2024

These code versioning tools are awesome but I feel like I'm running with scissors. I might have done something wrong - but I learn. Really, make any other observations.

@voodoo
Copy link
Author

voodoo commented Nov 14, 2024

I appreciate your patience and your coding skills. You can delete. I'll do better on the next one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants