Skip to content

Instantly share code, notes, and snippets.

Kris Leech krisleech

Block or report user

Report or block krisleech

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View rescue_arguments.rb
require "bundler/inline"
gemfile(true) do
source ""
gem "rspec"
BoomError =
RSpec.describe 'rescue arguments' do
krisleech / worker.rb
Created May 14, 2019
Worker (sidekiq) module to report exceptions only when last retry has failed
View worker.rb
# app/models/my_worker.rb
class MyWorker
include Carbon::Worker
BoomError =
retry_without_notification BoomError
def call(attributes)
raise BoomError
krisleech / notes.rb
Created Apr 25, 2019
ActiveRecord locking
View notes.rb
# Prevent other processes/threads from updating rows, uses "FOR UPDATE" lock by default:
Recruitment::CpmsTotal.transaction do
totals = Recruitment::CpmsTotal.not_sent.lock.each # [1]
# [1] each forces query to happen, #all on its own does not.
krisleech / all.rb
Created Apr 3, 2019
inline ActiveRecord and Bundler
View all.rb
require "bundler/inline"
gemfile(true) do
source ""
gem "activerecord"
gem "sqlite3"
require "active_record"
require "logger"
krisleech /
Last active Mar 29, 2019
Blood Glucose on low carb


Time BG (mmol/l)
13:43 4.4
15:00 4.8
16:44 4.2
19:30 6.9
22:25 5.1
krisleech / example.rb
Last active Jan 16, 2019
Where to put `if`, shallow or deeper?
View example.rb
# Lets say we have a Book model and we want to fetch some data about the book from an API and store it in the model.
# A Book needs an identifier before it can be fetched from the API. Not all books have an identifier, e.g. it may not have been entered yet.
# Do we silently return in FetchFromApi when the book has no identifer:
class FetchFromApi
def call(book)
return unless record.identifier.present?
# do API call and update record
krisleech / app.rb
Created Jan 14, 2019
Inline Ruby project with ActiveRecord
View app.rb
require 'bundler/inline'
gemfile do
source ''
gem 'active_record'
gem 'sqlite3'
View example.rb
if study.require_participant_type?
packed_permutations << Recruitment::ParticipantType.pluck(:identifier).append(nil)
packed_permutations << [].append(nil)
# or packed_permutations << [nil] (same, but less symmetry)
# or
packed_permutations << (study.require_participant_type? ? Recruitment::ParticipantType.pluck(:identifier) : []).append(nil)
krisleech /
Created Oct 29, 2018
Callbacks for Service
# in engine

class CreateStudy
  extend ActiveModel::Callbacks
  define_model_callbacks :create

  def call(form)
    if form.valid?
      @study =
krisleech /
Last active May 5, 2019
ActiveModel comforming Form object based on dry-type / dry-validation
require 'dry-validation'
require 'dry-struct'
require 'active_model/errors'

require_relative '../create_study'

module MyApp
  module Types
    include Dry::Types.module
You can’t perform that action at this time.