cft

Small Reviews and Sinatra: My Hobby, My Assignment and The Art of Good Looking Code

Sinatra is a Domain Specific tool to build web applications with Ruby.


user

Norberto Santiago

2 years ago | 5 min read

This blog was originally published on May 2nd, 2020 as part of my Sinatra project. In case you’re not familiar with Sinatra, is a Domain Specific tool to build web applications with Ruby.

Though I don’t know how many companies use it, it’s good for learning RESTful APIs. Just take a close look at the code to see the request types (get, patch, delete).

I also speak about how to organize well the code. What would be a good practice to make it look readable and good.

Like always, is it as technical as it is a personal inspiring story. Hope you enjoy it.

When deciding the theme for my projects, I often think if it would be something I would use. For example, my CLI project was all about looking for breweries in a city.

Cause that’s what I do, look for museums at day and breweries at night. I swear, I’ve probably seen more Claude Monet paintings than the total of IPA beers I ever had.

And also this one:

This time, I decided to take things further. Instead of something I would use. Why not do something I would DO for others? That’s when I decided to use my social media movie page as the theme for the Sinatra project.

Small Reviews which translates to Spanish as reseñitas (a quirk in Puerto Rico is to use the diminutive word for everything), started as a way to show my love for movies and distract myself from depression and post Hurricane Maria PTSD. I

As far as the code goes, is a simple project. You’re all LiveJournal probably looks better, but it does everything we need to have a full CRUD functional website. Being a reviews website the Controllers are Application Controller, Users Controller, Sessions Controller, and Reviews Controller. The typical Session Controller looks like this:

class SessionsController < ApplicationController

get "/signup" do
if logged_in?
redirect to '/reviews'
else
erb :"users/signup"
end
end

post "/signup" do
@user = User.new(:username => params[:username], :email => params[:email], :password => params[:password])
if @user.save
session[:user_id] = @user.id
redirect "/reviews"
else
redirect "/signup"
end
end

get "/login" do
if logged_in?
redirect to '/reviews'
else
erb :"users/login"
end
end

post "/login" do
@user = User.find_by(username: params[:username])
if @user && @user.authenticate(params[:password])
session[:user_id] = @user.id
redirect "/reviews"
else
redirect "/login"
end
end

get "/logout" do
session.clear
redirect "/"
end

end

What I would like to share is the reviews controller. Sinatra can look ugly, we can start with a conditional statement, then another statement, so on and so on to the point that hurts to watch. Thanks to a good friend in the cohort, we figure out how to make it better. Now, it looks like this:

class ReviewsController < ApplicationController

get "/reviews" do
@reviews = Review.all
erb :"reviews/index"
end

get "/reviews/new" do
erb :"reviews/new"
end

post "/reviews" do
category = Category.find_by(name: params[:category])
@review = Review.new(:title => params[:title], :content => params[:content], :user_id => params[:user_id], :category_id => category.id, :created_at => "#{Time.now}", :updated_at => "#{Time.now}")
if @review.save
redirect "/reviews/#{@review.id}"
else
redirect "/reviews/new"
end
end

get "/reviews/:id" do
set_review
if @review
erb :"reviews/display"
else
redirect "/reviews"
end
end

patch "/reviews/:id" do
set_review
category = Category.find_by(name: params[:category])
if @review && auth_user
@review.update(:title => params[:title], :category_id => category.id, :content => params[:content])
@review.save
redirect "/reviews/#{@review.id}"
else
redirect "/reviews"
end
end

get "/reviews/:id/edit" do
set_review
if @review && auth_user
erb :"reviews/edit"
else
redirect "reviews/error"
end
end

delete '/reviews/:id/delete' do
set_review
if @review && auth_user
@review.delete
redirect "/reviews"
else
erb :"reviews/error"
end
end

private

def set_review
@review = Review.find_by_id(params[:id])
end

def auth_user
@review.user.id == current_user.id
end

end
© 2021 GitHub, Inc.

Trust me, is way cleaner than it looked before. If you noticed I’m calling set_review and auth_user, it is through Private methods. Private methods are used like Helper methods to simplify your code. I recall doing Private methods in the lab but never used it again. We need to treat our codes like an art form and get creative with them. Make it look presentable and beautiful.

Take your time, be creative. Then we can celebrate like a Renoir painting.

Upvote


user
Created by

Norberto Santiago

Bilingual Software Engineer.


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles