Skip to content

Instantly share code, notes, and snippets.

View joeljunstrom's full-sized avatar
🥰

Joel Junström joeljunstrom

🥰
  • Stockholm, Sweden
View GitHub Profile
@joeljunstrom
joeljunstrom / selectable_attributes.rb
Created July 8, 2026 10:38
SelectableAttributes DSL. Delegation methods that prefers values that are fetched from a select. Automatic scope and delegation method generation
# frozen_string_literal: true
# = Selectable Attributes
#
# Declares attributes that normally come from an associated record, but which
# can instead be loaded in a single +SELECT+ (via a join) alongside the host
# record. This keeps bulk reads (CSV exports, large index pages) free of N+1
# association loads, while individual records still resolve the value by walking
# the association when the joined columns were not selected.
#
@joeljunstrom
joeljunstrom / local_dev_reloads_under_falcon.rb
Last active May 26, 2026 06:55
Glue code that makes hotwire-spark and rails reloader work under Falcon + async-cable
# frozen_string_literal: true
# Glue code that makes hotwire-spark work under Falcon + async-cable.
#
# hotwire-spark assumes Puma + `rails server`:
# - Hotwire::Spark.enabled? gates install on `defined?(Rails::Server)`,
# which is only set by `rails s`. Falcon boots through its own CLI.
# - The cable server is mounted through the router and upgrades via rack
# hijack + nio4r, which does not cooperate with Falcon's fiber scheduler.
#

Keybase proof

I hereby claim:

  • I am joeljunstrom on github.
  • I am joeljunstrom (https://keybase.io/joeljunstrom) on keybase.
  • I have a public key ASDVC8PAVOwxFxaT3VTiiMu09R0R8dKFfjrE-0uhhDlb9Qo

To claim this, I am signing this object:

@joeljunstrom
joeljunstrom / purchase.rb
Created July 7, 2020 13:56
Refactor of purchase script from slack
module Shop
class Stock
attr_reader :items
def self.with(*item_attributes)
new(
*item_attributes.map { |attrs| StockItem.new(**attrs) }
)
end
class User < ApplicationRecord
attribute :otp_secret, :encrypted
end
class EncryptedType < ActiveRecord::Type::Value
class << self
memoize def secret_key
Rails.application.key_generator.generate_key("encrypted_type")
end
end
# config/routes.rb
Rails.application.routes.draw do
resource :contact_message, only: [:new, :create], path: "/contact", path_names: {new: "/"}
end
# app/controllers/contact_messages_controller.rb
class ContactMessagesController < ApplicationController
def new
render locals: {form: ContactMessageForm.new}
end
# app/models/user.rb
class User < ApplicationRecord
attribute :otp_secret, :encrypted
attribute :otp_recovery_secret, :encrypted
end
# lib/types/encrypted_type.rb
class Payment < ApplicationRecord
end
class PaymentEventMessagesController < ApplicationController
# The controller does as little as possible. Not ethat this should probably verify that the payload is legit somehow
# We offload the heavy lifting to a job to we answer the gateway asap and not bog down our servers
def create
message = PaymentEventMessage.create!(gateway: gateway, payload: params[:payload])
ProcessPaymentEventMessageJob.perform_later(message.id)
Elasticsearch::Transport::Transport::Errors::BadRequest: [400] {"error":"SearchPhaseExecutionException[Failed to execute phase [query_fetch], all shards failed; shardFailures {[unicef_production_registrations][0]: RemoteTransportException[[Sunset Bain][inet[/172.31.13.218:9300]][indices:data/read/search[phase/query+fetch]]]; nested: SearchParseException[[the-collection][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"function_score\":{\"functions\":[{\"script_score\":{\"script\":\"(0.08 / ((3.16*pow(10,-11)) * abs(now-doc['created'].date.getMillis()) + 0.05)) + 1.0\",\"params\":{\"now\":1429858855000}}}],\"query\":{\"filtered\":{\"query\":{\"multi_match\":{\"query\":\"some name\",\"type\":\"cross_fields\",\"fields\":[\"_all\"],\"operator\":\"or\"}},\"filter\":{\"exists\":{\"field\":\"created\"}}}}}},\"size\":20,\"from\":0}]]]; nested: QueryParsingException[[the-collection] script_score the script could not be loaded]; nested: IllegalArgumentException[Unrecognized method call (d
<!--[if lt IE 9]>
<link href="stylesheets/ie.css" media="screen" rel="stylesheet" type="text/css" />
<![endif]-->