Lessons: 19Length: 1.8 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

4.8 Adding Some Business Logic

Well, if you've been following along to this point, we've come pretty far and we're just about to the end. But what I'd like to do now is I would like to add in just a little bit of calculation, a little bit of business logic if you will, to be able to populate the amounts of the minerals and the nutrients that were putting into the ground for each of the fertilizer applications. Now, there's several different ways that we could do this, but I'm gonna create a little bit of a class, not really a model, but I'm gonna put it in there with the models. And I'm gonna create a new class that we can populate with a bunch of data that we can then pass in as the context to our view, and then be able to use that information here. That's going to include these calculations as well. So the first thing that I'm gonna do is I'm gonna go over to models. And I'm going to drop in a bit of code here. So let's go ahead and drop down here and let's put in a new class called ApplicationAmount. Now, this is really just a combination of a couple of different things. This is going to contain an application, so an instance of one of our application objects and also the calculated applied numbers of our values for N, P and K. So just kinda the class that I can put together and put all of these things into. So this is what I want to populate. So let's make sure that we have that saved, and then we can come back into our views. So we can start to do a little bit of work here, but there's gonna be a bit of calculation going on here. So let's give ourselves a little bit more room. And where this is all gonna take place is going to be in our index function, but what I would like to do though is make this a little bit easier on myself. And instead of putting all the logic in here, I'm gonna create a little function that is gonna be a bit of helper me. So I'm gonna define a function here and you can call it anything that you want, but I'm gonna call it get_application_amounts. So what I wanna do is wanna pass in an instance of an application and then I wanna do this calculations. Now, the calculations are gonna be based on the weight of the fertilizer bag as well as its coverage and then we'll do some calculation based on that. And then we'll send it back into our context. So let's first begin by getting out bag_weight from our application. But remember now, a fertilizer is a class or a model that's connected to our application. So what I can do is I can say application.fertilizer. And then I can say give me that bag_weight, cuz now I have that property. I can also get the bag_coverage, and I can say application.fertilizer.bag_coverage, so now I have those values. And now I wanna calculate the amount of pounds of fertilizer that are going to be associated with a thousand square feet. Because that's just gonna make our calculations a little bit easier. So I'm gonna call this pounds or lbs_per_k, so pounds per thousand, how many pounds of fertilizer am I putting down per thousand square feet? And this is once again, just gonna make my calculation a little bit easier. So I'm gonna use a round function and I'm gonna take the bag_weight. And I'm going to divide it by the bag_coverage and I'm gonna divide that bag_coverage by 1,000. Just so it's gonna make my numbers a little bit easier to work with. And I'm gonna around that to one decimal place. I think that should be good enough. So now I have the amount of fertilizer that I'm putting down per 1,000 square feet, and I can sorta calculate this values. So I'm gonna calculate my applied_n, my applied nitrogen. And I'm gonna once again, used my round function, and now I'm gonna say application.fertilizer.percent_n, so now I can take that percentage, that number. But that number, remember, is a whole number, so I'm gonna do my calculation and then I'm gonna wind up having to divide by 100 at the end. I'm gonna take that number and I'm gonna multiply it by my pounds per 1,000. And from there, I am simply going to divide that number by 100, and then I'm gonna give that two decimal places. Now, one interesting thing here is that if you recall, this percent is actually a decimal. When we created it, we created it as a decimal and based on this division right here in this rounding, this is gonna wind up being a floating point number. So I'm actually gonna have to do a little bit of conversion here. So I'm gonna do another little trick here, I'm gonna say, from decimal, I want to import decimal. This is gonna allow me to use a function to actually convert numbers to decimals. So what I can do here now is I can say, Decimal lbs_per_thousand. So that's gonna convert that to a decimal, so I can then multiply these two numbers together. And so once I've done this now for the nitrogen, I can do it for the others as well. So I'm simply gonna copy this number and I'm gonna do this twice. So we have applied_n, applied_p, and applied_k. Then I just have to make sure that I change these values as well to get the proper percentages and p, n, k, just like that. So now that I have those values, I wanna return a new instance of my ApplicationAmount class, which I already have imported here. So we simply come down here and say return ApplicationAmount, and we'll just pass all of these things that we wanna pass in the application. I wanna pass in applied_n, applied_p, and applied_k. So now I can have a little bit of a function here that is going to help me out with these calculations. So all I have to do is pass in an application, and then I can get out the value and I can stuff that into an array just like we've done before here and pass that in. So let's start to make those changes as well. So I'm going to say I have an application amounts array, which is initially empty. And then I want to loop through all of my applications. So I'll say for application in applications, like that. And then for each one of those, I'm simply going to say application_amounts.append. And I want to append the result of get application_amounts of application. So now every time I loop through that, I'm simply going to add a new record or a new instance to my application_amounts. And then I'm going to send this context to, and now I wanna send application_amounts instead of just application. Cuz we're gonna pass this through. And just to make its a little more clear, I'm gonna change this, this is gonna be ApplicationAmounts, like that, so let's go ahead and save that. So after we've done that now, we need to actually go back into our view and modify our view to understand application_amounts now. So it won't be too bad, but we're just gonna go ahead and copy this. We'll go back into our index view. And now in here, instead of dealing with applications, we are dealing with these ApplicationAmounts. So let's go back in there, and we'll say, now we're in applicationAmounts. We're gonna have each one of those be called applicationAmount, and we'll go ahead and copy. And now these things are still valid, I'm just gonna have to preface everything here with applicationAmount so those things will still show up. And then, I now I have values down here for n, p, and k. So I'm simply going to drop these in here as well. So I'll say ApplicationAmount.applied_n. And then just to make my life a little bit easier, I will copy these values, and paste them over, just like that, and we'll change this to n, p, and k. So let's go ahead and save all of those changes, and if everything has worked correctly, I can come back. And now, I have my calculated values. So if expand this out a little bit more, you now can see that when I apply products of a certain bag size, or a certain number of bags on a certain date, I'm also calculating the amount of these nutrients into the ground. And now I can see that I have 0.86 of a pound of nitrogen, 0.58 pounds of phosphorus and potassium. And this might seem like nothing all that important. But when it comes to lawn care and taking care of your soil and the grass, these things are very important for not only homeowners but also for professional companies that do this for a living. So now, you can see I've been able to add in some custom business logic with some calculations and a couple little helper functions here and there. But I've been able to get it in there. So to this point, I think you've done a fairly good job of following along. We've been able to create a nice little app for ourselves where we can kinda start to work with things and start to get this thing up and running. But right now, we're running a little low on style. And so in the next lesson, I wanna show you how to start to integrate the concept of style with your own custom CSS or even things like Bootstrap and how to get those types of front end libraries and styling into your application.

Back to the top