Last time, we adjusted some code to make things easier to read and nicer to look at. Now we are going to move the code into our Rails project and make the first real commit to the project. By doing this preparation work today, we are close to utilizing Rails to begin showing tangible results beyond the command terminal.
First off, we need a place to put our newly developed code. Rails is set up using the MVC architecture. MVC stands for Models, View, and Controllers. Views display information to the user so this code obviously doesn't belong there. Controllers handles the flow of data and the passing of data from the model to the controller. Models handle the logic and computational backend of the data. If we treat the blockchain as a database, then it makes the most sense to put our relationships here since we are directly interacting with blockchain data.
As you can see I created a relationship.rb file inside of the models folder. The idea is to call our relationships from the controller and pass the data to the controller to eventually give to the user. Granted we will need to grab the content using the permlink in an intermediate step in the future, for now, we can focus simply on developing our relationships and passing the permlink along as if it were the final data.
I have mentioned in the past how the relationships in the previous posts would be translated into functions and today is the day we are doing just that. Inside our Relationship class, I have defined two different functions, one for each relationship. One function can be seen below.
As you'll quickly notice, the code is basically the same as it was when the code was in separate files. The only difference here is that we don't declare a specific account as that is now one of the arguments passed into the function. I have also added a depth parameter to make the number of results grabbed from the blockchain a flexible value rather than a hard-coded one.
To finish everything off, I committed the code for this project in a new branch labeled "basic-app" and pushed it up to the Github repository. It is good practice to develop features and fix bugs in separate branches from master as it makes master more reliable and stable and it allows the possibility of collaborate work in the future.
For this project, we'll develop a simple application and once that is done, merge it into master and deploy it to some server. But we are still awhile from that. For the near term future, we will continue to develop relationships and begin piping data through the MVC model to begin developing the application frontend.