Create Status model0:00
In this video, we can add statuses, which is very similar to the last video where we added categories. So again, let's make a new model, php artisan make:model Status, and let's add everything. Okay. And the relationship is the same as categories. So Idea belongs to one Status, and a Status has many Ideas. So let's do that Idea. It's the same as category, so we can just duplicate this and change the name. So this is Status, and this belongs to Status. And for Status, has many Ideas.
Add migrations and foreign key0:38
So this is Status, and this belongs to Status. And for Status, has many Idea. So let's do that. Return this, has many Idea. Okay. Now, for the migration, go create Status. And again, just like Categories, we can just give it a name for now. So table, string, name. And we have to update our ideas table to add that Status. So I'll put an after category, and it's named status_id.
And we have to update our ideas table to add that status. So I'll put an after category, and it's named status_id. And that will look for the status table. So again, we have to put it before the idea migration, or we have to add the status before the idea migration. So let's grab this date, and let's put it before that. So I'll copy this, and let's change this date to that new date or one day before, actually. So let's say 24 for this one. Okay. And for our factory, again, we don't really need this because I'm going to hard code it.
Update factories and seed statuses2:06
Okay. And for our factory, again, we don't really need this because I'm going to hard code it. But let's add one anyways. So let's just copy what we have in Category and add it in Status here. Okay. And for IdeaFactory, we have to change this as well. So we have to add a status_id. And I will pick a number between one to five and make sure I'll have five categories available. So let's go to our Seeder here. And let's go ahead and create five categories here.
So let's go to our Cedar here. And let's go ahead and create five categories here. So I'm going to duplicate these ones. Let's add one more. And these ones should be statuses. So let's say status, and let's import that. And let's give it the five actual statuses that we have in our design. So open is the first one, considering is the second one, in progress, what else implemented and closed. Okay.
Display and eager load status3:13
and closed. Okay. So now, hopefully, this should migrate fresh seed, and it does. Let's double check our database. There should be statuses here. We have five, and the ideas should have a random status. So just like before, let's update our actual app to show the status here. And we'll work on the classes after we get it showing here. So right now, let's just make sure it's showing the correct status. So let's go to index for status, or sorry, it should be open is what I hard coded.
So right now, let's just make sure it's showing the correct status. So let's go to index for status, or sorry, it should be open is what I hard coded. This one right here. So there should be idea status name. Let's try that out. Okay. And it looks like it does work. But again, we have the N plus one issue. So let's make sure to eager load that relationship in our IdeaController. So User category and now status.
So let's make sure to eager load that relationship in our IdeaController. So User, Category, and now Status. Okay. And now that should fix the N plus one issue, and it does. And let's make sure we do it for the show page as well. So let me just grab this and make sure it's on the show.blade.php. That would be where is it right here? Space that in and let's make sure it shows. Okay. Now let's work on the styling for each of the Statuses here.
Style status badges4:54
Okay. Now let's work on the styling for each of the statuses here. So for example, closed should be red with white text. So it should look like this. So it should have BG red and text white here. So that's what this particular status should look like. So let's take a look at a few options of how to do that. So obviously this is dynamic. So I'm going to put it on the Idea model as a method. Let's say idea.
So I'm going to put it on the Idea model as a method. Let's say idea. Let's name it getStatusClasses as a method. And let's go ahead and add that on our Idea model. Let's make it down here. Say what I name it getStatusClasses. And we don't need a program. Let me just move this up.
And we don't need a program. Let me just move this up. So we have different cases here. So you might want to initially do a bunch of if statements or switch statements. So let's try that way first. So if so we have the $status so we can grab the $name if $status->name. And the first case is open. Then let's return bg-gray-200 because that's the class we need for this case. And then we can just start adding the other cases on with else if. So else if.
And then we can just start adding the other cases on with else if. So else if. Let's grab this again. And let's paste that in here. Next case is considering. And that case returns bg-purple, text-white and just keep adding cases for however many you need. So I'm just going to paste in the rest of the cases. OK so I added the rest of the cases here. And in case it doesn't match any of these cases then I just have a default here.
So let's refactor our code. Right now it's fine but we can clean this up by making it more declarative. So let's do that. So instead of using ifs and else ifs we can use a lookup table. So we can grab all the statuses. So all statuses is an array. And let's make the key the status and the value the classes. So bg-gray-200 for open. And I'll just paste the rest in. OK.
And I'll just paste the rest in. OK. So I added the rest of the cases. And now we can just return all statuses. And this status name. And that should work as well. But now our code is more declarative and also shorter. So we can get rid of this. It's commented out for now. Let's make sure everything still works.
It's commented out for now. Let's make sure everything still works. And it looks like it does. Make sure the show should still work. It's the same method. So that's fine as well. So let's get rid of this. But I'm going to take it even a step further. And a lot of people might not like this. But I'm going to store the classes as a column on the database.
And a lot of people might not like this. But I'm going to store the classes as a column on the database. So right now we're just mapping each of the statuses to a set of classes. So to me it makes more sense to get rid of this completely and just add it as a column. So let's go back here. Let's make a new column. Let's call it classes. And now in our seeder, we can add the classes here as a second field, say classes. And open is PGGrey200. And let me just add the other ones in.
And open is PGGrey200. And let me just add the other ones in. Okay, so I added the other ones in. And now instead of doing this, we no longer need to use this method and we can just grab it directly from our column. So Idea classes. And same for the show blade. php artisan migrate --fresh --seed and see if this works. Okay.
So let's migrate fresh seed and see if this works. Okay. And let me go back to the index page. Let me do a hard refresh here. And that doesn't seem to work. Sorry. Let's do the IdeaStatus classes. IdeaStatus classes. Okay. There we go.
Okay. There we go. Works on the index page and also on the show page. So we no longer need this method on the Idea class. Because like I said, I stored it directly on a database column. Now again, some people may not like this because we're storing presentation logic in the database. But to me, it shortens my code, and I don't see anything wrong with it. So I'm going to stick with this approach. Okay, and everything should still work.
Update and harden tests10:57
So I'm going to stick with this approach. Okay, and everything should still work. Okay, let's go ahead and test this. So we have to add statuses to our tests. So let's go back to our view tests or was it showTest, showTest, showIdeasTest. And now we have to add statuses here. So let's do that. I'm going to grab some statuses here from our seeder. So let's grab two here. Let's paste it in here after the categories or before, it doesn't matter.
So let's grab two here. Let's paste it in here after the categories or before, it doesn't matter. Let's import status. And let's give it a variable name. So $statusOpen equals this. And this one $statusConsidering, okay. Let's give it a statusId here. $statusId. So this one is $statusOpen. And same for ideaTwo.
So this one is status open. And same for Idea two. But status considering, okay. And now we can assert against those statuses. So be careful when you use assert, because that might result in false positives if that text appears somewhere else on the page. So for example, on this page, on the index page, we can see the status obviously in its correct place here, but also up here. So you see some of them here. So if I were to do assert considering, even though, or assert considering, even though,
So you see some of them here. So if I were to do assert considering, even though, or assert C considering, even though, for example, if I didn't add that status here, I made it the open status, it would still pass. So let me show you what I mean. So if I do response assert C, status considering name, this is going to pass even though none of our ideas have that status, because it shows up over here. So if we run that, it's still going to pass, because we have a false positive. So to fix that, you can be more explicit about what you see. So let me change this back to status considering.
So to fix that, you can be more explicit about what you see. So let me change this back to status considering. And instead of the name, I want to assert against the actual HTML element. So the second argument to assert is if you want to escape the strings. So in this case, let's say false. And it's actually give this the HTML element of that status badge. So for this one, it is the open status. So let's look for the HTML for that. So for one here, there's none here. Here, let's grab this.
So let's just copy the HTML, paste it in here. Actually in here. And this is more specific. So this should no longer be a false positive. So if I did this correctly, this one should pass. Okay. And it does. Let's just make sure it is correct. So I'm going to go back to my index.blade.php. I'm going to remove this.
So I'm going to go back to my index.blade.php. I'm going to remove this. And let's run the test again. And it should fail. Okay. So it does. Put it back, save, run it again. And it passes. Okay. So let's go ahead and add the other status here.
Okay. So let's go ahead and add the other status here. So it should be considering. So that should be, let's put it after the Idea two or the Category two. And it should be what's considering. Considering is purple. So bg-purple, text-white. And this text should be considering. Okay. Let's run that and it does pass.
Okay. Let's run that and it does pass. So we can do the same thing for show. You can even change the statuses if you want, but I'll just stick to these two. So I'll grab these two, let's put it in the second test. Actually, we only need one here. So I will just use one, the open one. And we can assert the same thing here. So the open one. Okay.
So the open one. Okay. And let's add that after here. Let's run that test. And it fails. I forgot to add the status here. So it's the same as above. First one, statusId is statusOpenId. So let's put it here and let's run that test again. Should pass.
So let's put it here and let's run that test again. Should pass. It does. And for the other tests, I think I just have to add categories to make sure there's no error. So let's grab this or we can put everything in a setup method, but I'm okay with adding it for each test for now. Let's paste that status in. Oh, sorry. I copied the wrong thing.
Oh, sorry. I copied the wrong thing. Should be a status. That is open. Let's put it here. And statusId. Okay. And same for the last test. I'll just make it the same status. Put it here and let's just grab that first status.
I'll just make it the same status. Put it here and let's just grab that first status. Put it here and we should be good. Let's run the entire file. And everything is passing. So we do have more work with statuses later, especially for the admin functionality where they can set the status here. But we'll work on that when we get there. So yeah, let's run the entire test suite again just to make sure everything is running or everything is passing.
So yeah, let's run the entire test suite again just to make sure everything is running or everything is passing. I mean, again, let's make a commit here. So this is episode 14. Add statuses. git add, git commit, episode 14, add statuses.
