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
Last active October 3, 2024 21:30
ActionCable over SSE

Action Cable over SSE

This is a demo of leveraging the proposed Action Cable architecture to implement an SSE transport for Action Cable (without changing the user-space code, e.g., Connection and Channel classes).

Start the server by running the following command:

 ruby main.rb

Now, you can connect to Action Cable over SSE via cURL as follows:

@palkan
palkan / application_view_component.rb
Last active March 2, 2024 18:06
[ViewComponent] html_option
# From https://evilmartians.com/chronicles/viewcomponent-in-the-wild-embracing-tailwindcss-classes-and-html-attributes
class ApplicationViewComponent < ViewComponentContrib::Base
# Move default attrs to a constants so we can re-use it
EMPTY_ATTRS = {}.freeze
class << self
def html_option(name, default: nil, **opts)
if default
opts[:type] = proc { default.merge(_1) }
end
@palkan
palkan / patch.rb
Created November 14, 2023 02:42
TestProf vs. Wisper
# frozen_string_literal: true
# Patch Wisper to disable broadcasting by default
# (to speed up tests).
# Profile Wisper publishing via EventProf
TestProf::EventProf.monitor(Wisper::Publisher, "wisper.publisher.broadcast", :broadcast)
module Wisper
module Testing
@palkan
palkan / railsbytes.rb
Last active March 16, 2023 20:18
thor railsbytes:template
# This a custom Thor command which can be used to install RailsBytes templates
# without Rails.
# The primary use case is to make it possible to apply templates without a Rails app being installed.
# (For example, when you want to configure a dev environment and cannot run `bundle install`).
#
# Usage:
#
# 1) Install Thor:
#
# gem install thor
@palkan
palkan / double_query.txt
Created November 15, 2022 02:25
Syntax Tree doubles query
# Usage:
# gem install syntax_tree
# stree double_query.txt spec/**/*.rb
#
CallNode[
receiver: NilClass,
message: Ident[value: "double" | "instance_double"],
arguments: ArgParen[
arguments: Args[
parts: [StringLiteral | VarRef[value: Const], BareAssocHash]
@palkan
palkan / index.html.erb
Created October 8, 2022 18:15
Action Cable image publisher
<div class="min-w-full flex flex-row">
<%= turbo_stream_from "demo" %>
<div id="demo"></div>
</div>
@palkan
palkan / stdout.rb
Created August 29, 2022 22:03
Yabeda stdout adapter
# frozen_string_literal: true
require "yabeda/base_adapter"
module Yabeda
module STDOUT
class Adapter < BaseAdapter
def register_counter!(_metric)
end
@palkan
palkan / example.rb
Created August 16, 2022 19:33
pattern_matching_joe.rb
require 'active_support/core_ext/hash/indifferent_access'
def send_admin_notification(changes = previous_changes)
case changes
in ends_at: [Time, _]
:resubscribed
in ends_at: [nil, Time]
:churned
in processor_plan: [nil, String]
:subscribed
@palkan
palkan / mailers_preview_spec.rb
Created July 8, 2022 16:15
mailers_preview_spec.rb
# frozen_string_literal: true
require "rails_helper"
# Automatically smoke-test all mailer previews
describe "/rails/mailers" do
subject { response }
ActionMailer::Preview.all.each do |preview|
next if preview.emails.empty?
# frozen_string_literal: true
require "benchmark_driver"
source = <<~RUBY
class Hash
def symbolize_keys
transform_keys { |key| key.to_sym rescue key }
end