Skip to content

Instantly share code, notes, and snippets.

View michaelgpearce's full-sized avatar

Michael Pearce michaelgpearce

  • Enjoy, Inc
  • San Francisco
View GitHub Profile
@michaelgpearce
michaelgpearce / active_admin_share_views_helper.rb
Last active February 21, 2023 03:58
Active Admin share view helper
# Example:
#
# ActiveAdmin.register Customer do
# add_helper(:header) do |name|
# h1(name)
# end
#
# index do
# helper(:header, 'INDEX')
#
@michaelgpearce
michaelgpearce / preloader.rb
Last active November 21, 2020 01:35
ActiveRecord association preloading that works around some of the ActiveRecord::Associations::Preloader limitations
# Preloads ActiveRecord model associations with shared object references.
# This internally uses ActiveRecord::Associations::Preloader but works around some of its limitations.
# It works around a bug with associations containing loaded/unloaded models. https://github.com/rails/rails/issues/32140
#
# Usage:
# Preloader.new(models, association_spec).preload
# Also supports a third `scope` argument that is used when querying.
#
# There is an additional class utility method `uniquify_associations` to share object references that can be useful in preloading.
class Preloader
@michaelgpearce
michaelgpearce / caller.rb
Last active March 13, 2019 08:13
Parallel Workforce Caller
require 'bcrypt'
passwords = ['password 1', 'password 2', 'password 3']
password_hashes = ParallelWorkforce.perform_all(
actor_classes: Array.new(passwords.size) { PasswordHashGenerator },
actor_args_array: passwords.map { |password| { password: password } },
)
# check that password hashes match
@michaelgpearce
michaelgpearce / password_hash_generator.rb
Last active March 14, 2019 00:41
Parallel Workforce Actor
require 'bcrypt'
class PasswordHashGenerator
attr_reader :password
def initialize(password:)
@password = password
end
def perform