Skip to content
View 20160307172445_change_paperclip_attachment_path.rb
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)
View README.md

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

View ams_jsonapi.rb
# 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'
View globals.rb
global_variables.sort.each do |name|
puts "#{name}: #{eval "#{name}.inspect"}"
end
View logfmt.rb
# 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
View The Three Go Landmines.markdown

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) }
View convert-emarref-3374193.bash
#!/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:
View unrelased_ams_08.patch
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
View bench.rb
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
View binary.rb
# http://www.mikeperham.com/2014/11/26/building-a-binary-tree-with-enumerable/
class Node
include Enumerable
include Comparable
attr_accessor :data, :left, :right
def initialize(data)
@data = data
end
Something went wrong with that request. Please try again.