Skip to content

Instantly share code, notes, and snippets.

View palkan's full-sized avatar

Vladimir Dementyev palkan

View GitHub Profile
@palkan
palkan / 01_Readme.md
Last active April 30, 2023 00:03
Backport Rails 6 per-environment credentials

Backport Rails 6 per-environment credentials to Rails 5.2

Rails PR: rails/rails#33521

This patch makes it possible to use per-environment credentials (i.e., config/credentials/staging.yml.enc) in Rails 5.2.

Installation

  • Drop backport_rails_six_credentials.rb and backport_rails_six_credentials_command.rb somewhere, for example, into the lib/ folder
  • Add this line to config/application.rb:
@palkan
palkan / many_vs_flat_map_bench.rb
Last active December 4, 2019 09:04
many vs flat_map bench
# See:
# - https://codon.com/refactoring-ruby-with-monads#multiple-results
# - Rails PR: https://github.com/rails/rails/issues/37875
require "benchmark_driver"
Benchmark.driver do |x|
x.prelude %Q{
Blog = Struct.new(:categories)
Category = Struct.new(:posts)
@palkan
palkan / 00_Readme.md
Last active July 16, 2022 06:50
graphql-ruby fragment caching

PoC: GraphQL Ruby fragment caching

This example demonstrates how we can cache the response fragments in graphql-ruby.

Existing solutions only allow caching resolved values but not all the GraphQL machinery (validation, coercion, whatever).

Caching response parts (in case of Ruby, sub-Hashes) is much more efficient.

Benchmarks

@palkan
palkan / pp.rb
Created February 13, 2020 13:21
[GraphQL] @pp profiling
# frozen_string_literal: true
module Directives
class Pp < GraphQL::Schema::Directive
class Profiler < Types::BaseEnum
graphql_name "PpProfiler"
description "Available profilers for @pp directive"
value :mem
value :stack
@palkan
palkan / tracer.rb
Created February 13, 2020 13:35
[GraphQL] verify batch load
# frozen_string_literal: true
module Graphql
# This module implements the functionality to track whether the batch loading is
# required for a field to solve N+1 problem.
#
# If you want to check whether a particular field doesn't use batch loading in vain,
# mark it with `verify_batch_load: true` option.
#
# By default, it logs the following information when detects a possible N+1 (thus, confirms that batch loading is required).
@palkan
palkan / .rubocop_rspec.yml
Last active March 22, 2022 12:44
rubocop-rspec config
require:
- rubocop-rspec
# Disable all cops by default,
# only enable those defined explcitly in this configuration file
RSpec:
Enabled: false
RSpec/Focus:
Enabled: true
@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:
@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 / 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 / connection.rb
Last active April 8, 2025 19:39
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