Previously, I talked about making first steps with Sinatra, how to prepare and configure everything to get the app running similarly to Rails. This time, I’m going to show you how to make the app as full-stack, so you could use it instead of Rails.
Every app sooner or later needs to store credentials somewhere. To do it so we can choose between at least two options.
I’ve already mentioned about Sinatra:
We need to register the extension, define the config file and use one of the keys stored in the YAML file by invoking settings and the key.
Another solution is a gem called settingslogic.
After adding it to Gemfile, you need to create Settings class, set a path to the config file and define a specific namespace, related to the app’s environment.
Which one is better? Well, Sinatra’s extension seems to be a bit faster to get done but, in my opinion, accessing nested keys is slightly uncomfortable, because you need to type brackets. In case of settingslogic, you shouldn’t forget about loading the class before autoloader, to have all keys available.
Yeah, that’s about time to introduce a database to the app. I’m pretty sure you know ActiveRecord, don’t you? There’s a gem called Sinatra ActiveRecord Extension. At the beginning let’s add some gems that we need. We’re going to use PostgreSQL by the way.
Now, we should register the extension.
Of course we shouldn’t forget about the DB config file, at config/database.yml.
Because the gem offers some rake tasks as well, we have to create Rakefile and put into that the following lines:
What’s now? You can simply create your first table. Type:
And you will see first files in db directory. All other stuff related to ActiveRecord works the same as in a Rails app.
As I said at the beginning, we’re going to build a full-stack app. I’ve decided to use HAML with Bootstrap, so let’s configure it. Firstly, add gem ‘haml’ to Gemfile and the following lines to your main application file.
The first line says we’re going to use HAML with HTML5 format. Layout key says that the main layout file is called application, so application.haml file should be located in the main view directory. When it comes to the directory there’s the second line which slightly changes the default behavior. We want to have our views directory the same as in Rails so we create it within app directory.
What’s next? Let’s use it in practice!
To choose a layout for the specific action type haml with a symbol that is the same as your file. In the example above I have the top template located in users directory, so additionally I need to use make it as a string and later convert to symbol.
Did we miss anything? Yeah, we did. Assets. We have to configure it manually too. Add a few gems to Gemfile:
I believe you recognize them very well. They need to be configured in the main file.
What are we doing here? We need to include additional paths that will be used when serving assets, set the compressors for JS and CSS and create a route where the assets will be coming from.
It may look it’s done but it isn’t. We forgot to include assets in our main layout file.
Yeah, now it’s working fine, but I’ve mentioned about Bootstrap, haven’t I? As always, let’s add gem ‘bootstrap’ to Gemfile.
As in Rails, we need to configure it in assets directory.
Now you can polish your template a bit and check it out in the browser!
The last thing we’d like to configure in the application is background processing. We’ll use Sidekiq for that. Let’s start with adding gem ‘sidekiq’ to Gemfile and create config/sidekiq.yml file containing, for example, the following configuration:
We also need to create an initializer so it might contain something like:
Okay, great! It’s also done. If you consider setting up the Sidekiq UI you can easily configure it in config.ru file.
What’s going on above? We enable the UI at /sidekiq and add authorization using username and password stored in the settings file.
It’s also possible to add some helpers. Let’s say I want to protect my endpoint returning top 5 users.
How should the helper look? You may use helpers block.
We want our application working on Heroku but before doing it we need to create Procfile, that runs a web and worker dynos.
Of course, you should enable the dynos, Postgres and Redis add-ons first.
In some cases, e.g. during the development process, it may be useful to run a console. As you can expect, rails c won’t work here but you can type irb -r ./my_app.rb to run IRB with all classes loaded. If you’d like to run it in production you need to type heroku run bash first, but keep in mind that all actions in production environment can be unsafe!
Going to the end, now you should be able to build your first full-stack application in Sinatra. It’s not that hard, all you need to do is configuring some stuff on your own, without the magic that happens in Rails. Nevertheless, you can get a lightweight Ruby app so it’s worth doing it. I highly recommend you visit Sinatra documentation to explore more cool things!