Define Edited Comment Feature0:00
I thought you might like to come along as I implement this tiny little feature here. So you'll see we don't have anything live, this is only the design staging area. OK, so it looks like we need to track when a Comment is updated, and if it is, we need to display to the public a small little note that says, hey, this Comment has changed since the original publish date. OK, let's get started. I will visit the test for my Comment model. You'll see here are the handful of things that it can do. I'm going to add another one, though. Knows if it has been edited.
Write Model Test Case0:30
I'm going to add another one, though. Knows if it has been edited. OK, so here's what we'll do. We'll steal some of this here. We'll say, signIn. I'm not even sure if I need that, but we'll start with that. SignMeIn, and then create a Video with a single Comment. So I can now access that Comment through the comments relationship, or I have an extra relationship called latestComment. OK, so let's think about it.
Choose Edited Tracking Approach0:54
relationship called latestComment. OK, so let's think about it. Your first instinct might be to add a new column to the comments table, like edited. And this would be a Boolean. By default, it's false, but if the user changes the comment, you switch it to true. The next step would be to instead do editedAt. And this has a few bonuses, so I often reach for it. If you switch from a Boolean to a timestamp, not only do you get the benefit of a Boolean, but also you get the bonus of knowing the exact moment that the comment was edited. However, as you may know, there's no need to reinvent the wheel.
Add Edited Accessor1:24
but also you get the bonus of knowing the exact moment that the comment was edited. However, as you may know, there's no need to reinvent the wheel. All Eloquent models by default include a created_at and updated_at timestamp. So we already have everything we need here. Instead we might add an accessor that simply reads those timestamps. Something like this, edited. OK, so let's say by default it has not been edited. All right, so let's run this, and it is going to fail because we're getting null. Let's get that to work, and then we'll continue on with the test. We'll just do it right here.
Let's get that to work, and then we'll continue on with the test. We'll just do it right here. So this is an accessor, so I'll say getEditedAttribute, and let's think about it. Our table does have a created_at, so that's the original publish date, but it also has the updated_at. So we could check to see if they are the same, and let's think about it. If they are the same, that means the most recent update is the original publish date, which means it has not been updated. So let's tweak it to !=. So if they are not the same, that means at some point the Comment did receive an update.
So let's tweak it to not equals. So if they are not the same, that means at some point the Comment did receive an update. OK, let's start with that and see how it looks. So I'm going to give this a run, and it does return green. OK, so let's switch back to our test, and now we're going to update the Comment. So I'll say video, give me that latest Comment, and we'll do an update here, and we'll say changed. OK, so now as a result of that, the accessor should return true. But a common little gotcha is if you try this, it's still going to fail, and often this is related to a caching issue.
Fix Timestamp Test Flakiness2:54
But a common little gotcha is if you try this, it's still going to fail, and often this is related to a caching issue. So we've cached the most recent Comment on Video, but then we performed an update to the comment, which changes it, but we're still working with the cached comment. So let's get a fresh instance of the Video and then reload that relationship. But now if I give it a run, it's still failing. So this is going to be an issue related to how quickly phpunit runs. So let me show you this here. Let's say video, we'll get a fresh copy, reload,
So let me show you this here. Let's say video, we'll get a fresh copy, reload, and then all I'm going to do is cast that to an array, and then we'll die. I'm sorry, we'll dump and die it. So if we run it, you're going to see even though it did receive a change, the timestamps are still identical, and that's because the php execution is so fast. So you have three options here. I'll show you them in order. First, you might just say, well, let's manually sleep for one second. And that way, when we do update the comment, it will be one second later.
First, you might just say, well, let's manually sleep for one second. And that way, when we do update the comment, it will be one second later. I don't recommend this, but it's an option. So if we run it, you'll see we get green. But now that means for the lifetime of this website, this single test takes a minimum of one second. And it doesn't sound like much, but it actually is. Another option would be to force the updated_at timestamp as part of your update. So you could do this, and then you could say, now, and then add a minute, or add a day, whatever you want.
Add Edited Label UI5:11
So here's what I'll do. I'm going to switch from Safari to Chrome. And here you can see a working implementation of those comments. So let's get started adding it here. All right, back to phpStorm. I have a LessonComment view component. And you'll see, all right, here are the achievement badges. So yeah, right here, we could add another section. And I'm just going to hard code it to start. This is what I often do.
