Whoa, tomorrow I will have officially gone through half of the Lighthouse Labs Web Development bootcamp. It sounds like a cliche, but time really just flies.
After getting our assignment on Wednesday, “fml but funny and with photos”, Ashley, Fabien and I set about creating our app. The concept of “posts that people can upvote/down vote” was similar to our earlier assignment. We set up the Sinatra routes and database models similarly.
We initially had a comments table but decided we would only have votes because “who has time to write a comment anyways.” In this day and age of infinite scrolling, people are definitely more likely to just click “like” instead of commenting.
We built a photo wall where users could upload photos, and vote “funny” or “shameful” on a photo. We made a list of To Do tasks and used trello to pick up tasks as we went along. Having 3 boards: To Do, Doing, Done was really helpful and prevented people from being pigeon-holed into roles.
The biggest challenges I ran into were:
1. Using FactoryGirl to populate our database
Our original setup was simply to use image urls, so I had used FactoryGirl and Faker to generate random photo objects. However, Carrierwave’s mount uploader requires actual files.Carrierwave’s photo uploader would not accept strings for the file field, so we could not populate our database by simply passing image paths to FactoryGirl.
I had to do a “hack” around Carrierwave’s file requirement. In the Photo factory, I created an array of file names by using Dir.glob. I used “after_create” to add a file name to the file attribute of the object created by FactoryGirl. In order for the photos to not repeat, I used the pop method on the array.
By matching the number of times FactoryGirl’s create method was called to the number of files in our image directory, we could populate our database with photo objects for all the images.
A “hack” here we had to do was to turn off our file validation in order to populate the database this way. After populating the database, we could then turn the validation back on for any image uploads through the app. It’s really awesome to be able to set up all the photos with captions/votes in one file and run rake db:populate to set up our database!
2. Testing controllers with RSpec
Ok so writing tests for a midterm project was probably not the best idea. Since our models were so similar to the previous assignment, I thought writing simple validations for our models would be a good learning opportunity.
Well, “just testing models” turned into “create a test and development database with rake tasks to create and migrate them” + “test controllers” + “figure out how to test controllers with parameters passed in”… among other challenges.
Setting up the dependencies for the app test files was difficult. I put in so many require lines. Some “require”s that didn’t look needed but which would cause problems if removed. I learned how to test different get/post requests with Rspec by following redirects and looking at the last_response. In the end, I know my tests were not completely extensive (I could not figure out how to test photo uploads/routes related to photo uploads and sessions), but it was good to become a bit more familiar with Rspec. I used Faker and FactoryGirl for the tests and found them to be very convenient.
Tomorrow we’ll be organizing our photos and captions to get ready for presentations on Monday!