Ckeditor with Amazon S3 and paperclip.

I was working on project recently, where I had to use CKeditor with paperclip and amazon s3 as the storage. This all is running on Heroku. The solution is really easy. You have the paperclip in you model…

I was working on project recently, where I had to use CKeditor with paperclip and amazon s3 as the storage. This all is running on Heroku. The solution is really easy. You have the paperclip in you model

has_attached_file :avatar,
 :styles => { :big => "180x180#", :medium => "140x140#" },
 :storage => :s3,
 :s3_credentials => "#{Rails.root}/config/s3.yml",
 :path => ":attachment/:id/:style.:extension",
 :url => ":s3_domain_url"
And you have something like this in the picture.rb and attachment_file.rb (app/model/ckeditor/)
has_attached_file :data,
     :url => "/ckeditor_assets/attachments/:id/:filename",
     :path => ":rails_root/public/ckeditor_assets/attachments/:id/:filename"

So solution is obvious now:

has_attached_file :data, :styles => { :big => "180x180#", :medium => "140x140#"},
 :storage => :s3,
 :s3_credentials => "#{Rails.root}/config/s3.yml",
 :path => ":attachment/:id/:style.:extension",
 :url => ":s3_domain_url"

That’s it.

Pair programming with your mind

My last post was about pair programming. Recently I start working on a small project where I can’t pair with anyone. So I started thinking “what could I use from pairing even When I’m not ? “.

My first thought was to act as if I was, so I would have to act as 2 different people. I should change my t-shirt and wear a hat or just put on glasses and change my hairstyle every time I change from one persona to another, but this could look really strange and potentially have detrimental consequences.

Another thought was, “Hey what are the advantages of pair programming which don’t depend on the other person directly” ? The answer is a better understanding of code, cleaner code and faster development. But how exactly would I achieve this? Let’s find out!

Better understanding of code

Better understanding of code is based on the fact that you explain your implementation to your partner. You can still do this, and very often you are actually doing it without noticing.

If you are speaking or just mumbling to yourself out loud then your brain is using different parts then if you were just “thinking” about the problem. So if you ask yourself out loud “Why doesn’t this work?” or “Ok this points to that if the value is….” then it is same as if you were speaking to someone else. Even if you ask yourself “What would Mr X do?”. Than you force yourself to look at the problem from a different perspective it would also help you.

Faster development

In pair programming you usually switch between each other for short time periods or small problems. This could be easy implemented and the answer is sprint. Pick small timeframe (10 minutes) in which you can implement some functionality. Then you set your watch or phone or some tools that you can download and start. You will see that you are trying to finish your code in the time limit (including your tests of course ;-) ) which will speed up your programming.

Cleaner code

Cleaner code is basically only about you, and because nobody is watching, you tend to not care that much about sanity of your code.

When I start programming in Ruby my friend who taught was really strict about code sanity. Every time when I paired with him, he would point out every small thing e.g. end of the line, space between operators etc.. So I was forced to care. Since then even when I’m not pairing I always make sure my code is clean enough to satisfy his sanity check. So imagine somebody who’s got your respect and is really anal about clean code. Try to not disappoint him.

These three rules will help you in your “virtual pair programming”. If you have another technique feel free to leave a comment!

7 reasons why pair program

I was wondering why lot of people still don’t want to pair program. So I wrote down my most important reasons why you should pair program. Btw if you don’t know what is pair programming, here is explanation.

I was wondering why lot of people still don’t want to pair program. So I wrote down my most important reasons why you should pair program. Btw if you don’t know what is pair programming, here is explanation.

Faster learning – if you pair program with more experienced developer you will learn more faster than you would learn alone.

Faster development – Even thought sometimes pair programming may be slower (pair senior / junior), in general it’s faster. Two people working on same issue have different ideas how to solve the problem or they can suggest better and faster solutions.

Learn new things – It doesn’t matter if you pair with skilled or beginner developer. Every person thinks differently so you will learn new techniques or just different approaches on how to solve the problem.

Better understanding of code – If you pair program you have to explain what are you writing and why. That will help you think more about problems and their solutions.

Cleaner code – Pair programming usually forces you to write cleaner code, because you are watched and you don’t want to look like bad programmer, do you?

Bus factor – Pair programming help you increase the number of people that could be “hit by a bus” without affecting productivity, ie more people have knowledge of code base. (Bus factor explanation)

Eyes will rest – Yes this is good reason too. If you pair program you usually don’t stare at the monitor for long intervals (which help you eyes), because you have discussions with your co-worker, about the issue, that you are solving.

So are you finally convinced to pair program ?

How to tag SVN branch on the server from Git repository

Image the following situation:

You are using SVN on the server and to deploy on the server you have to TAG release. You are using Git on your local machine and you have following commits.

Image the following situation:

You are using SVN on the server and to deploy on the server you have to TAG release. You are using Git on your local machine and you have following commits:

Task is to create the SVN branch from first red dot and than add the another commit marked with the black dot.

Git version

To do that in Git  is what you would expect:

reset the master to last commit that you need (red),

1: git reset --hard SHA_code

than create and switch to new branch

2,3: git checkout -b name_of_the_branch

