What is coming up in Rails 5

While Rails 5 is still in the pipeline I went through its changelog and the source code to see what’s coming up. I got very excited about a bunch of new features I believe will make our lives easier!

So, let’s start by looking at the changes. First of, the new version will require Ruby 2.2. So if you are thinking of upgrading your app to Rails 5, you should start with upgrading Ruby.

Rake Tasks

On previous versions of Rails, you have commands executed with rails  and others with rake. In this new release, one of the features includes executing all Rake tasks using just Rails. The new format is similar to the older one:

rails db:migrate.

This move toward simplification will make both the framework and task automatization easier to learn.

Action Mailer

Regarding Action Mailer, the main change is that  deliver  and deliver!  methods were removed and replaced by   deliver_later  and it’s bang version. This change was introduced to make Action Mailer compatible with Action Job (see below).

Also, all  *_path  methods have been removed to prevent introducing programming bugs when writing senders. Instead, Rails 5 will force us to use use the *_url  methods.

Another change is that templates now respects I18n fallbacks. In previous versions of Rails if you had a template called welcome.en-US.erb  and another called welcome.erb  even if you had set the en-US  as the default language, the template welcome.erb  would be rendered. Rails 5 will use the local option rather than the fallback one.

So far, so good… and we’re just getting started.

Action Pack

Apart from Action Mailer, Action Pack also gets some interesting changes. Under development environment, when you browse to a non-existing route, Action Pack displays route information, letting the user filter the results. In previous versions this filtering was a pure Javascript regex trying to guess the route and was a little bit unreliable. Rails 5 improves this feature by getting accurate routing information directly from the Rails backend instead.

Action Controller

Now it is the turn for Action Controller. The novelty is a parameter of the  protect_from_forgery  method that allows prepending (or not) the forgery protection. As in previous versions, it also lets you add a conditional –e.g. like avoiding forgery protection in JSON requests. This is how:

When this option is set to false, Rails will not prepend the protection. In the example, it will also turn the protection off for JSON requests. If true, the callback will be inserted in the first place of the callback chain.

Action View

There are some cool new stuff in the Action View, too. One of them is the possibility of naming partials using any character, not only alphanumeric ones. In previous versions, partials’ names must start with an underscore and then followed only by alphanumeric characters, numbers, or underscores. In Rails 5 we can use any character after the underscore.

Another change is that helper methods like   content_tag_for , div_for and so on were removed from the core and moved out to a separate gem, called record_tag_helper.

Active Job

Active Job, a great addition to Rails 4.2.0, also got some tweaks in the new version. Formerly a separate gem, Active Job was merged into Rails to serve as an adapter layer for the most popular queuing libraries around.

Active Job allows us to change between inline jobs or delay jobs only by changing one line of code:

From:

To:

In the new version, all the jobs inherit from a base class app/jobs/application_job.rb. This is to be consistent with the already existing structure of others components in Rails, like controllers, models and the like.

The following is an example of how to use Active Job:

Active Job is integrated with Action Mailer allowing us to easily send emails asynchronously. The methods deliver_now  and deliver_later, and their bang versions, will use the preferred queue adapter.

Active Record

In my opinion, the most existing changes are in Active Record.

First, let’s refer to  belongs_to. Before Rails 5, given an Employee that belongs_to  a Company, nothing prevented from creating a new Employee without specifying the Company it belongs to. In the new version, a validation error will be thrown if the associated record is not specified.

If you migrate your app to Rails 5, this feature will remain disabled unless you specify the opposite. It will be enabled when creating new applications, though, or if you add the following line to the Rails configuration:

Second, another useful addition is  ActiveRecord::Base.suppress. This method prevents its class from being saved while executing the enclosing block.

Third, a much expected feature: now the or  method is available for relations:

And last, for migrations now it’s possible to specify a parameter :if_exists  for dropping a table only if it exists.

And there are a lot of bug fixes, and other features… I can’t wait to start using this new version of Rails! Looking forward to it!


Crash or… Monit!

As an application grows it starts demanding more and more work. At some point of the ongoing project, processing a task in the background becomes a necessity. One of the most popular tools to accomplish this in Ruby is Sidekiq.

Now, did it ever happen to you that you get it all up and running on the server and the next thing you know is the server is down and you get a colorful variety of error reports yelling that stuff is not working?

Meet Monit, a tool that’ll let you get ahead of trouble. It automatically restarts programs when they crash and can send email reports on failures!

In case you want to try it out –and make yourself a favour– here’s some aid on installing, configuring and get it up and running.

Install Monit

Depending on your package manager this may differ:

 

Configure monit

  1. Edit /etc/monit.conf  or  /etc/monitrc
  1. Uncomment (or add) these lines:

 

Add Sidekiq entry

Create the file /etc/monit.d/sidekiq.monitrc  with:

 

Start Monit

Let’s not forget to start Monit:  /etc/init.d/monit start

Now a sudo monit status should show the sidekiq_application_production0  entry.

If you use capistrano you will notice the current and shared directories. If you don’t use capistrano just make sure the PID is stored in some place where the path doesn’t change to let Monit find it.

This configuration will also be useful if you use capistrano-sidekiq, just make sure the check process name in the config matches what is used during deploy.

