Work
with us

Tell us about your idea
and we will find a way
to make it happen.

Get estimate

Join our awesome team

Check offers

“Rails is dead” – every year we hear the same words. Rails is fine, in my opinion, and as a framework it’s getting more mature and stable. However, people around the world are creating alternatives for Ruby on Rails. One of those is Roda framework. Today I’ll show you a quick guide – from Rails to Roda. 

Prerequisites

I assume you have basic knowledge about what Roda is and how to use it. Otherwise, I recommend the framework homepage : http://roda.jeremyevans.net/. I want to focus only on things you need to remember while creating you first web app in Roda framework.

Structure of Roda app

One of the keys to a successful application is to have a proper structure. I’m not saying that it’s the best solution, but it gives you a good start.

Rvm and Gemfile.

Rvm is optional, but I’m used to having it in all my projects. In the Applications folder create two files: .ruby-gemset and .ruby-version. In Ruby gemset, type the name of the gemset you want to have, and in .ruby-version just write the version of the Ruby you want to have. Next time, when you enter the folder from terminal, rvm will generate proper wrappers for you.

.ruby-gemset

roda-app

.ruby-version

2.3.0

 

Gemfile is pretty useful, and even though Roda is using a plugin system, we can use gems as we use them in the Rails application. Additionally, we have a bundler to manage and install gems.

Initial Gemfile:

source 'https://rubygems.org'

gem 'roda', '~> 2.6.0'

After that you just need the main file in the root, let’s call it roda-app.rb.

Folder structure

Apps

So, the main idea is to treat each app functionality as a separate application, and to place it in the Apps folder. You can decide whether you want to have some namespaces there, or you can create apps as Rails controllers. Additionally, you need to have plugin :multi_route

apps/order.rb

RodaApp.route('order') do |r|
  # POST /api/orders/new
  r.post 'new' do
    message = CreateOrder.new(r.params).call
    PostToSlack.say(:food_bot, message)
    status(200)
  end

  # POST /api/orders/show
  r.post 'show' do
    message = ShowOrderList.message
    PostToSlack.say(:food_bot, message)
    status(200)
  end
end


Then in you roda-app.rb file, you will need to include these files, so the file will look like this:
 

route do |r|
  require_relative 'apps/order'
  # /api
  r.on 'api' do
    # /api/orders
    r.on 'orders' do
      r.route 'order'
    end
  end
end

Assets

If you want to have assets in your project, there is a great plugin for this called assets. You just need to include it into file and point what do you want to have included. Default structure consists of folders named css and javascripts.

plugin :assets, css: ['signin.css', 'reservations.css'],
js: ['reservations.js']

Configurations

This is something like in the initializers in Rails. I place every file that needs to be included during the application start there. I put this line before the RodaApp class definition:

Dir['./configurations/*.rb'].each { |file| require file }

Helpers

Here I place some global app helpers. You need to include them in a manner similar to configuration files. I put here methods like this one:

def production?
  ENV.fetch('RACK_ENV') == 'production'
end

def test?
  ENV.fetch('RACK_ENV') == 'test'
end

Migrations and models

If you need to set up a database, I recommend this blog post: http://mrcook.uk/simple-roda-blog-tutorial. You can learn a lot from there, and it’s no point to write something that has already been explained in detail.

Services

This is a folder where I place classes responsible for handling the input. So, basically you fetch the service from the Apps folder, and handle the response there. All methods and business logic are placed in the Services folder. Of course, you have to remember to require them, just like helpers folder.

Specs

I use rspec for testing. Roda is a Rack framework, so running rspec tests under it won’t be a problem. You can configure the database cleaner or even the factory girl. If you want to have the second one, you need to make an ugly hack: put alias_method :save!, :save into the model you want to be created by the factory. You have to do it, because you don’t have an active record.

Tasks

This is a folder like the one in Rails where you have rake tasks for your project.

Views

I don’t think I need to explain contents of this folder ;)

Main differences between Roda and Rails

We all know that Roda has only one file at start, and Rails has tons of them. But imagine that you created more complex folder structure, and you want to have a web application that will have some views, authorization, and other basic stuff. What do you need to remember about?

No database.yml

You need to connect to your database to a connection string. So for postgres you will have, for example:

DATABASE_URL="postgres://user:password@127.0.0.1:5432/database-name"
Sequel.connect(DATABASE_URL)

No devise

This is one of the most used gems in Rails development. Unfortunately, it is tied with Rails and active record, so you can’t use it outside Rails. But no worries, some good guys figured out how to manage that.

https://github.com/badosu/Yogurt/wiki/Example:-Adding-authentication

https://github.com/beno/roda-auth

https://github.com/jeremyevans/rodauth

No view helpers

You may forget about link_to, form_for, and other helpers you use that come from Rails. Naturally, you can define them yourself.

Use plugins or/and gems.

Here is a list of plugins that can be used: http://roda.jeremyevans.net/documentation.html. Remember, you can use gems too (only if a gem is not strictly tied with Rails or active record).

Rails console

Of course you can obtain an irb console and include all models, e.g.:

irb -r /models.rb

Schema

You don’t have any schema.rb file, which means you can’t look at fields in the database for this file. However, it is possible to show the table:

DB.schema(:table_name)

Summary

I think every Rails user should try out Roda. It is a great workflow, that shows you how many things Rails’ magic does. You will have better understanding of which method comes from Ruby, and which from Ruby on Rails.

We’ve created a small application, which we use in our office from time to time, based on slack integrations. The source code is available here: https://github.com/binarapps/slack-integrator and all the examples I used come from this project. Happy Roding!

Post tags: