Hitting the ground running on a Ruby Sinatra project
Introduction
The most difficult part of almost any project is starting. It is easy to get overwhelmed with all of the things you have to do, from deciding what you want to create to planning the execution. As a beginner developer, the last thing you want is all of your momentum to be sapped by the daunting task of devising the perfect architecture to your project. Instead, it would be better to leverage certain tools to help us with the more tedious tasks so our focus can be spent on developing a good product. In this blog, I want to highlight a couple of gems that made my life easier: Corneal and Faker. I will also be using Visual Studio Code (VSC) as my text editor. VSC is free to install and very user friendly.
The Corneal gem is incredible. It was created by Brian Emory, and he wanted something that could mimic Rails functionality of creating a project skeleton (important files and directory structure). Rails had a command that could be typed in the terminal which would create a skeleton, so Corneal served this purpose for Sinatra. The file structure is very similar to Rails and is a good introduction of what to expect next when making more involved web apps with Rails in the future.
Corneal file structure:
├── config.ru
├── Gemfile
├── Gemfile.lock
├── Rakefile
├── README
├── app
│ ├── controllers
│ │ └── application_controller.rb
│ ├── models
│ └── views
│ ├── layout.erb
│ └── welcome.erb
├── config
│ ├── initializers
│ └── environment.rb
├── db
│ └── migrate
├── lib
│ └── .gitkeep
└── public
| ├── images
| ├── javascripts
| └── stylesheets
| └── main.css
└── spec
├── application_controller_spec.rb
└── spec_helper.rbI suggest spending some time looking through the file structure as it will help you understand the logic behind some of the organization. At the very minimum you should recognize that the structure follows the MVC model that is often used in web development.
Before we dive into seeing how we can use the Corneal gem to help us get started with our project, I should mention that the gem was updated by an instructor Corinna Brock Moore at Flatiron school with some compatibility improvements. The bulk of the gem was unchanged but in order for everything to work smoothly, we will use the gem “Corneal-new”. For more details on what was changed, check out the Readme file on Github.
https://github.com/cjbrock/corneal-new
Using Corneal gem to set up your project
We first need to install the gem itself. Navigate to your terminal and type in the following command:
gem install corneal-newOnce it installs successfully we can run create our project with our desired name with the following command:
corneal new APP-NAMEWhere “APP-NAME” will be the name of your project.
You should see corneal make a bunch of directories in the terminal.
Now to access our newly made project folder, we will navigate to our project directory.
cd DemoAppThen we want to open it in VSC by typing the following command:
code .Once we have our project opened in VSC we can go ahead and test out if everything was done correctly by typing “shotgun” in the terminal to boot up a local server on our machine.
shotgunYou should see something like this:
Finally, to check and make sure the app is running on our server, we need to type the following into our browser:
localhost:9393If you see the above page, congratulations everything was set up properly!
Helpful tip: Don’t forget to stop the shotgun session when you are done viewing the page by pressing “CTRL + C” in the terminal.
Creating a database in our app
Now that we have a nice structure to our project we can start adding functionality. Since our app is nicely laid out in a Model View Controller (MVC) layout we can work in those individual folders piece by piece. I think a good place to start is with the Model portion of our app. It makes sense to create the database we will be working with which will drive the logic of how our controllers and views should be made.
Get Alisher Battalov’s stories in your inbox
Join Medium for free to get updates from this writer.
For my app, I will want to make a User model and a Scorecard model. We can have Corneal generate our models with the command:
corneal model UserThis command will make a file in your model folder with a class of User and it will be set to inherit from the ActiveRecord::Base class.
We can repeat this step for all of our models that our web app is going to have.
Once we have all of our models in place, we can go ahead and check out our database folder which is labeled “db”. This folder will contain our ActiveRecord migrations. Migrations are a feature of ActiveRecord gem that allows us to change the database over time.
We should have migration files for all of our models. Notice that we pluralize the name of the table because that table will store many users. Also when these files are made, they will have timestamps in the file name of when they were created.
Now we will add some columns to our table which will reflect specific attributes our models will have.
Helpful tip: Your class should inherit from the ActiveRecord version you want to use. I.E. ActiveRecord::Migration[5.2]
In our migration file, we put the code above to make the columns for our table. Our table structure will have rows which represent scorecards, and the columns which will represent course names, scores, and date.
Once we are satisfied with our table structure, we can go ahead and migrate our migration files and create our database. This can be done with the command:
rake db:migrateIf successful, you should see the lines below in your terminal.
Using the Faker gem
Now that our table is made, we will need some dummy data to fill in our table. That can be a pain and real straining on the imagination. Coming up with course names or random dates. Wouldn’t it be nice if we could just have something make up this data for us? Fortunately, there is! Here is where the Faker gem comes in. The best part is, the Faker gem comes with the Corneal-new gem and we can just go ahead and use it.
To make our dummy data we will create a “seeds.rb” file in our “db” directory. This file will be responsible for -you guessed it- “seeding” our data in our table.
Now for a little background on the Faker gem. This gem is great for getting fake data. If you go onto the Faker gem Github page it has a list of a bunch of different types of data from names to phone numbers. The gem even goes to the lengths of providing locations from fictional places like the world of harry potter or movie quotes from the TV Seinfeld.
Check out the Faker gem here: https://github.com/faker-ruby/faker
So, with a little ruby code, we can include these different types of fake data and create individual instances of our scorecard.
The code above uses a loop to create 10 scorecards which will have fake data populated for the class attributes using the Faker gem. The last puts line is used as a sanity check for us to know that the scorecards were created and a message is displayed in our terminal to notify us that everything worked.
To seed our database, type the command below in your terminal:
rake db:seedOnce you see the message “seeds planted!!!” in your terminal, you know everything worked as it should and you have fake data in your database. You can play around with the data by running the command:
rake consoleThis will open up a pry instance in the terminal and you can access attributes of your scorecards by typing commands such as “Scorecard.all” to show all scorecards or “Scorecard.find_by_id(1)” which will display the first scorecard in the table.
You can read more about accessing data in a database with ActiveRecord here: https://guides.rubyonrails.org/active_record_basics.html
Summary
Hopefully this was an informative guide on how to use the Corneal and Faker gems to save you some time on the tedious tasks and help you get coding your web app as quick as possible!



