I started doing some real rails work back in the days when the mantra sounded "slim controllers, fat models". But I realized that the reason that we had that mantra, was there where a lot of logic going on in the controller layer, which didn't belong there. Much of this was moved to the models, and rightly so. But this presented another problem — for me at least — keeping state, and validating the right things, based on the context that the record was being manipulated in.

An example where I use this is creating a user. In the application I'm building at the moment, the initial creation of the user is as an invite, after which the user needs to complete his profile afterwards. This of course calls for different validation in the different states, in which the user object is in the process. To achieve this, I could make an advanced state machine out of the user object, to keep track of the state it's in, and which validations to do on the user object at the given state.

I found Virtus, which can make any old ruby object quack like an active record object. This allows you to make classes for a very specific contexts. So you could make a sign-up object, a sign-up-complet object, which each validate something different. The sign-up object — which is the invitation in the example above — should validate that an email, a name and an account (to which the user should be attached), but it should validate for no more. The completion object could validate that a password was entered, and that the user had filled out various other bits of information. So the user object, when saved to the database is always valid, in comparison to the context which last manipulated it.

Another use for the contextual classes are signing in. So instead of having the logic of signing in, in the controller layer, you could have a simple ruby class do that for you.

class SignIn
  include Virtus

  extend ActiveModel::Naming
  include ActiveModel::Conversion
  include ActiveModel::Validations

  attr_reader :user

  attribute :password, String
  attribute :email, String

  validates :email, presence: true
  validates :password, presence: true

  def persisted?
    false
  end

  def save
    if valid?
      persist!
      true
    else
      false
    end
  end

private

  def persist!
    # stor user session or what not, to keep track of his sign in
  end

end

Some of the clear benefits of this type of responsibility delegation is that each of these simple classes are easy to test in separation from the rest of the application, without having to mock or stub a whole lot. And as you go along — because the classes are simple and small — they are easier to refactor and extract even simpler objects from.

Lastly, I just want to touch on a design philosophy about object oriented programming, which is: Don't be afraid to have a large number of objects. Be more afraid to have large, complex objects.

Additionally, if you're having problems reaching a state, where you can do this type of extraction and refactoring, I encourage you to read Bryan Helmkamp's great blogpost: 7 patterns to refactor fat models


Monotasking

Published on May 11, 2013 21:00

I recently watched this TED Talk about monotasking and I realized, that it coined the exact term for what I have been trying to accomplish in my own, professional life.

It is something that I first got the scent of, after watching an interview with Christian Stadil, who is currently the chairman for Hummel. He talked about how he was coping with the stress of being the head of a multinational brand. One thing he said, which resonated greatly with me was: "Being fast isn't synonymous with being thorough. So do one thing at a time, and commit to it."

At the time, I wasn't sure, how to turn this into a mantra, what to do with it, but I was quite sure, that it was something that was going to follow me for a very long time. So after watching that talk, by Paolo Cardini, and reflecting a little on the term: "Monotasking" it became completely clear to me, what a lovely word that was, and how it covered the essence of Christians philosophy.

So let me share a little bit, how I try to use monotasking in my life. I will set very strict boundaries for working, and not working. Not in the sense that I will schedule my day, but in the sense that if I'm doing something, I try to commit to it wholly.

If I'm sitting, working, and I feel like actually doing something else, like tweeting or reading an article, I will try and shrug that feeling, and concentrate on the work. If the thought pops up again, I will go and fulfill it. And not just tweet a single time, and then return to work, no. Commit to it! Read the tweets you want, the articles you feel like, and read them properly. Then, when that's out of your system, you can return to work.

So what I'm trying to say is, if you do something, stay with it. If you don't feel like doing it: Stop, and do something else, then come back to it, when the time is right.


My tools of the testing trade.

Published on May 10, 2013 21:00

I just wanted to share a little list, of the tools, that I use for driving my test and behaviour driven development.

So here is the list:

CircleCI

Is a continuous integration server, which will run your specs for you in a fresh test environment. It integrates nicely with github, so when you push you code it wil automatically pick it up and test that push for you.

RSpec