This configuration includes a memory protection that will restart Sidekiq before it eats up all the memory in your server. Yes, I’ve got a server down more than once due to this issue :(

 

Configure email server

To receive Monit’s status via email just add the following to /etc/monit.conf  or  /etc/monitrc

Add the email server, e.g.: for gmail’s SMTP add in the following line to the config file

set mailserver smtp.gmail.com port 587 username "notifier@gmail.com" password "********" using tlsv1 with timeout 30 seconds

 

Subscribe

To subscribe to all emails, including non-error emails, add the following line to the config file:

set alert my_app_notifier@gmail.com

 

If everything is set up correctly, executing sudo monit reload  should trigger an email.

If you want to get emails for specific events or have more questions regarding Monit’s config check out http://mmonit.com/monit/documentation/monit.html.

A word of warning, the doc is huge. If you are looking for something specific like email, alert or whatever you need, just search it.

 

That’s pretty much it. Of course Monit doesn’t take care of starting the processes if the server restarts. You’ll have to look for another tool to do that.

Reposted from http://andisxp.blogspot.com/2015/03/keep-sidekiq-running-using-monit.html


Just Enough Linux to be Dangerous

This post is inspired on a talk about Ruby & Linux at the RubyConf Uruguay 2014.

The Linux Process in Ruby

Creating a new process

In linux we use fork to create a new child process. In Ruby we have access to this by using  Process::fork

It accepts an optional block. We can use either Process.fork or simply fork. When a block is given the child process runs the block and terminates with a status of zero. When no block is given the fork call returns twice, once in the parent process returning the child pid and once in the child process returning nil. When using fork only the thread calling fork will be running in the child process, no other thread will be copied. Since ruby 2 there’s been an improvement to copy on write with relation to the GC. Instead of duplicating all the data it just copies the data when the shared memory is modified by one of the processes.

Continue reading…


What’s new on RSpec

On June 2, 2014, RSpec 3 was released after months of announced features that were becoming available through new versions of the RSpec 2 branch.

Here at Moove-IT, we decided to take a look at the new stuff, as well as say goodbye to some of the old things that have been removed. Just in case some of us who use RSpec haven’t paid attention to them lately, we put a little bit of Better Specs on top of all that.

Continue reading…


When and why Clojure

Introduction

When you have to solve a problem, in order to make an efficient and effective solution you must choose the right tools to help you in the process of building it. As programmers, the languages we choose determine a big part of the solution and they will influence the way we reason about it, so it’s really helpful to understand what are the kind of problems that a language aims to solve before starting to use it. Following that line, this post tries to summarize the key features of Clojure that make it preferable over other languages under some conditions, based on what Clojure designers and its community predicate about the language.

Continue reading…


Token based Authentication – Json Web Token

What is it for?

Token based authentication is a new security technique for authenticating a user who attempts to log in to a secure system (e.g. server), using a signed token provided by the server.

The authentication is successful if the system can prove that the tokens belong to a valid user.

It is important to note that Json Web Token (JWT) provides signed tokens but not encrypted ones, so passwords or any critical information must not be included in the token unless you encrypt the data (e.g. using JWE).

Continue reading…


How we do Asynchronous Planning Poker Estimations

Have you ever worked in teams, where several and simultaneous projects take place and stories need to be estimated quickly and accurately so as to commit to a delivery date?
If you ever encountered this challenge, you would probably find this article very useful.

A different scenario from Sprint Planning

Traditional Scrum Sprint planning definitely works for some of our clients. But, in this particular scenario we are talking about a team working on several projects for a single client. Therefore, we are constantly receiving tickets which need to be estimated. Since it does not make sense to gather the whole team too often in order to estimate, we came up with this idea of an asynchronous planning. We are positive you would find it extremely helpful.

How it works?

Diagram

  • Once a new task arrives, the team members working for that particular project know they must post their personal estimates on a special board (asynchronously and not anonymously). This board would then contain several post-its with the right estimations facing the wall, so they are not visible for the rest of the team.
  • After a minimum of estimates have been cast, one team member turns around the pieces of paper and gets an average.
  • In cases where the estimates presented are too different (which is rare), and then the average could be just not that useful, the team members gather to discuss the estimates more in details and reach an agreement.

Every two weeks the estimations are analyzed and compared with the actual execution time. Along with this, a retrospective meeting takes place to improve the overall functioning of the team.

Pros

  • Super fast.
  • Average improves accuracy.
  • Visibility (every team member sees all estimates).
  • Time saving.
  • Team player philosophy.
  • Improves performance.

Cons

  • Fewer group discussions.
  • Only works with highly organized teams.

Taking it to the next level

We started this process using only a whiteboard, a few markers and some post-its. As we are geeks, and love creating software to solve pretty much everything, we decided to take this to the next level and create a specific tool for it. We called it “Zenkai” – playing with the letters of “Kaizen”.

Zenkai

It works by simply automating the above described process.

Pending Estimates
Dashboard with pending estimates.
Dashboard
Dashboard.
Report
Tickets per project report.
reports_2
Hours and Team Velocity over Time.

 

Zenkai on GitHub – Fork it and tweak it, or submit your Pull Request!

We’ve been following this process (and using this tool) for almost a year now and are constantly trying to improve it. Naturally, this facilitates the estimation process. Beyond that, it truly helps our teams to stay aligned, deliver in time, and keep track of their improvement.

If you want to implement this with your team, please contact us and we’ll be happy to discuss further!


Lean Startup Machine Montevideo

Lean Startup Machine is taking place in Montevideo this year, on August 1st – 3rd, 2014.

This three-day workshop focuses on teaching innovators and entrepreneurs how to start a business using Lean Startup.

Ariel Ludueña (CEO) and Martin Cabrera (CTO) both at Moove-IT have been invited as mentors to be part of this great Lean Startup educational experience.

For more information: https://www.leanstartupmachine.com/cities/montevideo/

 

Lean-Startup-Machine