Movie Reviewer: Learn-Verified Sinatra Project

This post is about “Movie Reviewer” a Web application I built for my Learn-Verified Sinatra assessment project. As the name implies, it is an application for Movie reviews where users can submit movies and write reviews about other movies. This is my first full-fledge application using the Sinatra web framework and I want to write about the process and a little bit about Sinatra. You can watch a demo of the app and see the code on Github.

Sinatra

Sinatra a is light-weight and unopinionated web framework written in Ruby, that allows quick building of web applications. It provides a great balance between simplicity and functionality for the developer. With a few lines of code as you see below, you can define routes and controller actions.

require 'sinatra'

get '/' do
  "Hello world"
end

Sinatra routes matches HTTP methods (get, post etc.) with a given URL pattern to render appropriate views or HTTP response. Read more about Sinatra on their intro page.

Project Setup

Sinatra let you structure your app however you want as long as you configure the app and reference files and directories appropriately. After a little tinkering I settled for a Rails-like folder structure.

app/
 |_ controllers/
 |_ models/
 |_ views/
db/
 |_ migrate/
 |_ schema.rb
public/
spec/
config.ru
Gemfile

Models and Associations

A Movie Review domain requires three basic models: a User, the Movie and Reviews. This project make use of Active Record, a great ORM (Object Relation Mapper) for mapping Ruby objects to database tables and building their relationships. Declaring model associations with Active Record is easy and intuitive.

Testing

The Learn-Verified program is heavily test-driving, and this has taught me the importance of testing and how to write tests. Testing helps you detect bugs early and well written test help you write better and extensible code. I used Rspec to for unit testing the models and Capybara for integration tests of the controllers and views.

Testing

Views and static files

For this project ERB is used for view templating, other options include Haml and Jade. First you need to tell Sinatra where to look for your views.

configure do
  set :views, 'app/views'
  ...
end

And for static JavaScript and CSS files you need to configure a public folder.

configure do
  ...
  set :public_folder, 'public'
end

Any file placed in the public folder is then served to the browser upon request. I used the Foundation CSS library to give the views some basic styling, because I am fairly comfortable using the Bootstrap library I decided to try out Foundation with this project.

Conclusion

I found Sinatra to be very flexible and it provided just the minimum tools to build a simple application from scratch. It is a balance between a bare-bones framework like Rack and a robust framework like Ruby on Rails.

Demo

Share this article.

Share Tweet Share
blog comments powered by Disqus