Han Loong Liauw Mostly abandoned and incomplete thoughts

Use hashes ActiveRecord:Enum

Did you know you can use Hash instead of Array to define enum’s in ActiveRecord? Well I recently did and it’s pretty rad.

# So instead of
class Post < ApplicationRecord
  enum status %w(draft published)

# You can use
class Post < ApplicationRecord
  enum status { draft: "draft", published: "published" }

You might ask “why? that looks worse”. But when you look into the database

# using array
   id   | status
    1   |        1
    2   |        0
    3   |        1

# using hash
   id   | status
    1   | published
    2   | draft
    3   | published

Much nicer yeah?

There is some performance overhead as integer comparison is faster then varchar in most databases but with well selected indexes it shouldn’t give you too much grief.

Finally, when your model needs a new state adding a new value to the enum is more explicit.

class Post < ApplicationRecord
  enum status {
    draft: "draft",
    reviewing: "reviewing",
    published: "published"

Removing the potential bug where changing the order of an enum changes the meaning of the data backing it.

Github 2 factor auth

Slowly but surely I have been moving all my main work related apps over to using 2 factor authentication. This was something I was pretty slow to adpot as it seemed like a real waste of time to check my phone everytime I log into google apps. However I bit the bullet and made the switch, mostly. The last one I held out for was GitHub, which as a developer for a company that uses github is the place where security was most needed. I think my resistance was due to the fact that I access github in a few different ways. There is of course the Github.com website (which would be the same as using google apps or any other 2FA system) and there is the command line (not a big use of the desktop client).

The CLI for git and github is where I do most of my github interaction and not being able to get that up and running would be a major pain. But I have also bitten the bullet and taken the leap and other random cliche and am now using it. It was actually pretty easy to setup so I wanted to share the instructions as the current github help files require you to jump around to find the solution.

My Setup

  • MacOS
  • SSH Authentication
  • and thats it

If you are using https git access github I think the steps are a little different and possibly simpler.

1. Setup 2FA on GitHub

There a great instuctions here but the basic steps are

  • Visit you account page on github.com
  • Scroll to the bottom and you should see a button to setup 2FA
  • Follow the wizard

2. Setup you CLI access

Instead of using the second verification step to authenticate through SSH you will authenticate with github as if you were an API. But instead of passing the authentication token a a special header you will replace the password you previously used with a “Personal Token”

Here is the link to the github help pages for creating a personal token.

As this would usually be the mean editing interface into github you should probably be pretty liberal with granting access for this personal key.

3. Reset local Authentication

I used a password caching tool to prevent me from having to authenticate over and over to save time, however this means that I no longer enter my password so its a bit harder to change the credentials previously entered. So here was my quick cheat.

  • Opened my global git config git config --global -e
  • Removed the [Github] section of the config that specified my username
  • Saved the file and exited
  • Tried to perform a git pull from a private repo
  • Entered my username when prompted
  • Enetered the Personal Access Token for my password
  • Celebrated with a little dance as everything now works

Back to School

In the past few months I have switched jobs, languages (programming that is), version control system and last but not least my editor. Its been quite an adjustment but I have never been more excited about being a software engineer. However I don’t want this to come off as a problem with my last job because it wasn’t the job, it was me. I had become stuck and I needed a reason to push myself to improve. The people I worked with were great but I had dug myself into a hole that I needed to get out of.

So with this switch I have had to learn a lot of new stuff. I mostly used the internet for learning material and wanted to share what I used and let you know what I found helpful. Personally I’m not great at reading technical books so the majority of these are tools that have some interactive element.

Free Tools

This is a great little tutorial to get started with ruby syntax and is actually included in code schools ruby path.

If you are lucky/unlucky enough to live in a larger city there might just be a meetup group for ruby and/or rails. In Sydney there is the ruby on rails oceania group which holds free tutorial classes and help you get ruby and rails setup on your laptop which can be a challenge for newcomers. ReInteractive is a rails shop that is heavily involved in the Sydney community and take people through building a blog in rails.

This one is a popular choice but has tuns of great information and is constantly the first result in google when I’m searching for something.

So this resource is probably for people a bit further along the ruby road but its a excellent resource for diving deeper into topics.

This one is also for people a bit further along but is a great site created around the idea of code kata and code review. The basic concept is that there are a bunch of challenges that you attempt and each challenge has a completed spec or test file. Then once you complete your program and all the tests pass you submit it to the site and the other members of the site will provide feedback. Also its a great way to get an introduction to TDD as all the tests are written and all you have to do is uncomment them and then Red, Green, Refactor. As someone fairly new to TDD it was a excellent way to see the benefits of the process.

This is where I started my learning and to be honest it was a great place to start. Everything happens in the browser so no need to setup your system and get lost down the rabbit hole of why RVM is complaining in your shell or why postgres won’t start because the default port is currently being used by another program.

It allows you to focus on the task at hand and steps you through the concepts in a great way. Initially I used this for Rails, Ruby and Git but have gone back through some HTML/CSS classes and even tried out the NodeJs and Clojure content they have.

Price US $ 29 Month

This might be a bit of Assuie pride but I have a lot of respect for the Envato lads and they have built a pretty great tutorial site with great tools on rails, node, javascript, php and most other popular web technologies.

** Price $19 month**

If you are learning rails there is no doubt you will come across railscasts. It is a pretty comprehensive library for using rails and rails gems. Its not a tool for learning from ground up but if you need to know how to use a library or “gem” its great. There are some free videos but because I found it so useful I am happy to pay for this one.

** Price $9 month** (however on hiatus at time of writing

So I am part way through one of the courses Applied Ruby Theory but have found it pretty good. My goal was to get better at ruby outside Rails because I think its a common trap for programmers learning rails to get stuck in that framework. This course also teaches some TDD principles which is a big interest of mine. However it is a little expensive but the guy who runs it is great and you can take a much time as you need to complete the course.

Price $19 month

So this one I tried for out for a month but as I was paying out of my own pocket I couldn’t justify the expense. However if you can afford it or are serious in being great in rails I would highly recommend learn prime. I have a lot of respect for the thought bot guys and most of my vim setup I got from @r00k. So I could see this one as something I would do in the future to improve my BDD/TDD skills.

Prices Start at $99 month


And to keep up on the news I usually listen to podcasts on my commute. Here are my go to’s at the moment.

I will attempt to update this post as I remember tools or finish courses..

Gzip Rails and Heroku

This is a call to action for all those of you who are running your rails app on heroku without gzip. PLEASE ADD GZIP to your servers. If you are on heroku its as easy as adding a gem to your Gemfile and it will make a greater speed improvement on your site then a day of optermising queires will do on the backend.

Gzip has been supported in browsers for ages and was even rolled into internet exploere 6 (although with some bugs) so its definatley production safe for all your sites.

The site I am working on is not a javascript heavy site yet thanks to some crazy and sometimes useful (SpinJs check it out) the app has around 800KB of uncompressed static CSS and Javascript.

Enter webpagetest.org and heroku deflater.

Web Page Test is a great site for analysing your sites performance and is great for all the mac users as it can give you a screenshot of your site using internet explorer 9 or 10 without having to borrow someones pc or fire up a VM.

Heroku Deflater is one of many heroku rails based compression rack middleware gems but it was quick and easy to setup and had decent defaults for our needs. The whole process of setting up the gem testing it on staging then creating a pull request for master happened in the space of my company’s friday meeting.

To install add this to your Gemfile

gem 'heroku-deflater', group: :production

Once implemented I found that our page size was almost halved and I’m sure this could be improved even further with some tweaking.

Let me know if you have used this gem or similar ones and what results you have found

those last two are maybe not so useful

Re-Learning to Program

Okay so here is the first post on this new site. I have been meaning to do some sort of writing for years but never felt I had anything of real value to add, and somehow the act of blogging seemed a bit self involved.

so whats changed?

Well, I have been a programmer of varying competence for many years and although I was working open source platforms and languages I never participated in the community.

In an effort to re-invigourate my interest in my profession I left my old PHP job working on legacy software and dived into the world of ruby (and yes Rails). A few months before I finished up at my last position I had been burning the midnight oil playing around with some great online learning tools when it hit me that I was enjoying the learning I was doing at home so much more then work that I decided to see if I could make my work as enjoyable as my home work.

Not too long after I was able to asked to join a small Rails development team at a startup here in sydney and this is where I am now. It’s early days yet but its been a great experience so far and I feel I have learnt more in the last couple of month then the last couple of years.

goals for the blog

I’m well aware that most of this might be usless information for the majoirity of people but I feel that inorder to have a chance of suceeding that I should set some goals for what I hope to accomplish, and what better way to remember these goals then to publish them.

  • Share and review the learning resources I have used.
  • Provide guides and mini tutorials for things I haven’t found learning resources for.
  • and lastly to selfishly record this for my own personal reflection sharing is caring or something like that

I have been truly amazed in the amount of support, dicussing and sense of community that ruby programmers seem to have. It has helped me get started and made we want to stay. So this is my way of trying to give back.