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

Update for missing name and add tests #320

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ class User < ApplicationRecord

has_many :estimates
has_many :comments

# Returns the email if the user doesn't have a name in Github
def name
read_attribute(:name) || email
Copy link
Member

@JuanVqz JuanVqz Jan 23, 2024

Choose a reason for hiding this comment

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

is the email required/validated to be present? what if the email is nil?

Copy link
Member

Choose a reason for hiding this comment

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

@JuanVqz Users come from GitHub and we always collect their email address. So there is no way for the email to be nil.

@FionaDL On the other hand, this doesn't look very straightforward, why do we need the call to read_attribute?

I would recommend two different alternatives:

Suggested change
read_attribute(:name) || email
name || email

Or:

Suggested change
read_attribute(:name) || email
name.presence || email

Copy link
Member Author

Choose a reason for hiding this comment

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

@etagwerker I tested with your suggestions, but I think it is creating an infinite loop, I get "stack level too deep (SystemStackError)".

I think we could do self[:name] || email if it feels more straightforward. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

@FionaDL Oh, I understand why that would do that.

How about this instead?

  def name_or_email
    name || email
  end

Copy link
Member Author

@FionaDL FionaDL Feb 7, 2024

Choose a reason for hiding this comment

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

@etagwerker I could do that, but then we will have to be sure that name_or_email gets called everywhere that we would want to call user.name, right?

Copy link
Member

Choose a reason for hiding this comment

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

When it comes to presentation logic, I think it is usually best to have this sort of code in a view helper file.

So maybe it would be best to move this code to the application_helper file and later change the references in the view to this new method.

So yeah, that means that all view files that call user.name will now have to call something like display_name(user)

Copy link
Member Author

Choose a reason for hiding this comment

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

Hey @etagwerker I think I updated with what you are aiming towards.

end
end
12 changes: 12 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,16 @@
end
end
end
describe "Instance Methods" do
FionaDL marked this conversation as resolved.
Show resolved Hide resolved
describe "#name" do
it "returns the name a name exists" do
expect(subject.name).to eql subject.name
end

it "returns the email if the name doesn't exist" do
subject.name = nil
expect(subject.name).to eql subject.email
end
end
end
end