and cherry-pick the commit/s.

4: git cherry-pick -x SHA_code

Git SVN version

Reset master to red dot

1: git reset --hard SHA_code

create and switch to new branch

2: git svn branch name_of_the_branch (this create branch on the server)
3: git checkout -b local/name_of_the_branch name_of_the_branch

and cherry-pick the commit/s.

4: git cherry-pick -x SHA_code

and push changes on the server

5: git svn dcommit

to tag this branch on svn type

6: git svn tag tag_name

Note: if you did not checkout the branch in proper way (svn way – step 2) you will tag the master branch

Fix your broken window!

As a programmer working with other programmers, I’m usually contributing to an already existing code base and basically just adding new functions or extending their functionality and because I’m person who could be influenced by others opinions, I also could be influenced by others code and code formatting.

For a moment try to remember what your code looks like if you are writing brand new project and how, after a couple of weeks, this code is still nice and shiny even if you are working with 2 other programmers. Now back to reality where you working at old project where is lots of ugly code. Do you think that project start with ugly code? No, usually it doesn’t. So what changed that nice code to ugly one? Why even if all of programmers are good and writing nice code does, this happened? The reason is what’s called the broken windows theory.

Broken windows theory as the name suggests is about broken windows(not the MS Windows though). Imagine a street with nice houses and white fences. One day, somebody brakes the window of one of the houses. After few days, nobody has fixed the window because the house is empty, even though it looks very nice. One of your’s neighbours kids then brokes another window and because there is already one broken, nobody cares that much and time goes by. After couple of months the house has a broken fence, a couple of windows and garbage all around it. Soon this ugly house will affect the behaviour of your neighbours as the surrounding area deteriorates. You may start to care about cleaning sidewalk, but by then, the whole street has started getting ugly and uglier.

This story represent exactly what happen to you brand new nice code if somebody brakes the window and nobody fixes it.

So behave to your code as a good neighbour and try to leave your code in cleaner state than was before your changes, the whole area will be nicer as a result.

I’m not working

Yes that’s right, despite the fact that I’m employed by Unboxed I’m not working anymore. I’m going to work every day, programming some stuff but it’s not work for me anymore. Really.

I have had this feeling since I start working with Rails and this feeling became stronger when I came to London and joined Unboxed. I had heard about Agile and Scrum and I had tried to practise TDD and BDD before, but it’s really different here and now.

I joined the real world of Agile. I was so excited (and scared) when I had my first standup and my first retrospective. I’m still excited, because everyday is different. One day we have to do standup without our Product Owner (PO) because he is stuck at the tube. On another day, I was alone since everyone else was on holiday. Is it still a standup if only one developer and the PO are present? I think that it is, because the important thing is that I can tell him what is going on and another day of excitement can start.

Of course there are days when I’m tired, but you can be tired even after sitting on the beach with a Mojito in one hand and great book in the other. At the end of a tiring day at the office I look back and discover that I’m tired because I experienced so many new and interesting things.

So what am I actually doing if I’m not working? I’m a little bit scared that I might be dreaming. I’m scared I will wake up in my cubicle with PHP code (without tests) on my screen and without any slight hint of Agile or Scrum and Waterfall knocking on my door. So please don’t wake me; I like it as it is!

Proper error messages on rails

In order to have proper validation error messages on the web page I want to specify what the message will look like. Problem is that if you set validation message in your Person model like this:

validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :message => ‘invalid e-mail address’

you get on you page error message like this:

1 error prohibited this person from being saved

There were problems with the following fields:

‘Email invalid e-mail address’

but customer wants something more human like., for example:

Oh dear there were problem

That doesn’t look like a valid e-mail address. Please try again.

I found a solution and here is how to do that. First we change the Header error message and delete the submessage.

In your view where you are displaying errors use proper options for error_messages_for

error_messages_for(:person, :header_message => “Oh dear there were problem”, :message => “”)

Than the interesting part, error messages are in this format [attribute_name, message] but we need only the message part so without any hacking or parsing we can specify format of the message directly:

validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :message => :”email.format”

The part :”email.format” says that you have specified message text at your i18n file (usually config/locale/en.yml) .

en:
  activerecord:
    errors:
      full_messages:
        email:
          format: "That doesn't look like a valid e-mail address. Please try again."

And that’s it :) Feel free to comment.

Hiring as a game

I saw an interview with Amy Jo Kim at Mixergy.com about game mechanics,recently. Today I saw Slideshare called Designing a Game Changer by Philip Fierlinger from Xero. At the and of the presentation is short notice that Xero is hiring.
Than I just got an idea about making from hiring a game.
Here is example situation:
You are working for big company as a HR person or something like that and you need hire few programmers. As a clever HR person you come out with game called “win a macbook” or some catchy. Rules of the game is simple.
Write some algorithms or complete some tasks. If you want to be more precise you will write more levels of this game and provide some kind of feedback.
The winner of course win the price and you can offer him and other best solvers the job.
If you are lucky some solvers accept your offer. In the end you will have great programmer and save some money for personal agency.
This principle is applicable in larger scale and has of course some difficulties but is just an idea. :)