Sinatra Project: Note-Worthy
Tags: sinatra, ruby, learning
Published: 2021-04-05 01:34:19

>As important as it is to keep striving and learning new things, it's also nice to look back at all the things that you've accomplished so far.

It's that idea that has led me to to create NoteWorthy, a Sinatra based web-app designed to log personal achievements. 

**This is not a full build tutorial, just an overview of some of the concepts involved in creating a Sinatra based web app**

##What is NoteWorthy?

NoteWorthy is a really simple app designed to mark and sort personal achievements. Whenever you do something interesting, personally satisfying or otherwise "note-worthy", you make a note of it in the app. Along with the name of the achievement, there are a couple metrics like difficulty and personal satisfaction which help to organize and sort the achievements.

When I was deciding what to build to practice Sinatra and Active Record, I was excited that I finally knew how to create a database and persist my objects. Unlike when I made PantryPal, the things created could (at least potentially) exist forever.  

#Creating NoteWorty with Ruby, Sinatra and Active Record

The first step in creating the app was to get the file structure sorted and load all the necessary gems. While it could be done manually, I used [Corneal](https://github.com/thebrianemory/corneal) to speed up the process considerably. Corneal sets up a good MVC file structure including a Gemfile, Rakefile, and database connection, and it also included some folders for testing. While I decided not to write or run any tests, I left most of the test environment setup in place. I also added a few extra gems like securerandom and rack-flash3. 

###The Models

There are only two models for NoteWorthy, Users and Notes. 

The User model represents a user and has a few attributes like first and last name, age and location. These are mostly cosmetic or, in the case of age, a placeholder for possible future updates (birthday achievement note). The remaining attributes are extremely important for the User to function properly. These are the email (which will function as the log-in) and "password digest" which will hold an encrypted version of the user's password.

The User model also has some methods to organize it's notes, like  the most_difficult method which finds the note with the highest difficulty rating. 

Notes represent the achievements a user logs into the app. They have several attributes that a user can set including a title, completion date and some details to help a user identify their notes later. It also has a satisfaction rating, and difficulty rating which are used to sort of quantify and organize the notes later.

#####A note on "notes"
Calling them notes is kind of weird because they're more like achievements that the user creates *after* something is done as opposed to *before* like the memo kind of notes that people usually think of. But, the idea is that you make a "note" of your "noteworthy" achievement, so that's what they're called.

###The Views

Both models have 5 views in common. They have slightly different names, but they are represent a view to create, view/read, update and delete each model, and a view for a model error. There are also two index views. there is a main index view for the app as a whole (which holds the login form) and there is a index view for Notes that shows a logged in user all their notes.

###The Controllers

There is a main application controller that inherits from Sinatra, configures where Sinatra looks for the views and sets up the session. The main controller also adds some helper methods to deal with some of the login logic for the other two controllers.  

The User controller manages the views and logic for the User model. This mostly means managing the sign-up process and user profile. It also handles deleting a user. Besides signing-up and logging in, the User Controller makes sure that a user is logged in before changing any users. It also will only allow the User that is currently logged in to be affected. 

The main functional part of the app is handled by the Notes Controller. The notes controller handles all the logic for logging achievements. It relies heavily on the current_user helper method from the Application Controller class to make sure that only the right Notes can be edited, and that created notes get assigned to the right User.

#Moving Forward

So I've learned a lot from doing this build. This was my first app that persists data to a database, and also my first app to use HTML and CSS, so I'm pretty excited about the results. It's my most usefully functional app, and I may even hook it up on a web server and make it public.

###Thanks for reading, I'll see you next time