For the final project of the Flatiron School’s Learn-Verified program, we were challenged to build an app with a Rails backend API and an AngularJS front end. Going through the AngularJS part of the Learn-Verified program was the most challenging part of the course so I had no doubt this project would also be the most challenging. OO Ruby, Sinatra, and Rails all made sense to me very quickly. AngularJS, however, tripped me up a lot. I still have a lot to learn and I fully intend to supplement my AngularJS studying with more studying and building. Building this project was a big confidence booster though.
Here were the requirements for the project:
I’m a big air travel nerd so I decided to make an app for storing and displaying information about flights, airlines, and airports. The app is called “Hiko” which means flight in Japanese. Hiko is pronounced “he-ko” in Japanese.
The first thing I built for this app were all the Rails models, controllers, and serializers. I created 3 models and they associated with each other as such:
The Flight and Airport models have a slightly complicated set up because it was important for flights to know their origin and destination airports. Similarly, airports should know their departing and arriving flights. This way I can use calls like “airport.departures” or “flight.destination” to quickly access that information. This was enabled by having “origin_id” and “destination_id” attributes on the Flight model. “origin_id” and “destination_id” are actually just airport ids.
Another thing worth mentioning is that I installed the Geocoder ruby gem to geocode airports. This automatically grabs and stores the latitude and longitude coordinates for an airport. We’ll need this for rendering flight path maps with the Google Maps API. More on this later.
Controllers for these models were also built of course, but it’s not worth posting all that code. All you need to know is the controllers are only set up to render JSON data that the Angular app will consume.
Angular & Rails API Service
In the Angular part of my app, the first thing I wanted to get working was the API service to pull data from the Rails backend. I accomplished this by creating a service called ‘BackendService.js’. This service handles all the API calls for flights, airports, and airlines. Another option for this could have been building individual factories for each model, but I figured having one service was a nice way to consolidate and save code.
The BackendService doesn’t use separate functions for each model. Instead, it takes in a method parameter for which resource we’re accessing. So if I wanted to get all the flights in my database I would call “BackendService.allRecords(“flights”)” or call “BackendService.getRecord(“flights”, id)” to get an individual flight.
Check out the code for BackendService.js as well as an example use case below:
Rendering Nested Views
I implemented nested views in a few places. One of them was nesting the airline show and new form views in the airline index view. The table for displaying the full list of airlines is only 2 columns so I decided to make it a sidebar rather than a full content page. I stuffed the list of all airlines in a Bootstrap “col-md-4” column and then left the remaining space blank until a user clicked an airline’s name or clicked the ‘Add Airline’ button. Then the white space would be filled by the HTML for showing an airline’s information or creating a new airline.
The forms for adding airports and flights also use nested views that pop up only after a user clicks the appropriate button.
Searching & Filtering
Another requirement of this project was to be able to search and filter lists. For the filter, we were tasked with allowing users to filter based on some condition like a category of food, past due tasks, or in my case, flight dates. Hiko will store both historical and future flight information so I set up buttons for users to dynamically view all flights, past flights, or future flights on the flight index page.
Each filter button on the flight index page is bound to a FlightIndexController function called “filterDates” that can take in a function parameter of how the user wants to filter the flights. Originally, “filterDates” did all the work itself, but in order to make search work nicely with date filtering, I ended up delegating the work to another function called “makeList”.
“makeList” works by checking how the user wants to filter the flights and then pushes each matching flight into an array called “filteredList”. For example, if the user wants past flights the function will check to see if a flight’s departure date was before or after the current time. If it’s before, it pushes that into “filteredList”.
As for search, it proved a little tricky to get search and filters working well together. My original search implementation ignored any filter the user clicked and just searched all the flights. This was obviously not good because if the user clicked ‘Past Flights’ and then searched, they should only get search results that are considered historical flights.
In order to solve this, I kept track of what filter the user applied in a variable called “filterStatus”. By default, “filterStatus” is set to “future”. Each time the user clicked a filter, I updated this variable. See the above code in the “makeList” function for this.
The function that does the search work is called “searchList” and it calls on the “makeList” function while passing in the “filterStatus” variable. The “makeList” function returns a list of flights that is then searched/filtered based on the current search query.
Google Maps Flight Paths
The last thing I should discuss is how the Google Maps flights paths work. This wasn’t a requirement, but I though it’d be a fun thing to include. The Google Maps API is very well documented so building this really just involved customizing their documentation examples to my needs.
The first thing I tried with the Google Maps API was getting a generic map to render. For Angular, this involved storing a Google Maps object in $scope.map that took in some attributes like where the map should be centered and how far it should be zoomed in by default.
The next task was getting the map markers to show where the origin and destination airports are on the map. This is done by creating a new Google Maps Marker, telling the marker which map it belongs to, and setting latitude and longitude coordinates. I created 2 markers and saved them in “marker1” and “marker2”.
Next, we need to create a line or “Polyline” as Google calls them. Google Maps can’t do this by default so you must include the “geometry” library when you load the Google Maps API. Once you have the geomety library, you can create a new Google Maps Polyline that has customizable attributes like color, opacity, weight, which map it belongs to, and a boolean geodesic. Google Polylines normally draw a straight path as if the world is flat, however, flight paths are generally displayed by how they actually move around the curvature of the Earth so we need to set geodesic to true to get the nice arc.
Finally, we create the line by using the “setPath” function that belongs to a Google Maps Polyline object. This function takes in 2 pairs of coordinates and then draws the line on our map.
Check out the full code below which is stored in an Angular directive:
As I mentioned before, Angular was a very challenging thing to learn, but building this app taught me a ton and boosted my confidence. I still feel like there’s so much to learn though. To learn more about Angular, I’ve bought a couple books to go through and will also come back and improve this app. Some things I would like to add to this app would be user log in, mock flight purchasing, and flights having passengers.
If you’d like to see the full code for this project, see the GitHub repo here: https://github.com/alexbarron/hiko