Mark Grealish

the life and code of a crazy cat man


I met a doctor walking his horse

in photo

We chatted together for a while along the road to Glencullen, then went our ways.

Doctor and Horse

Ow, my legs

in photo

17km there and back to Glencullen, but by Christ, I had the weather for it.

Walk to Glencullen

instasort 4.0

in code

Another year, another version. My growth as a programmer strikes me when I compare previous versions with the latest code. Less code which works better. You may compare the last three versions together on GitHub.

#!/usr/bin/env zsh

#
# Functions
#

#
# Check if Photograph is (Approximately) Square
#
# While it was originally written to detect explicitly square images, it will
# now detect squareness within a range of 20 pixels. Uses the absolute value
# of the size.
#

function is_square {
    local WIDTH=$(identify -format '%w' $1)
    local HEIGHT=$(identify -format '%h' $1)
    local SIZE=`expr $WIDTH - $HEIGHT`

    [[ ${SIZE#-} -le ${2:=20} ]]
    echo $?
}

#
# Create Directory from Date in Filename
# Images imported from Dropbox are named in format "2015-12-27 22.13.26.jpg"
#

function send_to_dated_directory {
    local FILENAME_DATE=${1:0:10}
    local DATE_REGEX='^[0-9]{4}-[0-9]{2}-[0-9]{2}$'

    if [[ $FILENAME_DATE =~ $DATE_REGEX ]]; then
        mkdir $FILENAME_DATE > /dev/null 2>&1
        mv "$1" $FILENAME_DATE
    fi
}

#
# Move File to Appropriate Location
#
# 1. To a dated folder if not an Instagram image.
# 2. To the Instagram folder otherwise.
#

function move_file {
    local EXTENSION_REGEX='^jpg$'

    if [[ ${1##*.} =~ $EXTENSION_REGEX ]] && [[ $(is_square $1) == 0 ]]; then
        mv $1 "$INSTAGRAM_FOLDER"
    else
        send_to_dated_directory $1
    fi
}

#
# Variables
#

UPLOADS_FOLDER="${HOME}/Dropbox/Camera Uploads/"
INSTAGRAM_FOLDER="${HOME}/Dropbox/Photos/Instagram/"

cd "$UPLOADS_FOLDER"

for FILE in *.*; do
    if [[ ! -f "$FILE" ]]; then
        continue
    fi

    NICENAME=$((tr '[A-Z]' '[a-z]' | tr ' ' '_') <<< "$FILE")
    mv "$FILE" $NICENAME && FILE=$NICENAME

    case ${FILE##*.} in
        gif|png) rm $FILE;;
        *) move_file $FILE;;
    esac
done

Avoid N + 1 issues in Ruby on Rails when checking existance

in code

How perfect and clear, hindsight! This was another “oh, duh” moment for me. Stupid means humble means another step closer to enlightenment, so that’s good.

In our application a user may favourite content in a manner à la Facebook. When rendering views, the naive way (the Mark way before this post) is to test for existence using exists?:

Returns true if a record exists in the table that matches the id or conditions given, or false otherwise.

Emphasis mine. @sparkle.like.exists? id: @user.id will hit the database each and every time I call it. In the case of our seeded data, for each given view there are:

  • 1 parent Group.
  • 10 Sparkles with 5 Comments each.
  • 10 Rainbows with 5 Comments each.

There will be an extra

(10 * 5) + (10 * 5) + 10 + 10 + 1 = 121

in each view, and that in seeded data with a minimum of records. The extra hits on the database added whole seconds to rendering! This was not optimal.

Solution

There aren’t many good Stack Overflow posts which detail this problem, hence this post. My own solution is to include the association with the initial query, and from there query whether the collection includes the given association:

@sparkle = Sparkle.include(:likes).where(id: 1).first
@sparkle.likes.map(&:id).include? @user.id

While this is a Good Enough solution for small-medium applications, it still loads a crapload of data for each page. What if @sparkle has a thousand likes? Five thousand?

Ware Ye

  1. This fucks up all existing tests for existence.
  2. This fucks up the tests because include? only looks in the array in memory. If you didn’t load the association, then you don’t get a true answer.

Listos Solution

Check inclusion instead of existence.


Ella and Oisin

in family

Taken at Eyre Square in Galway, May 1 2017.

Oisin
Ella

Some shit you cannot make up

in science fiction

I mean, while David Zindell did make up this shit, I didn’t make up my telling of it. Before you lies a description of the mighty dick of a race of hermaphroditic transhuman seal-people.

All Agathanians possess membrums, huge ones with triangular red tips...