Archive

Coding Bits

If you’re using Activerecord Enums, it’s common to store the values that represent the enums as integers in the database. If you wanted more guarantees from the database, you’d store the values as SQL enums. However, Rail’s schema dump doesn’t SQL enums very well.

Here are some tradeoffs:

Integers SQL enums
Database Guarantees No Yes
schema.rb Supported Not supported (need to use structure.sql)

Here’s a good tutorial on how to use Activerecord Enums for either case.

I’m new to both Kotlin and Android development. Since my upcoming job will focus on building an Android app in Kotlin, I’m going through some tutorials to ease my ramp up.

A lot of Kotlin+Android tutorials assume the reader is already familiar with Android development. Unfortunately, I need a gentler entry.

As someone who’s familiar with web development and knows a little Java, here’s what I found to be helpful:

    1. Kotlin Koans – Forget about setting up your favorite text editor to run Kotlin, and just dive right in. The online Kotlin playground gives you a taste of the language.
    2. Codelabs for Android Developer Fundamentals (V2) – I’m not sure how much Google will maintain these codelabs, but for now, they’re a solid introduction. All the sample code and tasks are in Java, but you can easily write everything in Kotlin.
    3. Keddit — Intro: Learn Kotlin while developing an Android App – This tutorial assumes you’re comfortable with Java and Android development. It gives good advice on how to interface with various libraries.

Other resources I’ve heard are good (but haven’t dug into yet):

  1. Kotlin in Action – I’ve seen this book mentioned in a lot of places but haven’t had a chance to read it yet.
  2. Lynda Courses by Chiu-Ki – I met Chiu-Ki at a local Meetup. She’s been mentoring a lot of junior developers in the area. I heard from the junior devs that they found her Lynda courses to be super helpful. I haven’t watched any of the lessons yet, but they seem like a good resource.

 

I’m still in the early stages of learning and will revise this post once I’m a couple months in.

Got other suggestions? Let me know!

In FactoryBot, you can reference fields of a factory when defining other fields of the same factory. That helps reduce code duplication!

For example, suppose you have a Person class that has the fields first_name and full_name. You can define the Person factory so that full_name will reference first_name.

That looks like this:

factory :person do
  sequence(:first_name) { |n| "foo#{n}"}
  full_name { first_name.capitalize + " Smith" }
end

This was tricky to figure out, so I’m jotting it down here!

When you’re running a simple Flask app in development, you might specify a particular port where the server should run (the default is 5000).

Then in your browser you can visit localhost:5000/foobar.

But in Flask tests you don’t need to specify the port, just the route:

from flask_testing import TestCase

class TestFoobar(TestCase):
    def test_foobar(self):
        response = self.client.get("/foobar")
        # ...

That’s because, underneath, Werkzeug test clients don’t issue actual HTTP requests. They abstract that away.

Source: this Stack Overflow post.

Sometimes you just need long strings.

In tests, I sometimes like to include tiny CSVs as a part of the test code instead of a fixture file. However, that looks real gross real fast with all the linebreaks.

Heredocs to the rescue!

In Ruby, instead of:

sample_csv = "foo,bar,baz\na,b,c\nd,e,f\n,g,h,i"

You can do something like:

sample_csv = <<~EOS
   foo,bar,baz
   a,b,c
   d,e,f
   g,h,i
EOS

Isn't that much better?