Saturday, December 29, 2012

I have moved.

The blog has moved to: http://jkamenik.github.com.

The full details of why are here: http://randomsoftwareinklings.blogspot.com/2012/11/the-long-away.html

Monday, November 12, 2012

The long away

The Reason

First, let me apologize to my 3 readers that I have been away for so long.  There are a lot of reasons why I have been gone, but mainly it boils down to how much I hate the blogger interface for dealing with code.

Sure, blogger is a simple platform and does exactly what it says.  Sure, it integrates with google plus, twitter, etc...  Sure, it was easy to configure, setup and deploy.  Sure, it has a lot of features.

But, it sucks for code!  And I mean Sucks!  For code there are a few very specific hot button items that are must haves:
  1. Monospaced font and significant whitespace (<pre> in HTML)
  2. Line numbers
  3. Syntax highlighting
Not that much to ask, but those features are ONLY ever going to be needed by a programmer.  So I can see why they don't get that much attention.

Both the old and the new blogger interface have a way of dealing with this, but it is time consuming and annoying:
  1. Use gist.github.com as a dumping place for all your code samples
    1. Create a separate file in the gist for each sample (makes it easier later)
    2. Make sure the file name has the right extension so that syntax highlighting happens
    3. Copy the embed codes for each gist
  2. When editing the blog only use HTML mode!
    1. The gist code is a javascript so it will only render in preview
    2. Switching between HMTL and compose sometimes rearranges non-visible items, which means when your page renders your code could be anywhere
    3. If I am going to write raw HTML I might as well use Markdown, which sucks less and converts to HTML
  3. Hope that nobody views your blog with JS disabled!

The Solution 

After playing with many blogging solution and giving up on Wordpress entirely (Sorry I just cannot get what I want without a LOT of php hacking), I have decided to give a Octopress a serious shot.  I use github all the time and even host a few things using github pages.

In case you don't know, Github pages can render Jekyll sites.  And Jekyll is a blog aware static site generator, which can read markdown.  And Markdown is a much more human friendly markup language then HTML, but compiles into HTML; also, Github uses it everywhere in their site (as do a lot of other places).

So things started to fall into place with Jekyll.  I just needed to install a syntax highlighting gem, and design the site.  In the process, I stumbled across Octopress, which is a wrapper around a Jekyll site, works with github, has syntax highlighting, provides a clean theme, and has a programmer friendly workflow.

So Octopress is the blog engine for me.  And it will reside at http://jkamenik.github.com.  I will slowly be transferring all my entries from here, but I doubt I will retire this site entirely.

Friday, March 9, 2012

Factory Girl Automatic Tests

Introduction
Early in a project I started to use factory girl without fully understanding it.  After many months of creating steps like "Given /^(\d+) blog exists$/" and "Given /^the following blogs exist:$/" I started to come up with generic functions that would build those steps.

Stupid me for not checking that factory girl already does something like that.  All you have to do is include factory girl's step_definitions.rb file:

Once you start using FactoryGirl correctly there are a world of new features that can make your steps both cleaner and more concise.  Here are some tips I have found via trial and error.

Tip 1: Do not reinvent the wheel
Factory girl will create steps for all factories that you have register.

Tip 2: Use association, Do not add more steps then are needed
Factory girl association are automatically created before the factory is created and they are automatically linked.  It only supports the belongs_to behavior, so keep that in mind.

Using the "Given the following XXX exist" step you can define attributes, on the the association, in the table.  If we leave the association out then a default is created.  If we define an attribute then it will be found or created using that attribute.

Lets say you have a product and it can belog to a category.  You do not need to create a category.

Tip 3: Attaching files via CarrierWave
Since cucumber is a text file it doesn't make much sense for you to define full files in steps.  It also doesn't really make sense to embed full file paths in the tests.  Instead, you can use a Transient attribute and some code so that in cucumber you define a file name and in the factory it converts to an actual file.

Tip 4: Fixing a circular dependency between two models
Lets say you have a Store model and User model.  And a User can both work at and own a Store.  If you put associations in both the User and the Store model then each will try to create the other, infinitely.  We can reuse the transient method as before to break the circle.

The trick is to avoid defining an association in both factories, but instead use a transient attribute in one factory to simulate the behavior of an association.  Also, since transient attributes are not likely to have the same level of sophistication as the associations you should use the association to define the more complex of the two models.

Tip 5: Fixing a circular dependency between the same model
Let say you have a Category model, and that model can belong to another Category (a tree) then you cannot use an association or you get the same infinite recursion issue as before.  Here we can use a transient attribute along with an after_create hook to simulate the behavior we want.