Skip to content

Instantly share code, notes, and snippets.

View koffeinfrei's full-sized avatar

Alexis Reigel koffeinfrei

View GitHub Profile
@koffeinfrei
koffeinfrei / README.md
Last active December 23, 2023 03:43
Idempotent seed helper for rails -> THIS IS NOW A GEM https://github.com/panter/seed_box

Idempotent seed helper for rails

  • The first model argument is the model class
  • The second argument find_or_create_by is a hash of attributes that are used for finding a record. If the record doesn't exist, it will be created. This hash is like the unique identifier of a seed record.
  • The third argument update_with is a hash of attributes that will be always set on the record, whether the record already exists or has to be created. This is useful when the seeds are extended and you want to update existing records with new attributes.

Example usage

@koffeinfrei
koffeinfrei / .ruby-version
Created March 14, 2018 08:11 — forked from aishfenton/serializer_benchmarks.rb
Performance comparison of different ruby serializer methods
2.5.0
@koffeinfrei
koffeinfrei / .gitignore
Last active May 26, 2021 15:09
Ruby system call benchmark
.bundle/
@koffeinfrei
koffeinfrei / README.md
Last active December 11, 2018 09:13
how-to-contribute-a-metaflop-font
@koffeinfrei
koffeinfrei / generate_many_runner_tags.rb
Last active December 11, 2018 09:00
generate a lot of random ActsAsTaggableOn::Tag
require 'bulk_insert'
old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil
model = WhateverModel.find_by(whatever: 'something')
tag_names = ('a'..'i').to_a.permutation.to_a.shuffle.map(&:join)
tag_names_count = tag_names.count
max_id = ActsAsTaggableOn::Tag.maximum(:id)
@koffeinfrei
koffeinfrei / git-fame.sh
Last active September 22, 2017 09:11
git relevant contributors -> https://github.com/koffeinfrei/git-alpha
#!/bin/sh
# Help output
help() {
echo "Usage: git-fame [options] <repository_path>"
echo " -m Machine readable output"
echo " -h Print this help"
exit 1
}
@koffeinfrei
koffeinfrei / active_record_base_spec.rb
Last active March 6, 2016 12:01
has_many dependent spec
# File: spec/models/base_spec.rb
require 'rails_helper'
RSpec.describe 'All ActiveRecord::Base models' do
context 'has_many with :dependent key' do
each_active_record_model do |model|
has_many_relations(model).each do |has_many_relation|
it "has :dependent defined on the #{model}##{has_many_relation.name} relation" do
expect(has_many_relation.options[:dependent]).not_to be_nil
@koffeinfrei
koffeinfrei / combinations.rb
Last active December 11, 2015 11:48
3 char combinations (of my name)
# try pairs of 2
def pairs_of_2(names)
slices = names.join.scan(/.{2}/)
combinations(slices)
end
# try each chars
def pairs_of_chars(names)
chars = names.join.chars.to_a.uniq
combinations(combinations(chars))
@koffeinfrei
koffeinfrei / backtracking-sudoku-solver.rb
Created October 16, 2012 10:49
really stupid backtracking sudoku solver
=begin
ruby solver
=end
$count = 0
def valid?(state, x, y)
# check in col and row
0.upto(8) do |i|
return false if i != y and state[x][i] == state[x][y]
@koffeinfrei
koffeinfrei / process_locking_clipboard
Created November 21, 2011 08:28
Gets the process that locks the clipboard (i.e. openened but didn't close it)
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace ConsoleApplication1
{
public class Program
{
[DllImport("user32.dll")]
static extern IntPtr GetOpenClipboardWindow();