Today I Learned

A CCSalesPro project TwitterFollow on Twitter

Strict local variables

Rails partials let you do strict local variables with a magic comment:

<%# app/views/customers/_customer.html.erb %>
<%# locals: (customer:) -%>
<%= customer.name %>

If you don't pass a customer to the parital, it will raise an error:

<%= render "customer", not_customer: 42 %>

You must pass the customer:

<%= render "customer", customer: %>

Run all tests in a single go file

You can use sed to find all the test functions in a go file to pass to the regex flag:

// my_api_test.go
package my_api

func TestHappyPath(t *testing.T) {
}

func TestSadPath(t *testing.T) {
}

func TestHorriblePath(t *testing.T) {
}

func TestAwesomePath(t *testing.T) {
}

Test command:

go test -run $(sed -n 's/func.*\(Test.*\)(.*/\1/p' my_api_test.go | xargs | sed 's/ /|/g')

This runs:

go test -run TestHappyPath|TestSadPath|TestHorriblePath|TestAwesomePath

Add custom inflectors for irregular words

ActiveSupport::Inflector allows you to add custom inflections for words it doesn't know how to pluralize already:

# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.irregular "evidence", "evidence"
  #   inflect.plural /^(ox)$/i, "\\1en"
  #   inflect.plural /^(ox)$/i, "\\1en"
  #   inflect.singular /^(ox)en/i, "\\1"
  #   inflect.irregular "person", "people"
  #   inflect.uncountable %w( fish sheep )
end

This allows us to use evidence as a table name with a model called Evidence

puts "evidence".pluralize
"evidence"