Saturday, November 15, 2014

Use FactoryGirl And Faker for easy data generation in unit testing (Part1)

The  most irritating thing in writing tests is the data generation preparation process, sometimes you want to create an object with 20 fields that 10 of them are mandatory but you only care about the value of 1, and you don't want to mock, cause you interact with other methods and object that fetch this model from db.

To me it happens a lot so I found the FactoryGirl + Faker combination that made my life much more easy and now I can write tests in peace.

To add them just put in Gemfile:

 gem "factory_girl_rails", "~> 4.0"
  gem "faker"

So lets take a complex sample to explain all there is to know.

We have a User, the User belongs to a Company, User has many tasks.

Company has many irritating mandatory fields

Lets define the Company Factory:

FactoryGirl.define do
    factory :company do
      name {}
      trp {Faker::Number.between(0,10)}
      grp {Faker::Number.between(0,5)}
      budget {Faker::Number.number(4)}
      cpm {Faker::Number.between(1,70)}
      trp_price {Faker::Number.between(100,700)}
      viewer {Faker::Number.between(0,100000)}
      total_viewer {Faker::Number.between(0,200000)}
      unique_viewer {Faker::Number.between(0,50000)}
      spots {Faker::Number.between(0,1000)}

As you can see I define lots of fields with random values.

Now we can create a company by writing
Or just build one (without save to db) by calling


That's all for now, I'll continue in part2.

Get the user locale from http headers

If you want to provide default localization support for guest user in your website yo can locate their browser locale by the following code:

def extract_locale_from_header
    unless request.env['HTTP_ACCEPT_LANGUAGE'].nil?
      return request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first

This way you can determine on server side the locale of the user currently entered your site.

Dedicating a DJ(Delayed job) worker to a specific queue in heroku.

Let's say you have a vey important procedure that takes a while, and you need to process it in background, but you still want to execute ASAP.

I'm using Delayed Job on Heroku, and could not find a lot of tutorials to do this simple task.

So here is the simplest way to achieve this ability, you can raise a process/deamon  that will have only one queue to work on. (Dealyed job worker by default is queue agnostic, just process all jobs)

In procfile:

urgentworker:  QUEUE=urgent bundle exec rake jobs:work

You can call the worker in any name you want and even define multiple workers for multiple queues.
And in the "urgent" job just define the queue name to be "urgent" the worker will process only jobs in this queue.


handle_asynchronously :some_job, :queue => "urgent'

This will also work for Resque.

Notice that for rescue you can write QUEUE=* but for delayed job you can't.

Overriding default id in mongoid

This is an easy one, just want to make sure it's clear.

Add the following field to your model:

field :_id, type: Moped::BSON::ObjectId

And to allow setting it:

1 def id=(id)
2   self[:_id] = id
3 end

Now you can just do[Some ObjectId] and save the object.

Sunday, October 5, 2014

Nice post about cyber war

Popular Science: The War Of Zeros And Ones.

Friday, August 8, 2014

ClusteTV is hiring

Cluster TV is looking for it's next programmer/Superstar!

If you can:

* Write code like an Effin' machine
* Test your OWN code
* Speak English like it's your mother's tongue
* Handle challenges
* Be self-driven & independent

We would also appreciate if you'll have:

* Great communications skills
* Independent thinking
* Passion to learn new things
* RoR or Angular experience
* Coffee script knowledge
* NOSQL understanding

Then we want you!

What we offer:

* Great and professional team.
* Opportunity to become an integral part of an innovative TV solution.
* A chance to learn from the best in the industry.
* Full time job in the Tel Aviv area.
* Unlimited supply of food and beer. Really.
* Flexible work hours.

If you like what you see and have da skillz, drop us an email at
Or, call me tel: +972 542124335.

Good luck!

Sunday, February 2, 2014

Karma Rails Angular Jasmine testing - set up in 5 minutes

It took me about an hours and a half because google is not up to date, so here it is:

First - as a rails developer, if you don't have node.js, this is the time to install it.
If you don't use RubyMine as you IDE, I recommend it as well, but whatever you prefer is fine by me.
Here you'll fins instructions about karma installation, follow them carefully.

You'll see that you need to tell karma where is your sources and test sources.

I'm working with jasmine, and using angular as a gem, check my configuration:

frameworks: ['jasmine'],

        // list of files / patterns to load in the browser
        files: [



To things you should notice:
1. I use rails assets pipeline to give all my app code js, (just run your server before you test)
2. I add the angular mocks lib

All other stuff are basic, I got code in coffee and js so needed a little more lines to find all my tests.

this is the way to make this work pretty fast.

Sunday, January 12, 2014

Export Hebrew (or any non english language) with php to excel

I found it very annoying that php data to excel is not so straight forward as one might think ,lots of wrong stuff here.

The thing is, as a developer you don't know a lot about encoding.

So you don't really think about it until you have bugs, than you start searching for a better solution, I found this.

Meaning you manually convert encoding to support excel, and add BOM.

Just wanted to gather a few links together for future use.