Someone coding

How to delete local git branches after the merge

From time to time we all get annoyed by the git branch autocomplete, which used to be so useful, but now makes us practically write the whole branch name! 😡

Why it keeps happening? Because we are awesome developers who use feature branch flow and now, after working so hard, we have dozens of branches on our local machine.

Yogo, can you tell me an easy way to clear these branches? I don’t wanna delete each one manually! 😩

I sure can, my friend! Here is an alias that I use to delete all my merged branches:

alias clear_merged_branches="git branch --merged | egrep -v '(^\*|master|production)' | xargs git branch -d"

It will only leave the “master” and “production” branches, which you can easily change on that code. Besides the branches that haven’t been merged into master, of course.

Remember to use this alias while you are on the “master” branch 😉


As lpmusix pointed out, you can also use it as a git alias. Add to the alias section of your ~/.gitconfig:

cb = "!git branch --merged | egrep -v '(^\\*|master|production)' | xargs git branch -d"

Ruby already has its own regular expression to validate emails

I’ve searched and written a lot of regular expressions to validate email on our models and forms but recently I’ve found out that Ruby already has a good one and its easy to access and use.

All you have to do require uri library and use its constant:

require 'uri'

It’s also easy to use on an Active Record format validation:

validates :email, format: { with: URI::MailTo::EMAIL_REGEXP, message: "only allows valid emails" }

It even accepts emails with “+” as gmail enables.


How to solve rails 4 UndefinedTable Error when creating namespaced models

In a given rails 4 application, I have two namespaced models with a has_many association between them:

# models/review/asset_type.rb
module Review
  class AssetType < ActiveRecord::Base
    belongs_to :review_asset_category, class_name: Review::AssetCategory

# models/review/asset_category.rb
module Review
  class AssetCategory < ActiveRecord::Base
    has_many :review_asset_types, class_name: Review::AssetType,
             foreign_key: 'review_asset_category_id'

With migrations as:

class CreateReviewAssetCategories < ActiveRecord::Migration
  def change
    create_table :review_asset_categories do |t|
      t.string :name
      t.timestamps null: false

The migrations ran well but everytime I ran the tests I received the error:

Failure/Error: it { respond_to(:name) }
       PG::UndefinedTable: ERROR:  relation "asset_categories" does not exist
       LINE 5:                WHERE a.attrelid = '"asset_categories"'::regc...
       :               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                            pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                       FROM pg_attribute a LEFT JOIN pg_attrdef d
                         ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                      WHERE a.attrelid = '"asset_categories"'::regclass
                        AND a.attnum > 0 AND NOT a.attisdropped
                      ORDER BY a.attnum

The solution is simple, just add this class method to each model or to the module that is namespacing the models:

module Review
  def self.table_name_prefix

I hope this simple advice will save some time for other developers 🙂

MegaMaker Challenge

Justin Jackson started the Mega Maker Challenge a week ago and I joined right away:

The new year is here. How can you make this year better than the last? For me the answer is to create new things. I don’t want to “just be a consumer.” Making things, and releasing them to the world, gives my life purpose.

This year, I’m challenging myself to make 100 things. Want to join me?

Mega Maker Challenge logo

I sure want! And you can as well. As he says, you don’t need to create 100 things, you can challenge yourself to create as many as you want.

He even created a newsletter with good insights to help you conquer this challenge. Join the challenge and let’s make all the things!

Exercism – Practice your dev skills

A week ago I  was trying to find some algorithm exercises and I found, a website that helps you practice your developer skills using your terminal, your favorite IDE and TDD 🙂

Captura de Tela 2015-12-28 às 18.12.33

Besides the TDD approach, what I really liked about it is that it encourages you to solve problems iteratively, one test at a time and when all tests are passing, refactor your code for a better solution.

It also lets you discuss each others solutions and I’ve already received some nice advices. Some exercises also made me learn some new methods that I’ve never used before like reducetr and learn that count can receive a block!

I’m practicing Ruby but they have exercises for a lot of other languages like: PHP, Lua, Lisp, F#, Javascript and Elixir.

I suggest you try it and then let me know if you liked it or not and if you did, comment here your profile so we can try to help you improve your coding skills 🙂

ps: you can also see my answers and help me improve mine

Useful RSpec matchers that you may not be using

Just sharing a couple RSpec matchers that I think are very useful but people doesn’t seem to know they even exist 🙂

all matcher

When I wanted to make sure that every element of an Enumerable, for example, should be an instance of Course, I would write:

it 'populates @catalog_courses only with courses' do
  assigns(:catalog_courses).each do |course|
    expect(course).to be_a(Course)

But with the all matcher, its just:

it 'populates @catalog_courses only with courses' do
  expect(assigns(:catalog_courses)).to all( be_a(Course) )

I used the be_a matcher inside the all but you could use any other like: be_truthy or eq.

contain_exactly matcher

When I wanted to make sure that an Array should include a and b elements but in any order I would write something like:

it 'populates @courses with courses a, b and c' do
  courses = assigns(:courses)

  expect(courses).to include(a)
  expect(courses).to include(b)
  expect(courses).to include(c)

Now, using the contain_exactly matcher:

it 'populates @courses with courses a and b' do
  expect(assigns(:courses)).to contain_exactly(a, b, c)

Hope you learned something new here. Leave in the comments any other awesome matcher that you think other people may not be using 😀