Skip to content

Instantly share code, notes, and snippets.

View brendon's full-sized avatar

Brendon Muir brendon

View GitHub Profile
@brendon
brendon / add_position_to_things.rb
Last active June 25, 2023 21:42
A simple Rails Concern for maintaining an arbitrary rank in a model based on a parent scope
# An example migration to add your position column to an existing table
class AddPositionToThings < ActiveRecord::Migration[6.1]
def change
add_column :things, :position, :integer, null: false
ParentThing.all.each do |parent_thing|
parent_thing.things.order(:updated_at).each.with_index(1) do |thing, index|
thing.update_column :position, index
end
@brendon
brendon / configuration.js
Created November 19, 2021 20:57
Google Fonts for Froala 3.x and probably 4.x
// You need to add these two configuration options when initialising froala:
googleFonts: true,
googleFontsAddress: `https://${ASSETS_ADDRESS}/webfonts/detailed`
// The address can be anything but the expected structure is fixed and you should be able to infer that from
// the font_family.js. I've included the controller I use to generate the cached copy of the webfonts list.
// Don't be tempted to hit google all the time with your key as you'll soon reach your fair use limit.
// When rendering the page, add data-controller="froala--webfonts" to the surrounding tag (if using my stimulus controller)
@brendon
brendon / is_geoff_right.rb
Last active August 26, 2021 22:21
Is Geoff Right? He is
# To run this just install Ruby (it's already on Macs), download this file
# into a folder, open your terminal and `cd` into that folder type:
# `ruby is_geoff_right.rb`
cars = 0
goats = 0
1000000.times do
doors = [:car, :goat, :goat]
# frozen_string_literal: true
require "bundler/inline"
# This reproduction script allows you to test Action Policy with Rails.
# It contains:
# - Headless User model
# - UserPolicy
# - UsersController
# - Example tests for the controller.
/*
████████╗██╗ ██╗███████╗███╗ ███╗███████╗███████╗
╚══██╔══╝██║ ██║██╔════╝████╗ ████║██╔════╝██╔════╝
██║ ███████║█████╗ ██╔████╔██║█████╗ ███████╗
██║ ██╔══██║██╔══╝ ██║╚██╔╝██║██╔══╝ ╚════██║
██║ ██║ ██║███████╗██║ ╚═╝ ██║███████╗███████║
╚═╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚══════╝╚══════╝
*/
@brendon
brendon / gist:56943e8bdcd8ca9c215742304a83dcc4
Created July 15, 2018 03:44
RUBYOPT=-d bundle exec appraisal rails-5-2 rake
➜ ranked-model git:(master) RUBYOPT=-d bundle exec appraisal rails-5-2 rake
Exception: `NoMethodError' core/type.rb:154 - undefined method `to_str' on for_lockfile:Symbol.
Exception: `TypeError' core/type.rb:156 - Coercion error: :for_lockfile.to_str => String failed
Exception: `NoMethodError' core/type.rb:195 - undefined method `to_ary' on an instance of String.
Exception: `NoMethodError' core/type.rb:195 - undefined method `to_ary' on an instance of String.
Exception: `NoMethodError' core/type.rb:154 - undefined method `to_str' on to_spec:Symbol.
Exception: `TypeError' core/type.rb:156 - Coercion error: :to_spec.to_str => String failed
Exception: `LoadError' core/code_loader.rb:303 - no such file to load -- rubygems/defaults/operating_system
Exception: `NoMethodError' core/type.rb:195 - undefined method `to_ary' on an instance of String.
Exception: `NoMethodError' core/type.rb:195 - undefined method `to_ary' on an instance of String.
@brendon
brendon / google_fonts_froala.js
Last active March 19, 2018 21:04
Google Fonts in Froala Editor
// Here's a super bare-bones and unstructured implementation of google fonts for the Froala Editor.
//
// The required dependency is Semantic-UI/dist/components/visibility.js in order to lazy-load google font previews
$(document).ready(function() {
var $editor_selector = $('.froala-editor');
if ($editor_selector.length) {
var options = {
fontFamilySelection: true,
@brendon
brendon / gist:8332688
Last active January 2, 2016 16:49
delayed_job upstart configuration (No Hacks) - Ubuntu 12.04 LTE
description "Delayed Job Server"
author "Brendon Muir"
setuid www-data
setgid www-data
start on started mysql
stop on stopping mysql
respawn
@brendon
brendon / jquery.ui.touch.js
Created August 26, 2012 23:18
jsTree Touch Compatibility
/**
* jQuery.UI.iPad plugin
* Copyright (c) 2010 Stephen von Takach
* licensed under MIT.
* Date: 27/8/2010
*
* Project Home:
* http://code.google.com/p/jquery-ui-for-ipad-and-iphone/
*
* Modified: 19/01/2012
@brendon
brendon / gist:1365258
Created November 14, 2011 21:35
Gemfile
source 'http://rubygems.org'
gem 'rails', '3.1.1'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'pg'