Skip to content

Instantly share code, notes, and snippets.

fuzzy logic

Josh Goodall inopinatus

fuzzy logic
View GitHub Profile
inopinatus / config_objects.rb
Last active Oct 5, 2020
excessively dynamic accessors
View config_objects.rb
class MyConfiguration < BasicObject
def method_missing name, *args
getter = name.end_with?(?=) ? name[0..-2] : name
(class << self; self end).class_eval { attr_accessor getter }
__send__ name, *args
config =
config.liverpool = 4
inopinatus / latest_association_by_distinct_on.rb
Last active Sep 19, 2020
preloading the latest of an associated collection
View latest_association_by_distinct_on.rb
class Post
has_many :comments
has_one :latest_comment, -> { latest_by :post_id }, class_name: 'Comment'
class Comment
scope :latest_by, -> column {
order(column, created_at: :desc).arel
inopinatus / matches_any.rb
Created Sep 16, 2020
programmatic LIKE in Arel
View matches_any.rb
scope :search_uid, ->(*terms) {
where(arel_attribute(:uid).matches_any( {|term| "%#{sanitize_sql_like term}%" }, nil, true))
inopinatus / callback_registration_tracing.rb
Created Sep 14, 2020
initializer for determining where callbacks were registered
View callback_registration_tracing.rb
# frozen_string_literal: true
module CallbackRegistrationTracing
module Capture
def normalize_callback_params(*args)
super.tap do |_, _, options|
options[:_caller] = caller(3)
inopinatus / arel_reverse_order_issues.rb
Last active Sep 4, 2020
Arel and reverse_order interaction
View arel_reverse_order_issues.rb
# frozen_string_literal: true
# In this example we have an application concerned about the length of a string,
# and is pushing the work to the database for efficiency. There is an index
# on the length of a Post's title, and the scopes and orderings have been
# neatly refactored to minimise the amount of Arel required.
# A bug has now arisen in the behaviour of `last`. It is giving the first.
# Subsequent investigation has shown that customers were also complaining that
View work_allocation.rb
# frozen_string_literal: true
class WorkAllocation
# Runtime process allocations. Last reviewed: 24/9/2019
# Half a gig system allocation for agents and monitors and, unfortunately,
# a couple of deploy-time asset compiles due to dependencies. It's
# okay to push some processes into swap on smaller instances during
# deploy. Ideally we wouldn't compile on-instance at all.
inopinatus / exceeding.rb
Created Aug 24, 2020
AR generalised association join, group, count, and filter
View exceeding.rb
# app/models/concerns/exceeding.rb
module Exceeding
extend ActiveSupport::Concern
included do
scope :exceeds, ->(association, n) do
joins(association).group(primary_key).having(arel_table[primary_key].count.gteq n)
View adderall.rb
# app/lib/adderall.rb
module Adderall
def adderall(column_name, relation = self, filter = :itself)
owner = proxy_association.owner
records =
if loaded? || owner.new_record?
relation = none
records =
inopinatus /
Last active May 10, 2020
ar inequalities
Inequality Interval Ruby range Active Record* Arel**
n >= x [x..∞) x.. where(n: x..) n.gteq(x)
n <= x (-∞..x] ..x where(n: ..x) n.lteq(x)
n < x (-∞..x) ...x where(n: ...x)
n > x (x..∞) n/a where.not(n: ..x)
x <= n <= y [x..y] x..y where(n: x..y) n.between(x..y)
x <= n < y [x..y) x...y where(n: x...y) n.between(x...y)
x < n <= y (x..y] n/a where.not(n: ..x).where(n: ..y)
x < n < y (x..y) n/a where.not(n: ..x).where(n: ...y)
View by_most.rb
module ByMost
extend ActiveSupport::Concern
included do
scope :by_most, ->(assoc) {
reflection = reflect_on_association(assoc)