Create idempotent migrations

When you want to write destructive migrations please use if_exists: true when you’re trying to remove a table and check for a table existence when you want to add or remove a column ActiveRecord::Base.connection.table_exists?

Case 1:

def up
  drop_table :kittens, if_exists: true
end


Case 2:

def up
  return unless ActiveRecord::Base.connection.table_exists?(:kittens)
  add_column :kittens, :kind, :integer
end


Ali

Add spacers to your macOS app dock to make groups

Open term (or equivalent) and paste the following one-liner:

defaults write com.apple.dock persistent-apps -array-add '{tile-data={}; tile-type="spacer-tile";}'; killall Dock


When the Dock restarts, there should be a draggable blank space that you can move anywhere. Repeat to your heart's desire.

2
Jeff

Send messages to Slack using cURL

Slack allows you to send messages via the Webhooks service

Create a webhook for any channel and then you can send notification using curl.

$ curl -X POST -H 'Content-type: application/json' --data '{"text": "Something important"}'  https://hooks.slack.com/services/YOUR/TOKENIZED/URL


Joe

If you want to override previously set order

If you want to override previously set order (even through default_scope), use reorder() instead.

E.g.

User.order('id ASC').reorder('name DESC')


would ignore ordering by id completely

Ali

List directory structure except one or more dirs

Ever wanted to display the structure of the current directory except or two directories?

#NPM or yarn project
tree -I node_modules


# Elixir Mix project
tree -I 'deps|test'


Ali

Calculate anagrams in Elixir

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once

For example:

  • "restful" = "fluster"
  • "funeral" = "real fun"
  • "adultery" = "true lady"
  • "customers" = "store scum"
  • "forty five" = "over fifty"

Create a new project using mix new anagram then let's write some test cases:

defmodule AnagramTest do
  use ExUnit.Case

  test "calculates anagram" do
    assert Anagram.anagram?("restful", "fluster") == true
    assert Anagram.anagram?("funeral", "realfun") == true
    assert Anagram.anagram?("adultery", "truelady") == true
    assert Anagram.anagram?("customers", "storescum") == true
    assert Anagram.anagram?("fortyfive", "overfifty") == true
    assert Anagram.anagram?("fiftyfive", "overfifty") == false
    assert Anagram.anagram?("funeral", "real fun") == false
  end
end


Now we have to write the code:

defmodule Anagram do
    def anagram?(a, b) do
    charlist(a) == charlist(b)
  end

  def charlist(w) do
    w |> String.trim() |> String.downcase() |> String.graphemes() |> Enum.sort()
  end
end


Run the tests mix test:

Compiling 1 file (.ex)
Generated anagram app
.

Finished in 0.1 seconds
1 test, 0 failures

Randomized with seed 752850


Done, now you have an anagram calculator!

Ali

Destructuring assignment in JS

Simple ones javascript const [a, b, ...c] = [10, 20, 100,200] // a= 10, b=20, c=[100,200] const { first, second } = { first: 1, second: 2 } // first = 1, second = 2

Crazier one javascript const resp = { data: { message: 'hello', random: 'xxx', test:'hehe' } } const { data, data: { message, ...rest } = resp // data = { message: 'hello' }, message = 'hello' // rest = { random: 'xxx', test: 'hehe' }

Tino

Run rubocop against your current branch

$ git diff develop feature/one-signal --diff-filter=M --name-only -- *.rb | egrep -v 'db|config|spec' | xargs rubocop


Ali

Use `host` to get more information about domains

You might be using dig to find out A/CNAME/MX etc records of a domain from DNS servers. But if you just want to get a quick overview, use host:

$ host oozou.com
oozou.com has address 52.89.55.136
oozou.com has address 35.165.206.160
oozou.com has address 34.212.209.65
oozou.com mail is handled by 1 aspmx.l.google.com.
oozou.com mail is handled by 5 alt1.aspmx.l.google.com.
oozou.com mail is handled by 5 alt2.aspmx.l.google.com.
oozou.com mail is handled by 10 aspmx2.googlemail.com.
oozou.com mail is handled by 10 aspmx3.googlemail.com.


2
Const

Attaching file to ActiveStorage without HTTP

If you want to attach a file you generated on disk or downloaded from a user-submitted URL, you can do it like this.

@model.image.attach(
    io: File.open('/path/to/file'),
  filename: 'file.pdf',
  # content type is optional
  content_type: 'application/pdf'
)


2
Tino