Skip to content

Instantly share code, notes, and snippets.

@rzane
rzane / polymorphic_has_one_primary_key.rb
Created June 23, 2023 15:23
ActiveRecord polymorphic associations don't accept a custom primary key?
require 'bundler/inline'
require 'minitest/autorun'
gemfile true do
source 'https://rubygems.org'
gem 'activerecord', require: 'active_record'
gem 'sqlite3'
end
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
@rzane
rzane / deprecations.rb
Created October 31, 2022 20:46
Deprecate Rails path helpers
deprecator = ActiveSupport::Deprecation.new('in the future', 'B4B')
deprecator.behavior = ActiveSupport::Deprecation.behavior
ActiveSupport::Deprecation.deprecate_methods(
Rails.application.routes.named_routes.path_helpers_module,
:foo_path,
:bar_path,
deprecator: deprecator
)
@rzane
rzane / controller_spec.rb
Created June 16, 2022 20:31
Writing a controller spec outside of a Rails application
require 'spec_helper'
require 'rails'
require 'action_view'
require 'action_controller'
require 'rspec/rails'
class TestApplication < Rails::Application
end
RSpec.describe 'example', type: :controller do
@rzane
rzane / print-fields.rb
Last active December 14, 2021 17:02
Print fields used by GraphQL query
require "set"
require "graphql"
require "json"
SCHEMA_PATH = ENV.fetch("SCHEMA") do
abort "Run with SCHEMA=<path-to-schema.json> and try again."
end
SCHEMA = JSON.parse(File.read(SCHEMA_PATH))
REFERENCES = Hash.new do |h, t|
@rzane
rzane / convert.rb
Last active July 13, 2021 18:06
Convert to TypedDocumentNode
# Convert your codebase to use TypedDocumentNode. It's not perfect, but
# it does a pretty decent job. You'll want to review the changes manually.
# Usage: ruby convert.rb [...GLOBS]
# Example: ruby convert.rb "src/**/*.{ts,tsx}"
require "set"
def convert_import(line)
parts = line.scan(/use(\w+)(Query|Mutation)/)
return [line] if parts.empty?
@rzane
rzane / gem.md
Last active May 4, 2022 20:49
Actions

Gem

Build

name: Build
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
@rzane
rzane / README.md
Last active July 18, 2020 13:14
QueryExtensions

QueryExtensions

First, you'll need to incorporate the extension:

class ApplicationRecord < ActiveRecord::Base
  extend QueryExtensions
end
@rzane
rzane / fileExchange.ts
Created August 1, 2019 23:42
URQL file exchange (which doesn't work at all)
import { print } from "graphql";
import { pipe, map } from "wonka";
import { Exchange, Operation } from "urql";
type FetchExchangeFn = (
operation: Operation,
options: RequestInit
) => RequestInit;
const createFetchExchange = (fn: FetchExchangeFn): Exchange => {
@rzane
rzane / github-to-pivotal.rb
Created May 22, 2019 15:57
Migrate GitHub issues to Pivotal Tracker
require 'github_api'
require 'tracker_api'
GITHUB_USER = 'username or organization'
GITHUB_REPO = 'repo'
GITHUB_LOGIN = 'username'
GITHUB_PASSWORD = '***'
PIVOTAL_TOKEN = '***'
PIVOTAL_PROJECT_ID = 'last segment of the pivotal URL'
@rzane
rzane / Dockerfile
Created April 1, 2019 02:27
Tiny debug image
FROM golang:alpine as build
WORKDIR /usr/src/app
RUN apk --no-cache add upx
COPY ./server.go ./
ENV CGO_ENABLED=0
RUN go build -o server server.go
RUN upx --brute server
# ---