Skip to content

Instantly share code, notes, and snippets.

View pixeltrix's full-sized avatar

Andrew White pixeltrix

View GitHub Profile
@pixeltrix
pixeltrix / migration_status.rb
Created August 30, 2019 12:39
Glue code required to get an Active Record rake task working outside of Rails
require 'rake'
require 'active_record'
require 'erb'
require 'yaml'
ActiveRecord::Tasks::DatabaseTasks.migrations_paths = [File.expand_path('db/migrate')]
module Rails
def self.env
'development'
@pixeltrix
pixeltrix / toy_robot.rb
Created May 15, 2019 12:47
Ruby implementation of the common code exercise
require "matrix"
require "readline"
class ToyRobot
PROMPT = "> "
PLACE = /\APLACE\s+(\d+),(\d+),(NORTH|SOUTH|EAST|WEST)\z/
COMMAND = /\ALEFT|RIGHT|MOVE|REPORT\z/
QUIT = /\AQUIT\z/
LEFT = Matrix[[0, 1], [-1, 0]]
RIGHT = Matrix[[0, -1], [ 1, 0]]
<!DOCTYPE html>
<html>
<head>
<title>FSM</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.css" type="text/css">
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, Sans-Serif;
margin: 0;
@pixeltrix
pixeltrix / rails-32083.rb
Created March 9, 2018 13:46
Example for issue #32083
# frozen_string_literal: true
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
@pixeltrix
pixeltrix / turbolinks_helpers.rb
Last active October 31, 2017 15:20
Assert redirect helper for Turbolinks (untested code)
@pixeltrix
pixeltrix / 20171004211734_add_timestamps_everywhere.rb
Created October 4, 2017 21:27
Migration to add timestamps easily to a bunch of tables
class AddTimestampsEverywhere < ActiveRecord::Migration[5.1]
def change
now = Time.current
%i[
candidates
evaluations
interviewers
questions
responses
@pixeltrix
pixeltrix / date_and_time_compatibility_test.rb
Created February 25, 2017 13:44
Enhanced Date/Time compatibility tests
require "abstract_unit"
require "active_support/time"
require "time_zone_test_helpers"
class DateAndTimeCompatibilityTest < ActiveSupport::TestCase
include TimeZoneTestHelpers
def setup
@utc_time = Time.utc(2016, 4, 23, 14, 11, 12)
@date_time = DateTime.new(2016, 4, 23, 14, 11, 12, 0)
@pixeltrix
pixeltrix / behaviour.rb
Last active February 24, 2017 17:03
Script to output behaviours of Time, Date and DateTime across Ruby versions
require 'date'
require 'time'
ENV['TZ'] = 'America/New_York'
time = Time.new(2017, 1, 1, 0, 0, 0, "+02:00").freeze
date = Date.new(2017, 1, 1).freeze
datetime = DateTime.new(2017, 1, 1, 0, 0, 0, Rational(2, 24)).freeze
# Time
puts "Time#to_time | #{RUBY_VERSION} "
@pixeltrix
pixeltrix / download-petitions.rb
Created February 22, 2017 15:34
Ruby script to download archived petition data
require 'csv'
require 'faraday'
require 'json'
require 'uri'
url = URI.parse('https://petition.parliament.uk/archived/petitions.json')
response = Faraday.get(url)
unless response.success?
@pixeltrix
pixeltrix / postgis21.rb
Last active September 4, 2018 22:09
Formula for install postgis-2.1 with the legacy postgresql@9.4 homebrew formula
class Postgis21 < Formula
desc "Adds support for geographic objects to PostgreSQL"
homepage "http://postgis.net"
url "http://pkgs.fedoraproject.org/repo/pkgs/postgis/postgis-2.1.8.tar.gz/c33923e37424978a1306ce461c1d14ed/postgis-2.1.8.tar.gz"
sha256 "7c2380b895fe7bda34c2e70deab3fcf4c12b13ab40d7501cdaa6fa36f1a6c662"
revision 1
def pour_bottle?
# Postgres extensions must live in the Postgres prefix, which precludes
# bottling: https://github.com/Homebrew/homebrew/issues/10247