This is my default goto testing framework for any application. It gives you great tools for testing your entire application stack. Here is a quote from the home page, which I feel describes how I feel about the tool:

Born under the banner of Behaviour-Driven Development, it is designed to make Test-Driven Development a productive and enjoyable experience [...]

Capybara

Will give you that extra edge, allowing you to in fact interact with your application like a real user. Clicking elements, filling in text fields etc. I use this extensively to test wital parts of my view layer, where there is complex user interaction, which is prone to errors because there might be many parts working together.

Turnip

Is for those really nice user stories, which will allow you to almost simply copy the user story to a .feature file and run it. This builds on Capybara and Cherkin, so it utilizes the great features of Capybara, along with the functionality from cucumber.

Guard

Is my goto place for running all those testing frameworks and tools automatically. Guard will spy on your files, and as you save a file, it will run the test which is appropriate. So if you change the application controller, the entire controller test suite will be run, since they all (most likely) inherit from this class.


I had a project handed over to me, from Jeppe Liisberg. It was a prototype project, to pilot a possible, bigger project.

In order for me to understand the project, we had a talk about the scope, the domain and plain and simple the functionality of the project. I had some questions about some of the technical implementation, and the level of testing.

This discussion left me thinking: How do you test prototype applications?

Due to the nature of the beast of prototyping, changes needs to be introduced incredibly quickly, as the client changes his mind, or problem arises, and you need to go another direction. This extremely rapid change of functionality sometimes clashes with the more slow process of doing well tested and test driven development. So the question arises: When is it appropriate to slag on the TDD principles to gain that rapid development streak?

The application in question had a small behaviour driven Cucumber
suite set up, that just described how the main feature was supposed to work, and not much more. There where no tests that controllers where redirecting correctly, and that models where validating the data integrity before committing to the database.

Before I started doing prototype work, I worked with some good greenfield applications, where we could do some extensive test driven work. And being a fan of Capybara that meant adding a feature test, and using that to drive the development. But I realized, after reading through the Cucumber feature, that this was much more flexible, in the sense that it was more readable to the client, when he had little understanding of the application, and more flexible to me, as a developer, when he changed his mind.

So I tried converting some of the tests for an existing application to cucumber, to see how that would work out, and unfortunately it didn't go well. I found that if you already have a functioning application, with an extensive test suite, you should stick to the systems you have in place. If you are — on the other hand — developing an experimental feature, which will probably change a lot during the initial phases, writing a Cucumber test would be awesome.

So I looked into the various BDD frameworks, and they are all more or less build on Gherkin (which was extracted from the Cucumber project). From the Gherkin webpage:

A fast lexer and parser for the Gherkin language based on Ragel.

One of the other frameworks that I found, which is also based on Gherkin is Turnip by Jonas Nicklas.

And this one is by far my favorites, in parts for some of the considerations that went into it — lessons learned from experience with Cucumber — but mostly because it's a better fit with my existing testing framework setup, which is Capybara and Rspec. Turnip will nest it self in spec/acceptance, and that's exactly where I would want my acceptance tests to be.

But more to the point, I like BDD frameworks for testing my prototype applications because they are based on a lexer, which means they are an attempt to translate real human formulations into something that the machine can understand.

This offers great flexibility and an immediate understanding form a non-technical point of view, and you can write the test with the customer, and then go home and implement them afterwards. Let me give you an example:

The customer: So after you login, you should be able to see the ten most recent articles.

This easily translates into something like this:

# spec/acceptance/articles.feature
Feature: Reading articles
  Background:
    Given a user exists
  Scenario: Viewing the latest articles
    When I sign in
    And I go to the articles url
    Then I should see the 10 most recent articles

Now this is a syntax that you can translate as a developer, and the client can understand. But a little word of warning at the end. This is meant to be high level, broad acceptance tests. Don't use them to get into insane details like links having particular classes, or even the dom having specific elements. Keep it to the lexer related content on the page. This is when BDD truly shines.

Good luck, and don't hesitate to comment.


Running Puma on Heroku

Published on May 06, 2013 18:00

