Skip to content

Instantly share code, notes, and snippets.

View RStankov's full-sized avatar
🤖
👨‍💻

Radoslav Stankov RStankov

🤖
👨‍💻
View GitHub Profile
@RStankov
RStankov / application_component.rb
Created April 25, 2024 11:56
ViewComponent Tips
class ApplicationComponent < ViewComponent::Base
private
def fetch_with_fallback(hash, key, fallback)
hash.fetch(key) do
ErrorReporting.capture_exception(%(key not found: "#{key}"))
fallback
end
end
@RStankov
RStankov / counter_cache.sql
Created April 4, 2024 13:05
Counter Cache in SQL
CREATE OR REPLACE FUNCTION comments_count_update()
RETURNS TRIGGER AS $$
BEGIN
-- Increase count on insert
IF (TG_OP = 'INSERT') THEN
UPDATE posts SET comments_count = comments_count + 1
WHERE posts.id = NEW.post_id;
RETURN NEW;
-- Decrease count on delete
ELSIF (TG_OP = 'DELETE') THEN
@RStankov
RStankov / emergency_kit.md
Created March 7, 2024 19:38
⛑️ Emergency Kit

⛑️ Emergency Kit

🎥 Video walkthrough of emergency handling `[VIDEO IN TOOL LIKE LOOM]`

Tools

@RStankov
RStankov / template_postmortem.md
Created March 7, 2024 17:26
Post postmortem template

[REPLACE TITLE] Postmortem

Summary:

[TLD of what happened]

Responders:

[who handled the issue]

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>Menu</title>
<style>
.wrapper {
margin-left: auto;
margin-right: auto;
@RStankov
RStankov / container.tsx
Created February 21, 2024 21:22
Modal System
// The most complex part of the modal system
// There are lot of small details heres
// I extracted this from a real project
// Notice: Hooks hide a lot of complexity
import { IModalOpen } from './types';
import { closeModal } from './triggers';
import { useEffect, useMemo, useRef, useState } from 'react';
import { useEventBus } from '../eventBus';
import { InsideModalProvider } from './context';
import * as React from 'react';
import mitt from 'mitt';
interface IEventsMap {
modalOpen: {
content: React.ReactNode;
url?: string;
};
modalClose: null;
class Controller < ActionController::Base
include DevelopmentLogging if Rails.env.development?
end
# frozen_string_literal: true
# Complex counter counter caches
#
# Usage:
#
# In parent model
# class Post < ApplicationRecord
# ExplicitCounterCache.define(self, votes_count, -> { votes.active })
# ExplicitCounterCache.define(self, comments_count, -> { votes.visible })
# frozen_string_literal: true
# Note(rstankov):
#
# **Don't use for `connection`s, only for `field` attributes**
#
# Preload associations.
#
# Supports all preload formats from [ActiveRecord::Base.includes](https://api.rubyonrails.org/v5.2.4/classes/ActiveRecord/QueryMethods.html#method-i-includes).
#