Adrien Siami

Software engineer, Ruby, Rails, Web and stuff.

Read this first

Diving in Rails - Exceptions handling

Warning : This article was written a while ago, my views may have changed and this may not be relevant anymore.


Ruby code, just like any other code, can be subject to failure.

Fortunately, when an error occurs inside your Rails application, your rails server does not crash and stop serving requests.

In this article, I’ll explain how rails catches exceptions that may happen in your code, and how it renders a nice error page, depending on the Rails environment, and how you can customise it.

I won’t be talking about rescue_from, and ActionController::Rescue, but about the rack part.

Remember, it’s all Rack !

If you read my previous article about [Rails request handling](blog.siami.fr/diving-in-rails-the-request-handling), you know that Rails is based on Rack, and uses middlewares for various things.

Rails also handles exceptions with middlewares.

This article will explain how...

Continue reading →


Diving in Rails - The request handling

Warning : This article was written a while ago, my views may have changed and this may not be relevant anymore.


Introduction

There is an aspect of Rails I both like and dislike, Rails is automagical.

It does a lot of complicated things for you and makes the job super easy, however, it can be kind of frustrating, this feeling of relying on some magical piece of code.

I wanted to have a better grasp on the core problems Rails try to resolve.

You probably know it, reading the source of software, frameworks, libraries, is one of the best way to learn how they work.

In this article, I’ll talk about the stuff I learned while reading Rails’s source code, it may be the first of an undefined number of similar articles.

Today I’ll speak about how Rails handles requests, its relation to rack and more globally what is happening when you submit a request to a HTTP server backed by a Rails...

Continue reading →


Use roles in your rails models for mass assignment

Warning : This article was written a while ago, my views may have changed and this may not be relevant anymore.


As you probably know, with ActiveRecord (more exactly ActiveModel), you can whitelist some fields of your models so they are safe to be mass-assigned from a form.

Sometimes, you want some fields to be alterable by normal users, and other fields to be alterable by admins.

What I did in this case for a long time was to use slice on the params hash or create a if / else statement if the user is admin then individually assign each field.

ActiveModel provide us a much better way to to this.

Let’s assume you have a User model :

class User < ActiveRecord::Base

  attr_accessible :bio

  def role
    self.admin ? :admin : :default
  end
end

You can mass assign the bio field. There is also a role method that will tell us if the user is an admin or a default user.

Now imagine...

Continue reading →


Add some regular ruby code in your rails migrations

Warning : This article was written a while ago, my views may have changed and this may not be relevant anymore.


For long, I’ve used rails migrations only as a way to update my database schema, no more.

Today, I had to do a more complex operation.

I have a Message model and a Conversation model; for historical reasons, the read / unread was stored in the Message model, but I actually don’t need to keep a track of what message was read or not, only conversations.

So I had to do a simple migration that would create a filed in the Conversation model and remove it from the Message model.

However, I’ll have to apply this migration to production, and this will cause some data loss, how can I mark as unread the unread conversations ?

A few months ago, I would have created a complex script for doing that.

Today, I tried something pretty intuitive :

class MoveUnreadToConversation <
...

Continue reading →


My Ruby on Rails Stack

Warning : This article was written a while ago, my views may have changed and this may not be relevant anymore.


For deploying Ruby on Rails apps to production, I’ve previously used many different tools to get an idea of what will suit me the most.

Today, I think I’ve found what I wanted, so I’d like to share this with you, as picking around on the net config files and ideas was very useful to me.

Here’s the short version : I use Nginx as the frontal web server, it will act as a reverse proxy, sending all the requests from the browser directly to a unix socket on which several unicorn workers are listening. I use capistrano to automate the deployment, based on a github (or any git) repository.

I usually create 3 instances of the same website :

production (The master branch on the git repository)
pre-production (The preprod branch on the git repository)
development (The develop...

Continue reading →


Why I hate JQuery Mobile

Warning : This article was written a while ago, my views may have changed and this may not be relevant anymore.


If you follow me on twitter, you probably have seen me whining about JQuery mobile.

Indeed, I’ve worked a lot with JQuery Mobile recently, and this has been a pain.

So I’ll try to sum up here what I don’t like about JQuery, maybe I’m wrong, maybe I’m right, just tell me in the comments what you think about this.

Bugs, bugs everywhere

If you have a conversation of more than five minutes with a developer that worked with JQuery Mobile, you’ll probably hear that JQM is full of bugs.

Well, after several months, I’ve been through a ton of them.

Some bugs are well-known and stackoverflow is full of useful workarounds dirty hacks.

This adds a considerable amount of time to the development time, and gives me the impression of being innefective, hate that feeling.

Documentatio

...

Continue reading →