Skip to content

Instantly share code, notes, and snippets.

View inopinatus's full-sized avatar
🐼
fuzzy logic

Josh Goodall inopinatus

🐼
fuzzy logic
View GitHub Profile
@inopinatus
inopinatus / relational_recommendations.rb
Last active October 9, 2019 01:34
Fun with relational composition in Rails
# frozen_string_literal: true
require "bundler/inline"
gemfile true, quiet: true do
source "https://rubygems.org"
gem "rails", "6.0.0"
gem "sqlite3"
gem "pry"
end
@inopinatus
inopinatus / all_year.rb
Created January 22, 2020 03:50
ActiveSupport's Date#all_* ranges inadvertently exclude times that should match.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails", github: "rails/rails"
gem "sqlite3"
end
require "active_record"
y = ->(f) {
->(x) { x.(x) }.(
->(x) { f.(->(v) { x.(x).(v) }) } )
}
fib = y.(->(f) {
->(n) { n < 2 ? Array(0..n) : f[n-1].then { _1 << _1[-1] + _1[-2] } }
})
fib[6] #=> [0, 1, 1, 2, 3, 5, 8]
@inopinatus
inopinatus / frequency_extension.rb
Last active May 10, 2020 06:45
arel "most frequent"
module FrequencyExtension
def most_frequent
frequency_order.take
end
def frequency_order
group(arel_attribute(primary_key).order(arel_attribute(primary_key).count.desc)
end
end
@inopinatus
inopinatus / digest_queries.rb
Created May 10, 2020 06:50
Digest predicates
module DigestQueries
def predicate_builder
@predicate_builder ||= super.tap do |pb|
pb.register_handler(Digest::Instance, DigestHandler.new(pb))
end
end
class DigestHandler
def initialize(predicate_builder)
@predicate_builder = predicate_builder
module ByMost
extend ActiveSupport::Concern
included do
scope :by_most, ->(assoc) {
reflection = reflect_on_association(assoc)
left_joins(assoc)
.group(primary_key)
.order(reflection.klass.arel_table[reflection.foreign_key].count.desc)
}
@inopinatus
inopinatus / inequalities.md
Last active June 17, 2025 16:51
ar inequalities
Inequality Interval Ruby range Active Record¹ Arel² Description
x >= a [a, ∞) a.. where(x: a..) x.gteq(a) Right unbounded closed
x > a (a, ∞) n/a where.not(x: ..a) x.gt(a) Right unbounded open
x <= a (-∞, a] ..a where(x: ..a) x.lteq(a) Left unbounded closed
x < a (-∞, a) ...a where(x: ...a) x.lt(a) Left unbounded open
a <= x <= b [a, b] a..b where(x: a..b) x.between(a..b) Closed
a < x < b (a, b) n/a where.not(x: ..a).where(x: ...b) x.gt(a).and(x.lt(b)) Open
a <= x < b [a, b) a...b where(x: a...
@inopinatus
inopinatus / adderall.rb
Last active August 20, 2020 12:56
Adderall
# app/lib/adderall.rb
module Adderall
def adderall(column_name, relation = self, filter = :itself)
owner = proxy_association.owner
records = proxy_association.target
if loaded? || owner.new_record?
relation = none
else
records = records.select(&:new_record?)
@inopinatus
inopinatus / exceeding.rb
Created August 24, 2020 15:07
AR generalised association join, group, count, and filter
# 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)
end
end
end
@inopinatus
inopinatus / work_allocation.rb
Created August 29, 2020 03:43
Work allocation
# 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.
OPS_MEMORY_ALLOWANCE = 512