After writing my article about setting up your dev env with Heroku, there where some discussion about thin vs. unicorn. Personally I enjoy Puma as a concurrent, multithreaded web server. So here is my little setup guide for using puma with Heroku.

So even though I really enjoy Heroku, I'm grateful to Engineyard for making this awesome web server.

Setup your environment

First off, simply add the gem to your gemfile, and install with Bundler.

gem "puma"

Then update your Procfile, so it reflects this change, and uses puma instead of thin, mongrel or unicorn or any other web server you might have running there.

# Procfile
# web: bundle exec rails server thin -p $PORT -e $RACK_ENV
web: bundle exec puma -t 1:4 -b tcp://0.0.0.0:$PORT

Restart your server, and you'r up and running again.

Modifying and tweaking the setup

From the official documentation: "Puma utilizes a dynamic thread pool which you can modify." The threads are denoted by the -t option, and defined a minimum and maximum number seperated by a colon. The default is -t 0:16.

It's important to understand that this isn't a magic number. Simply putting in a higher number won't make your website run faster. If your application uses 128 MB RAM, you can only have four threads running on each dyno. Exceeding this will slow down or even break your setup. Equally, you shouldn't run the maximum number of threads always (e.g. -t 4:4), since that will cost you maximum dyno hours, always.

There is a performance gain by using unix sockets instead of TCP, about 5-10% (from puma's own documentation), but I haven't looked into this in detail, so if you have some experience there, please let me know, and I'll update the article.

Performance gains

There don't seem to be a concensus as to the actual performance of unicorn vs. puma. The documentation from puma says one thing, a blogpost by Ylan Segal says otherwise.

So I guess I'll leave it up to you, and the specifics of your setup.


Setup your Heroku dev environment

Published on May 04, 2013 19:00

Recently, I have been working alot with Heroku. So I thought that I would just share a couple of my snippets and go-to gems for setting up my local development environment to develop for Heroku. I know that Heroku goes for being replaceable, and unobtrusive, but if you know you'r going to use it, let's face it: You might as well optimize a bit for it.

A couple of gems that will really help you along are the dotenv-rails which will allow you to load env variables from a .env file into your environment.

Also Heroku has a toolbelt which they suggest that you install. I completely agree, but I believe there are to many things included there. So I usually manage the foreman gem in my gemfile.

I also add Thin server to my gemfile, to have control over that, and I install git via homebrew.

Spin up your application on Heroku will use the Procfile to define the various services. So to make my development environment match your production environment as much as possible. I do this with the following setup:

# Procfile
web: bundle exec rails server thin -p $PORT -e $RACK_ENV
# .env
PORT=3000
RACK_ENV=development
RAILS_ENV=development

This will effectively load those three variables into my environment, causing the completely valid production procfile to work in your development environment, when you spin up the server with foreman like this:

$ bundle exec foreman start

Edit May 6th 2013

Additional services that you can define in the Procfile are things like background workers for doing batch email processing or handling large-scale file uploads.

And based on one of the discussion on LinkedIn about this article I should probably mention that the services you define in your Procfile should of course be real services. They should adhere to the correct exit codes and such, for Foreman to manage them.


I'm cheap — I admit it. If I make an application that runs on a free, single Heroku dyno I would still want it to perform properly, even if I'm not paying money!

There is a bunch of possibilities for optimizing your web page. Both page rendering, read/writes, caching and many others. But the first problem that a user experiences with a website run on Heroku is that it takes a really, really long time for that first byte to reach the user, if the dyno is sleeping. Although this isn't bad performance from a programmatic viewpoint, it's still bad performance from the user's perspective.

The idea started when I discovered the new feature that Heroku has implemented, where you can make a production check on your system. One of the things that it runs through is performance measuring. Right now there is no other tool than New Relic available, but since I believe that's the best tool, I don't mind.

So setting up New Relic for my application, I noticed their availability feature which allows you to specify a url to ping, to make sure if the application is still alive. This small ping will also prevent your Heroku dyno instance from sleeping, giving your users the best possible experience.

TL; DR

To stop your Heroku dyno instances from sleeping, add the New Relic add-on, and set it up to ping your application.


Some vacation pictures

Published on May 03, 2013 12:00

This is a bit of a different post. I just thought, that I would share some of the great photo's that I took, while I was with my in-laws in Spain.

Image

Image

Image

Image

Image

And now for some more colorful pictures.

Image

Image

Image


TDD refactoring

Published on May 02, 2013 23:00

Recently I began using codeclimate. I should have done so long ago, but as the proverb goes: "Better late than never".

I'll take off in a concrete example, which I ran into, where codeclimate and TDD helped me factor aggressively. I have just introduced the related articles in the sidebar, here on my blog (they are here -->)

I wrote the tests for how, I would like the functionality to function, and then I wrote the code. I placed it in the article model, in a related method. I then pushed the code, and voila. New code online.

Now comes the bad news. In comes a mail from codeclimate: "Your Article class has dropped from B to a C" it said. That was quite a surprize, but after looking at the other stuff, which was rated better, it was clear that this method was indeed cluttered and complex. To paraphrase Fight Club: "I am Emil's cluttered code" I felt it telling me.

Next step. Look for ways to refactor this. It tried moving it out into smaller methods, but that made it seem like each method was missing context to what the purpose of the method was. This is when I remembered that Bryan gave a great talk about refactoring at wroclove_rb. So I tuned into the video, to trigger my memory of that talk.

Using some of the principles about refactoring class methods, I extracted a quite complex method into it's own more contextual class. This is what I ended up with. There is always room for improvement, but it's easier to test, contextually more contextual and not least; it's simpler.

#
# This class in an abstraction, to simplify a complex relational query in the
# article model.
#
# This class will build up an array of @limit related articles to the initially
# passed @article. If there isn't enough related articles to reach the @limit
# additional, unrelated articles will be appended to the array.
#
class Article::RelatedArticlesQuery
  attr_reader :articles

  def initialize article, limit = 5
    @article = article
    @limit = limit
    @articles = related_articles + unrelated_articles
  end

private

  def related_articles
    @related_articles ||= Article.published.chronological.
      not_in(id: @article.id).
      where(:keywords.in => @article.keywords(true))
  end

  # Grabs the unrelated articles, which is added to the related ones, if there
  # are not enough related ones.
  #
  def unrelated_articles
    Article.published.chronological.not_in(id: taken_ids).
      limit(number_of_needed_unrelated_articles)
  end

  # Returns an array of the ids, which has already been used in the related
  # articles.
  #
  def taken_ids
    [@article.id] + @related_articles.map(&:id)
  end

  # Returns the number of needed, unrelated articles, to make the given limit.
  #
  def number_of_needed_unrelated_articles
    @limit - @related_articles.count
  end
end

This allows me to simply call RelatedArticlesQuery.new(article).articles in the related method which I had implemented earlier, like this:

def related
  RelatedArticlesQuery.new(self).articles
end

So aside from the immediate benefits, my tests also got simpler. I was able to extract the test for related articles into it's own test. Which brings me to a point, which I made a while back, that where TDD truly shines is when you need to refactor. Now, not only where the tests invaluable when extracting this code, but the fact that the tests themselves got better is a self fulfilling prophecy.

So, if you haven't watched it yet, have a look at Bryan's talk and give codeclimate a try. This gives you great insight into where your code is truly complex. And with the help of your trusty tests, it will make refactoring fun and easy!


A little, nifty trick for CircleCI

Published on May 01, 2013 23:00

I recently came across a situation, where I needed to run a long running task through CircleCI. The guys there pointed me to a — for me, and the people I talked to — little known feature: You can set the timeout of each of your commands in the circle.yml file.

So how do you do this, you ask, well it's quite simple. Here is an example of heightening the timeout for deploying to Heroku to 5 minutes:

deployment:
  production:
    branch: master
    commands:
      - git push --force git@heroku.com:your-repo.git $CIRCLE_SHA1:master: { timeout: 600 } # 5 minutes

So as you can see, it's simply adding the : { timeout: xyz } part to the command, and CircleCI will pick it up, and wait the given amount of seconds.