Investigating replyPath issue0:00
Now, we want to dedicate a whole lesson to implementing the leaderboard PR from John, so I'll save that for the next episode. For now, let's switch over to Issues. And hmm, let's do this one on top. Modify the replyPath method to generate the correct URL. Okay, so what's the issue here? replyPath method needs to be modified to generate the correct URL for paginated replies. Okay, this is something we can take care of. So let's go to replyPath. Yeah, so you can see here, like imagine this is the reply with an ID of, you know, 80 or
So let's go to reply path. Yeah, so you can see here, like imagine this is the reply with an ID of, you know, 80 or something like that. Well, we get the path to the thread. So that would be something like threads/channel/thread_slug, right? And then we add a hash onto it so that you can quickly scroll to that specific reply. So we would get something like this, right? However, what if that specific reply occurs on page 3? So we're paginating the replies, and on page 3 is when you would hit this specific one.
Adding failing unit test1:21
Yeah, this can get a little tricky. So why don't we take care of that? So let's get that fixed. I'm going to go to my ReplyTest. So these are the unit tests or model tests, if you want to call them that, specifically for a Reply. And at the moment, if we filter this down, yeah, everything's passing. So let's add a new one down here, and we'll say it generates the correct path for a paginated thread. How about that?
Thread. How about that? Okay, so in these situations, it can sometimes be a little tricky to figure out, okay, how do I write this test? And that's why I always recommend starting with comments. If you're a little blurry, just start speaking, and then you will kind of narrow in on what you need to do. So in this case, we might say something like, well, given we have a Thread, and that Thread has three replies, well, yeah, we kind of want to say something like, if we're paginating the replies, and let's make the pagination really low.
has three replies, well, yeah, we kind of want to say something like, if we're paginating the replies, and let's make the pagination really low. Something like, and we are paginating one per page. We want the minimum number per page, right, so that we don't have to generate an unnecessary number of replies. Okay, so if we have a Thread, and then there are three replies associated with that Thread, and then when we render the results, we are paginating them into sets of one, well, if we then, if we generate the path for the last reply, so that would be the third one, well, it should include page equals number three. So something like, it should include something like this, page equals three in the path.
Configurable pagination setting3:12
With my helper, the thread ID will be the thread we've already created, and specifically I want three of them. Next, we are paginating one per page. Yeah, so let's figure out how we're going to do this. So it sounds like, what you can see here, if we go to ThreadsController, we have this method called getThreads, and we're hard coding the pagination there. So ideally, we'll make that configurable, and that way we can also assume it and use it from the model. So instead, why don't we say config console.per_page, and we'll set a default there of 25, but we can also just set it right here.
So instead, why don't we say config console.pagination.per_page, and we'll set a default there of 25, but we can also just set it right here. So console, and we'll say, we could even put this under pagination, how about that? Something like this. per_page will be 25. Okay, so now we could say console.pagination.per_page. So when we run that, it will go to console.pagination.per_page, and that's 25. Now we can overwrite that. So for example, what I could say is here, we'll config console.pagination.per_page.
So for example, what I could say is here, we'll config console.pagination.per_page. We're going to set that to one. All right, next, if we generate the path for the last reply, okay, well, let's do this. Here's our replies, and we could say something like replies, well, remember, when you generate more than one reply with Laravel's factory class, you're going to get a Collection in return. So if I say replies.last, that will give me the very last item within that list. Okay, so let's generate a path for that, and yeah, really quickly, I just want to show you what we get.
Okay, so let's generate a path for that, and yeah, really quickly, I just want to show you what we get. We give that a run, and yeah, notice, here's what we have. So it did tack on the replyId, but once again, if we're paginating into sets of one, well, this should technically be on the third page. So we're missing page=3 there. So as a result, yeah, this is going to fail. Okay, let's bring this down, and we'll say this assertEquals, and something like this. Okay, so it should equal the path to the thread, so it should equal this, but then also page=3, and finally, the ID, so that we can quickly scroll to that specific reply.
Okay, so it should equal the path to the thread, so it should equal this, but then also page equals three, and finally, the ID, so that we can quickly scroll to that specific reply. All right, so yeah, I think that's what we want. Let's give it another run. Yeah, there we go. So now we have a good test here. We can see, all right, well, that's what we expected, but this is what the system is doing at the moment, so we need to fix that. Great. Yeah, so notice how the comments really did help us.
Calculating reply page number6:12
our full test, and I didn't have to fill in all the blanks on my own. I just took it one step at a time. It's useful. So now, yeah, here is what we're doing at the moment, and clearly, it's not working. So we need to figure out how do we determine which page we should be on. Well, let's see. One thing we need is we have to figure out perPage, and we can look in the config for that now. So I can say console.pagination.perPage. So yeah, at the moment, let's just write this out in comments so you can see.
So I can say console.pagination.perpage. So yeah, at the moment, let's just write this out in comments so you can see. All right, so if we want one per page, next we need to figure out, well, what is the position of this specific reply? So if you have a thread with 10 replies, I need to figure out, well, is this specific reply the fourth one in that list or the fifth? So what we could do there, well, let me show you something. Let's php artisan tinker. Let's say we have some things here, and we'll have name equals foo, name equals bar, and then finally, name equals bass.
Let's say we have some things here, and we'll have name equals foo, name equals bar, and then finally, name equals bass. All right? Let's say we want to find out the position of this one. Well, one thing we could do is say things, well, first, let's pluck all of the names. So, oh, I'm sorry, I didn't collect it, sorry about that. So we'll just say things equals collect(things). Okay, anyways, what I could do is say, well, give me all of the names there. So now we get a new Collection that contains only the keys there. And then I could say, well, search for bar.
So now we get a new collection that contains only the keys there. And then I could say, well, search for bar. And what search is going to do, much like php, is it's going to return the key that is associated with it. So one more time. There you go. Well, bar, that would be 0, 1, and that does return the correct value. If we do bass, so search for bass, that should return 2. And if we do search foo, that should return 0. So this should help, because we can now figure out where the position is for the current
And if we do search foo, that should return 0. So this should help, because we can now figure out where the position is for the current reply. Let's come on back, and we'll say, we'll get the thread, figure out all of its replies, grab me the ID, and that should be in the correct order. A higher ID will be a newer reply, so I think that should be okay. And if not, we can always just die and dump that real quick. Yeah, so you'll see that is in order. Now I can say search for the ID of the current reply. So let's say the ID is number 2, well, that's going to return 1.
Now I can say search for the ID of the current reply. So let's say the ID is number 2, well, that's going to return 1. Or if the ID is number 3, that's going to return 2. So we give that a run, and there we go. Okay, so now we can figure out the reply position. I hope that makes sense. Now, it's possible, by the way, that in your system, you're just storing that as a hardcoded value within your database. So reply maybe has a column that says position is 4, and that's fine as well, in which case it's significantly easier to calculate this.
So reply maybe has a column that says position is 4, and that's fine as well, in which case it's significantly easier to calculate this. But anyways, if we now have the count per page and then the position, well, this ends up being really easy at this point. Let's calculate the reply position divided by the number per page. So let's work on this together. Let's say the position is 3, and we want 1 per page. All right, well, that would mean this is not on the first page or the second page. It's on the third page. And 3 divided by 1 is 3.
All right, let's give this a run. But it's failing. Okay, so we are tacking on page there. We expected page number 3, but it actually returned page number 2. And that's because, yeah, we have to account for a zero index. Okay, I think that should do the trick. We'll give this a run, and it does return green. So that's one way to deal with this. We can talk about another way later. Let's come back and do one more assertion, though, because I want to be very sure this.
Updating controller pagination12:18
And refresh. It's not working. Oh, you know what? We switched to JavaScript-based, didn't we? Let's go into our Controllers, RepliesController. And there we go. Once again, we are hard-coding the pagination. Yeah. So once again, by extracting this, now we're being a little more consistent, because we could see in one place we got 25, and then in another place we got 20.
It worked. And if I look at the URL, it is correctly adding page equals 2. So that's great. And if we come back to our config, we bring that back to 25 there. If we come back and recalculate that, well, now it should be on page 1. And it is. So this is working. Let's go to GitHub and let's see what we've done. We've swapped out our pagination for a config item in two different places. The reply path method now properly determines the page number that should be included.
Committing and closing issue13:23
We've swapped out our pagination for a config item in two different places. The replyPath method now properly determines the page number that should be included. We've added this new section for pagination. And then replyTest looks like my editor automatically added public. And I'd rather not include those. So I will turn this off and instead just manually tack on the portion of the file that I want to commit. Okay. Let's come back and we'll say right here we are closing issue number 21. Correctly calculate the replyPath.
Discard the formatting there. And sync my changes. git push. So if I now come back to Safari, we'll give that a refresh. It is now closed. And if we take a look at it, we've fixed the issue.
