Sinatra’s Last Few

Abdelrahman Elsirafy
5 min readMay 27, 2021

For the second of five phases through our journey at Flatiron, our cohort was fortunate enough to learn and use Sinatra. I have yet to experience Rails but while learning about web frameworks, I feel fortunate to experience Sinatra’s lightweight and less “magical” convention. If there’s anything that helped me through this project, it was actually knowing why my code rendered it’s end result.

For my project build, I created a task tracker in which a user create, read, update, delete their to-do list in a sense but at the same time I wanted it to be universal enough to where a user could implement and use it however they please. What had started off as a simple idea for a weekly planner eventually expanded to become more dynamic in ways to something I actually look forward to using in the future. From a daily task list, to year long self-improvement goals, it was interesting and enjoyable writing out all the things this application could be.

Needless to say, it still involved inheriting from Active Record which does carry a lot of it’s own magic. The documentation for it was incredible and all the new methods discovered really left me smiling like a child on Christmas. Keep in mind, about five minutes prior I was ready to rip my hair out of frustration but it’s pre-written conventional methods was still really cool.

One of the most interesting things I came across were all the different ways to validate “truthiness”. I almost had a full page of code written while trying to establish that tried preventing an existing user in the database be written in and created twice if they shared the same username when signing up. Sure enough with a lot of reading I could establish the user attempted to be created and simply call “.valid?” on the value of that usernames attribute, and not only could it validate it for me with a boolean, I found that I could even call “.errors” on that same value and see exactly why it wasn’t validated in the first place. Just to make it even better, I could chain other methods on top of it to fish out the “full_messages” as to what was wrong with the data entered. What’s kind of funny is that one of my error messages for trying to create a user with a username that already exists, aren’t even my own word’s. I just use a Sinatra gem that let’s Active Record’s Validation method do the talking for me! I might geeky as hell to myself right now but what I took away building this project taught me a lot and I feel as if it’s the perfect Segway into a framework that’s as heavy and complex as Rails.

Speaking of which, a challenge for that came with using Sinatra with Active Record I’d have to say is it’s lack of resources when it comes to things like StackOverflow or other resources. I’m usually able to find, through any forums, someone with a like-minded approach to a problem and see responses that can at least nudge your problem-solving in the right direction. However, I’d say about 95% of those resources are referring to Rails and I could pick from it some that made sense or looked familiar, but a lot of it looking like hieroglyphics.

Anyways, in regards to building this Sinatra project, I can really appreciate it’s organized file structure between Models, Views, and Controllers. It’s almost like and art when all said and done as it comes together so beautifully. A great practice gained from this build and implemented was the Single-Responsibility Principle in which no area/ file of code was doing another’s job. The lines were most definitely blurred out at times but as you keep building, everything eventually finds it’s place. For example, you’d almost be inclined to put everything in an ApplicationController as it seems the easiest but by the end, even my sessions have their own controller and eventually you learn there’s nothing wrong with that. Sinatra from what I understand is actually one of the simpler file structures compared to other frameworks as it’s lightweight and convention never hurts. One quote that cemented in my brain since I read it; “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” — Martin Fowler. I just feel fortunate enough to have read that when I did.

Rake and Rack were extremely unique ways to implement code. One mistake I made multiple times was attempting for the initial migration to have all the attributes and data values I needed which kept sending me in a loop of a broken program. I then realized that either way “rake db:migrate” has to be run in the terminal and when it is, the same thing happens and it includes ALL the migrations you make throughout building the app. I also found it a lot easier to just create migrations from the terminal for two honest reasons. First, It’s foolproof (on my end at least), and second, sending commands from the terminal and things happening on it’s own like files appearing makes me feel really cool for some reason. Regardless I always want to code smarter, not longer, as it is one of my weaknesses that could use improvement. What’s odd is that starting this project, I had to delete two repositories from Github and start over because I kept trying to force code into migrations when I shouldn’t have and THEN tried rolling back my migrations and it was just an awful mess. Then by the end of this build, all of my migrations were created for me using Rake, and when I was almost done and “technically“ was meeting all of the requirements by the bare minimum, I just knew I wanted more from my and decided out of the blue that I wan’t users to be able to track their progress and have a means of seeing their completed task so I kept going by adding a migration to a task, not foreseeing how much longer or more complicated it might make my project but I knew it’s what I wanted. Not only was as simple as adding a boolean to the “tasks” of completed or not, but I had it working in time for me to even crush the project bonus. It’s not a familiar feeling being happy with myself and proud of my accomplishments but I strived and it turned out for the better. It was a bummer at first to hear that Sinatra was no longer in the curriculum. However looking at things now, I can’t help but feel like one of the most fortunate one’s to have experienced it.

--

--