Skip to content

Instantly share code, notes, and snippets.

for f
do
# Change this value to increase maximum size
GIF_MAX_SIZE=480
dir="$(dirname "$f")"
name="$(basename "$f")"
cd "$dir"
@bf4
bf4 / 20160307172445_change_paperclip_attachment_path.rb
Created April 29, 2016 03:38 — forked from 121onto/20160307172445_change_paperclip_attachment_path.rb
Example migration for changing paperclip storage path
class MoveAttachmentsToNewLocation < ActiveRecord::Migration
def initialize(name = self.class.name, version = nil)
access_key = Rails.application.secrets.g3_access_key_id
secret_key = Rails.application.secrets.g3_secret_access_key
storage = Fog::Storage::Google.new google_storage_access_key_id: access_key,
google_storage_secret_access_key: secret_key
@bucket_name = Rails.application.secrets.g3_bucket
@bucket = storage.directories.get(@bucket_name)
super(name, version)
@bf4
bf4 / README.md
Created April 29, 2016 03:36 — forked from jlecour/README.md
Identify paperclip attachment files that are not attached to any record

Let's say you have a model, with an files attached, using Paperclip. You have a couple millions of those files and you're not sure that every one of them (and all its thumbnails) are still used by a database record.

You could use this rake task to recursively scan all the directories and check if the files need to be kept or destroyed.

In this example, the model is called Picture, the attachment is image and the path is partitioned like images/001/412/497/actual_file.jpg

The task is going down the path. Each time the path ends with 3 triplets of digits ("001/412/497" for example) it looks for a record with the ID 1412497. If such a record doesn't exist, the whole directory is moved to a parallel images_deleted directory. At the end you can delete the files if you like, or move them to an archive location.

You can use the "dry run" mode : to print which files would be removed

# config/initializers/active_model_serializers.rb
ActiveModelSerializers.config.key_transform = :unaltered
ActiveModelSerializers.config.schema_path = 'docs/schema/schemata'
ActiveSupport.on_load(:action_controller) do
require 'active_model_serializers/register_jsonapi_renderer'
end
# Routing constraint:
# Request 'Content-Type' must be 'application/vnd.api+json'
@bf4
bf4 / globals.rb
Created April 1, 2016 15:59 — forked from detunized/globals.rb
List global Ruby variables and their values
global_variables.sort.each do |name|
puts "#{name}: #{eval "#{name}.inspect"}"
end
@bf4
bf4 / logfmt.rb
Created March 31, 2016 20:06
logfmt
# Based on logfmt:
# https://www.brandur.org/logfmt
# For more complete implementation see:
# see https://github.com/arachnid-cb/logfmtr/blob/master/lib/logfmtr/base.rb
# For usage see:
# https://blog.codeship.com/logfmt-a-log-format-thats-easy-to-read-and-write/
# https://engineering.heroku.com/blogs/2014-09-05-hutils-explore-your-structured-data-logs/
# For Ruby parser see:
# https://github.com/cyberdelia/logfmt-ruby

There are three easy to make mistakes in go. I present them here in the way they are often found in the wild, not in the way that is easiest to understand.

All three of these mistakes have been made in Kubernetes code, getting past code review at least once each that I know of.

  1. Loop variables are scoped outside the loop.

What do these lines do? Make predictions and then scroll down.

func print(pi *int) { fmt.Println(*pi) }
@bf4
bf4 / convert-emarref-3374193.bash
Last active March 14, 2016 14:47 — forked from tasdikrahman/ico.rst
Everything you ever wanted to know about ico's
#!/bin/bash
# Copied from https://github.com/tlvince/scripts-shell/blob/808f36d2144f5b9ca4399e6cef4add96279c75d7/favicon.bash
# Favicon and Apple Touch Icon Generator.
# Copyright 2012 @emarref
# Copyright 2012 Tom Vincent <http://tlvince.com/contact>
# From: https://gist.github.com/3374193
#
# This bash script takes an image as a parameter, and uses ImageMagick to
# convert it to several other formats used on modern websites. The following
# copies are generated:
From 64ed05c484dc0add53183579a347b13d138ee944 Mon Sep 17 00:00:00 2001
From: Santiago Pastorino <santiago@wyeworks.com>
Date: Tue, 7 May 2013 17:51:56 -0700
Subject: [PATCH 01/66] Define serializer as DefaultSerializer if not set
---
lib/active_model/array_serializer.rb | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/lib/active_model/array_serializer.rb b/lib/active_model/array_serializer.rb
@bf4
bf4 / bench.rb
Created March 7, 2016 16:34 — forked from martinemde/bench.rb
Compare s.hex.chr vs [s.hex].pack('C') vs [s.hex].pack(C)
require 'benchmark/ips'
require 'securerandom'
Benchmark.ips do |x|
C = 'C'.freeze
DATA = (1..1_000_000).map { SecureRandom.hex(1) }
x.report("hex.chr") do |times|
i = 0
while i < times