LoveWithFood Tech

LWF

Git_pretty_accept: Accept Pull Requests the Pretty Way!

| Comments

A few months ago, I suggested the Simple Git Branching Model and Best Way To Merge A (GitHub) Pull Request to Hendy, our CTO. We were using GitHub’s pull request feature for our code reviews, and as great as that feature was, it was turning our git history into, sorry for the pun, spag-git-ti. With the Simple Git Branching model, I was hoping for a more linear, more readable git history, with visible feature branches that can be easily reverted.

Hendy liked the idea, but felt that the workflow needed to be automated. With a script, accepting pull requests will be consistent and error-resistant.

And so, after several weeks of testing and usage, we’re happy to share git_pretty_accept.

git_pretty_accept is a ruby gem that rebases a pull request before merging it to master. The pull requests are always merged recursively to make branches visible in the history.

To accept a pull request, just run git pretty-accept BRANCH_NAME in the branch where BRANCH_NAME will be merged.

You can also provide a merge commit template .git-pretty-accept-template.txt to your project directory. This is great if you want your merge commits to follow a certain format e.g. you want your team to enter the issue link when accepting pull requests.

We wrote git_pretty_accept for our particular workflow. If you’d like to use the gem, here are a few things you’ll need to keep in mind. The script

  1. Assumes that your remote repository is origin. It doesn’t work yet with Github forks.
  2. Automatically removes the local and remote branch of the pull request once the pull request is merged.
  3. Complains if you accidentally try to accept the master branch.

Let us know what you think! Pull requests are most welcome.

Five Steps to Organize Your Translations by Resources

| Comments

Recently, we noticed that our translations were getting disorganized as more categories and subcategories were added like below.

1
2
3
4
5
6
7
8
9
10
11
12
13
# config/locales/en.en.yml
en:
  coupons:

# config/locales/activerecord.en.yml
en:
  activerecord:
    errors:
      models:
        coupons:

# config/locales/simple_form.en.yml
en:

As more resources (models, service objects) were being added, it was getting time consuming to keep track of all our translations. Furthermore, we were running the inherent risk of duplicating translations with a large translation file. Our solution then was to organize the translations by splitting each resource to its own translation file. In the next five steps, we will show you how you can reorganize your translation file making sure no translation is missing.

Step 1 – Back up your translations

Duplicate config/locales as config/locales_new

Step 2 – Slave through the sorting of your translations

1
2
3
4
5
6
7
8
9
10
11
12
# config/locales_new/coupons.en.yml
en:
  coupons:

  activerecord:
    errors:
      models:
        coupons:

  simple_form:
    labels:
      coupon

Well… We can’t help you with this one. You may also want to sort them in alphabetical order.

1
2
3
4
5
6
en:
  activerecord:

  admin:

  simple_form:

Step 3 – Install HashDiff gem

In your Gemfile under group :test, add require 'hashdiff'. You can read more about the gem here.

Step 4 – Convert locale directories to hashes

Add a TranslationsDirectory class under spec/support to convert config/locales and config/locales_new to hashes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# spec/support/translations_directory.rb
require 'hashdiff'

class TranslationsDirectory
  def initialize(path)
    @path = path
  end

  def to_hash
    result = {}

    Dir.glob("#{@path}/*.yml") do |file|
      if result.empty?
        result = YAML.load(File.read(file))
      else
        result = result.deep_merge(YAML.load(File.read(file)))
      end
    end

    result
  end
end

The method to_hash basically looks through the directory for all files ending with.yml and merges them into a single hash.

Step 5 – Write a test to make sure no translations are missing

Under spec/config:

1
2
3
4
5
6
7
8
9
10
11
# spec/config/locales_spec.rb
require 'spec_helper'

describe 'Difference between old locales and new locales' do
  it 'should be empty' do
    old_trans = TranslationsDirectory.new('config/locales')
    new_trans = TranslationsDirectory.new('config/locales_new')

    expect(HashDiff.diff(new_trans.to_hash, old_trans.to_hash)).to eq([])
  end
end

Once that’s done, run the test and watch it pass! If it doesn’t, HashDiff.diff(new_trans.to_hash, old_trans.to_hash) will generate an array showing duplicated, additional or missing translations.

That’s all from the tech geeks at Love With Food. Let us know what you think by leaving a comment below.

Setup

| Comments

This post is a step-by-step explanation of how this blog was set up. Octopress documentation explains all of this. However, if you keep reading here, you’ll get a method to create this exact blog.