Skip to content

Instantly share code, notes, and snippets.

View palkan's full-sized avatar

Vladimir Dementyev palkan

View GitHub Profile
@palkan
palkan / README.md
Created January 24, 2022 13:29
Rails boot time profiling

Add the following to application.rb:

$icallbacks = []
$icallbacks.define_singleton_method(:print) do
  puts sort_by { |(a, b)| -b }.map { |(a, b)| "#{b}\t\t#{a}" }.join("\n")
end

ActiveSupport::Notifications.subscribe("load_config_initializer.railties") do |event|
 $icallbacks << [event.payload[:initializer], event.duration]
class Array
def deconstruct
self
end
end
class Hash
def deconstruct_keys(_)
self
end
@palkan
palkan / normalize.rb
Created December 24, 2021 14:47
ActiveModel .normalize
# frozen_string_literal: true
require "bundler/inline"
gemfile(true, quiet: true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "activesupport", "~> 7.0.0", require: false
@palkan
palkan / frame_display.rb
Last active November 2, 2021 15:32
tcpdump -> pretty http2 packets
# frozen_string_literal: true
# Example usage:
#
# tcpdump -i lo0 'port 3010' -v -X -l | ruby frame_display.rb
#
require 'bundler/inline'
gemfile(true, quiet: true) do
source 'https://rubygems.org'
@palkan
palkan / bulletify.rb
Created September 7, 2020 17:00
Bulletify: RSpec helpers to run Bullet in tests
RSpec.shared_context "bullet" do
before(:each) do
Bullet.enable = true
Bullet.bullet_logger = true
Bullet.raise = true # raise an error if N+1 query occurs
Bullet.start_request
end
after(:each) do
Bullet.perform_out_of_channel_notifications if Bullet.notification?
@palkan
palkan / compile.c
Created June 28, 2020 08:01
compile.c ADD_STACKPRINT
#define ADD_STACKPRINT(label, depth) do {\
ADD_INSN1(ret, line, putobject, rb_fstring_lit(label));\
ADD_SEND(ret, line, rb_intern("putme"), INT2FIX(0));\
ADD_INSN(ret, line, pop);\
for (int x = 0; x < depth ; x++ ){\
ADD_INSN1(ret, line, topn, INT2FIX(x));\
ADD_SEND(ret, line, rb_intern("putme"), INT2FIX(0));\
ADD_INSN(ret, line, pop);\
}\
} while(0)
@palkan
palkan / connection.rb
Last active February 26, 2021 18:26
Action/AnyCable + Apartment
module ApplicationCable
class Connection < ActionCable::Base::Connection
# we need to keep tenant information for subsequent messages,
# so let's store it as an identifier
identified_by :tenant
def connect
# assuming you store current tenant in session
self.tenant = request.session[:current_tenant]
reject_unauthorized_connection unless tenant
@palkan
palkan / Dockefile
Last active May 14, 2020 06:09
hybrid dockerfile
ARG RUBY_VERSION
# === Base image ===
FROM ruby:${RUBY_VERSION}-slim-buster as base
ARG NODE_MAJOR
ARG POSTGRES_VERSION
ARG BUNDLER_VERSION
ARG YARN_VERSION
# Common dependencies
@palkan
palkan / .rubocop_strict.yml
Created March 18, 2020 20:29
rubocop-strict config
# Inherit from TODO here to make sure we enforce the rules below
# (and TODO is ignored)
inherit_from:
- .rubocop_todo.yml
Lint/Debugger: # don't leave binding.pry
Enabled: true
Exclude: []
RSpec/Focus: # run ALL tests on CI
@palkan
palkan / .rubocop_rails.yml
Created March 18, 2020 20:25
rubocop-rails config
# Based on removed standard configuration:
# https://github.com/testdouble/standard/commit/94d133f477a5694084ac974d5ee01e8a66ce777e#diff-65478e10d5b2ef41c7293a110c0e6b7c
require:
- rubocop-rails
Rails/ActionFilter:
Enabled: true
EnforcedStyle: action
Include: