Ecto query using left join, group by, order by, and count

Tagged count, ecto, group_by, left_join, order_by  Languages elixir

This is an example of an Ecto query that uses a left join, group by, order by, and count to produce a count of associated records for a list:

query = from list in List,
  left_join: subscriber in assoc(list, :subscribers),
  order_by: [asc: :name],
  select: %{ list | subscriber_count: count( }
query |> Repo.all

Remember to add a virtual attribute named subscriber_count:

schema "lists" do
  field :subscriber_count, :integer, virtual: true

Copy-to-clipboard with plain Javascript

Tagged clipboard, copy, javascript  Languages javascript
var Copy2Clipboard = {
  init: function(selector) {
    var btns = document.querySelectorAll(selector);
    for (var i = 0, len = btns.length; i < len; i++) {
      var btn = btns[i]
      btn.addEventListener('click', function(event) {
        var btn =
        try {
          var textarea = document.getElementById(btn.getAttribute('data-target'))
          if (textarea == null) {
            alert("copy-to-clipboard target is undefined")
          var successful = document.execCommand && document.execCommand('copy')
          if (successful) {
            btn.innerHTML = 'Copied...'
          } else {
            alert("Press Ctrl+C or Cmd+C to copy")
        } catch (err) {
          console.log('Oops, unable to copy')

textarea id="embed-code">
  This will be copied to the clipboard.

How to use nmap to find security vulnerabilities

Tagged nmap, security, vulnerability  Languages bash
$ brew install nmap
$ cd /usr/local/share/nmap/scripts
$ git clone
$ nmap -sV -A -oX nmap-report.xml --script=vulscan/vulscan.nse
$ xsltproc nmap-report.xml -o nmap-report.html
  • sV: Probe open ports to determine service/version info
  • A: Enable OS detection, version detection, script scanning, and traceroute

Also see:

Analyze Nginx's access logs with goaccess

Tagged access, analytics, goaccess, log  Languages bash

Show all statistics

sudo zcat /var/log/nginx/*.access.log.* | goaccess --log-format=COMBINED

Show server errors

sudo zcat /var/log/nginx/*.access.log.* | awk '$9~/500/' | goaccess --log-format=COMBINED

How to test ActiveRecord associations in Rails

Tagged activerecord, rails  Languages ruby


def assert_association(model, name, type, options)
  reflection = model.reflect_on_association(name)
  assert reflection, "#{model} has no association named #{name}"
  assert_equal type, reflection.macro
  assert_equal options.sort, reflection.options.sort
require 'spec_helper'

describe Order do
  it "has associations" do
    assert_association Order, :organization, :belongs_to, {}
    assert_association Order, :items, :has_many, class_name: Order::Item

How to use the html/template package in Golang

Tagged golang, html, template, reload  Languages go, bash


  • template reloading
  • view helpers
  • HTML files are embedded in the binary
package main

import (
    rice ""

type renderTemplateFunc func(w http.ResponseWriter, tmpl string, p interface{})

var renderTemplate renderTemplateFunc
var templates = template.New("").Funcs(templateMap)
var templateBox *rice.Box

func loadTemplates() {
    if config.debug {
        renderTemplate = renderTemplateDev
    } else {
        renderTemplate = renderTemplateProd
        newTemplate := func(path string, _ os.FileInfo, _ error) error {
            if path == "" {
                return nil
            templateString, err := templateBox.String(path)
            if err != nil {
                log.Fatal("Unable to parse: path=%s, err=%s", path, err)
            templates.New(filepath.Join("tmpl", path)).Parse(templateString)
            return nil
        // Load and parse templates from binary or disk
        templateBox = rice.MustFindBox("tmpl")
        templateBox.Walk("", newTemplate)

var (
    templateMap = template.FuncMap{
        "Upper": func(s string) string {
            return strings.ToUpper(s)

func renderTemplateProd(w http.ResponseWriter, tmpl string, p interface{}) {
    err := templates.ExecuteTemplate(w, tmpl, p)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)

func renderTemplateDev(w http.ResponseWriter, tmpl string, p interface{}) {
    t, _ := template.ParseFiles(tmpl)
    t.Execute(w, p)


go get
go get
rice embed-go
go build


Testing Whenever with Rspec

Tagged cron, rspec, whenever  Languages ruby
require 'spec_helper'
require 'whenever'

describe Whenever do
  def gen_cron(stage:)
    x =
      file: Rails.root.join("config", "schedule.rb").to_s,
      set: "stage=#{stage}"
    x.gsub(Dir.pwd, "")

  it "generates correct cron configuration for each stage" do
    [ :qa, :production ].each do |stage|
      expected ="spec/fixtures/#{stage}.cron")
      cron = gen_cron(stage: stage)
      # NOTE: Uncomment to update expected cron configuration"spec/fixtures/#{stage}.cron", "w") {|x| x << cron }
      assert_equal expected.strip, cron.strip

How to get the latest version number from a list of git tags

Tagged bash, git, version, zsh  Languages bash

This will print the latest version number that a git repository has been tagged with:

$ git tag -l 'v*' | sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n | tail -n 1

Note that some versions of the sort command have a —version-sort switch, and some don’t:

sort: unrecognized option `--version-sort'
Try `sort --help' for more information.