TIL

Today I Learned by OOZOU

Unique ID for DOM in Rails

Rails has many great view helpers, one of which is dom_id:
dom_id(Post.find(12))       # => "post_23"
dom_id(Post.new)            # => "new_post"

You can also provide a prefix:
dom_id(Post.find(23), :edit) # => "edit_post_23"
dom_id(Post.new, :custom)    # => "custom_post"
Const

Heredoc without interpolation

Sometimes you want to use Ruby's Heredoc without interpolation ('#{....}'). You can do this by adding single dashes around your keyword:
doc = <<-'SONG'
Business men, they drink my wine
#{person} dig my earth
None will level on the line
Nobody offered his word
Hey, hey
SONG

Const

Fix input on Heroku's rails console

If you end up pasting lots of lines into a rails console on Heroku (or else where), you might get issues with the lines "bleeding" into each other.

Next time this happens, try disabling multiline:

$ heroku run 'bundle exec rails console -- --nomultiline'
Const

Angry Docker Purge

If you receive No Space left error on Docker this the brute force solution:

It would stop and delete ALL containers, images and volumes.

docker stop $(docker ps -aq); docker rm $(docker ps -aq); docker volume rm $(docker volume ls -qf dangling=true);docker system prune -af


2
Babar

Run same command on different input/inputs

If you have to run the same command repetitively over different input/inputs.

Example input:

c595270372e3
c04094382ae1
498a6505a1f7
4ac0e1872789
170a7f2ec51a
930743cb4e19


Command:

pbpaste | xargs -I id docker image rm id


Note: pbpaste is clipboard paste command for Mac, would be different for operating systems.

1
Babar

Check for IKEA item availability

A bit of a hack, but we want to be notified if an item becomes available at IKEA.

This is just a shell script that should work on any *NIX that has curl installed.

This is how their API response looks like (replace th with your country code): https://www.ikea.com/th/en/iows/catalog/availability/S99067497

There are only two stores in Thailand, both of which are close enough to the Oozou office, we only care that it's available in one of those stores.

#!/usr/bin/env bash

url="https://www.ikea.com/th/en/iows/catalog/availability/$1"

# This is the string that we want to see changed
string="0"

while true; do
  # Get the API response and return as string
  text=$(curl -s "$url")

  # Get a count of how many times the substring matches
  count=`grep -o "$string" <<< "$text" | wc -l`

  # If less than 2, then it's available!
  if (( $count < 2 )); then
    echo "AVAILABLE, yay.";
    # Notify us via Slack
    curl -X POST --data-urlencode "payload={\"channel\": \"…\", \"username\": \"ikea\", \"text\": \"Item $1 is now available at IKEA.\", \"icon_emoji\": \":shooping_bags:\"}" https://hooks.slack.com/services/…
    break
  else
    # If not available, sleep for 30s
    echo "$1 not in stock"
    sleep 30
  fi
done



Pass in your item ID to start:

$ ./check.sh S99067497


2
Const

Create Element with attributes in one line

We normally use this kind of code to create a HTML element with JS:

const a = document.createElement('a')
a.setAttribute('class', 'foo bar')
a.setAttribute('id', 'id')
a.setAttribute('innerHTML', 'Hello World!')
a.setAttribute('href', 'https://www.google.com/')


Or some similar ways. But setting each attribute one by one is tedious!

We can do this in a single line if we want:

Object.assign(document.createElement('a'), {
  id: 'id',
  class: 'whatever class',
  href: 'https://www.google.com/',
  innerHTML: 'This is a link'
});


One more way to mess up with your JS codebase!

Babar

Change Ruby version for single command with rbenv

You can switch Ruby version temporarily with rbenv local

But it'd need to be switched back after you're done. Specially for a single command it's kind of pain. In those cases, you can use RBENV_VERSION env variable.

RBENV_VERSION=2.6.3 ruby -v


This might be useful in a shell script or so.

Babar

Go through a file Git history (GUI)

$ gitk --follow file


Ali

Reset Postgres table sequence after DELETE

When deleting recent records from a table, you can reset it's sequence counter if auto-increment field continuity is important to you. Consider actions on the following table, users: ``` select max(id) from users;

+-----+ | max | +-----+ | 896 | +-----+ 1 rows in set (0.03 sec) Then, some delete action against most recent rows: delete from users where created_at > timestamp 'yesterday';

96 rows in set (0.15 sec) `` The next auto-increment value foridwould be 897 on the next insert. Use the following command to reduce the sequence value for theusers` table by 96.

select setval('users_id_seq',(select max(id) from users));


+--------+
| setval |
+--------+
| 800    |
+--------+
1 rows in set (0.04 sec)


Jeff