Skip to content

Instantly share code, notes, and snippets.

View claudiug's full-sized avatar
💜
I eat stickers all the time, dude.

Klaus Heissler claudiug

💜
I eat stickers all the time, dude.
  • cto@relatico
  • Berlin
View GitHub Profile
@claudiug
claudiug / cheatsheet.rb
Created January 15, 2023 21:23 — forked from mabenson00/cheatsheet.rb
Rails ActiveRecord JSON cheatsheet
# Basic key operators to query the JSON objects :
# #> : Get the JSON object at that path (if you need to do something fancy)
# -> : Get the JSON object at that path (if you don't)
# ->> : Get the JSON object at that path as text
# {obj, n} : Get the nth item in that object
# https://www.postgresql.org/docs/9.4/functions-json.html#FUNCTIONS-JSONB-OP-TABLE
# Date
# date before today
@claudiug
claudiug / example_reflex.rb
Created January 10, 2022 09:53 — forked from leastbad/example_reflex.rb
slim-select Stimulus controller w/ Reflex callback
class UsersReflex < ApplicationReflex
def search(name)
users = User.where("name LIKE :prefix", prefix: "#{name}%")
result = users.map { |user| { text: user.name, value: user.id }}
cable_ready.dispatch_event(name: "data", detail: {options: result}).broadcast
morph :nothing
end
end
@claudiug
claudiug / customer_reflex.rb
Created January 4, 2022 08:21 — forked from leastbad/customer_reflex.rb
TomSelect + StimulusReflex 3.5 (w/payload)
class CustomersReflex < ApplicationReflex
def lookup(search)
# use search string to do some kind of DB lookup here
self.payload = {} # I have NO IDEA what tom-select expects!
morph :nothing
end
end
@claudiug
claudiug / select_controller.js
Created January 4, 2022 08:20 — forked from leastbad/select_controller.js
SlimSelect Stimulus wrapper
import ApplicationController from '../application_controller'
import SlimSelect from 'slim-select'
export default class extends ApplicationController {
static values = {
limit: Number,
placeholder: String,
searchText: String,
searchingText: String,
reflex: String,
@claudiug
claudiug / ruby31onrails.md
Created December 26, 2021 10:37 — forked from yahonda/ruby31onrails.md
Ruby 3.1 on Rails

Ruby 3.1 on Rails

Actions required to use Ruby 3.1.0 with Rails

Rails 7.0.Z

  • Rails 7.0.0 is not compatible with Ruby 3.1.0.
  • "Rails::Engine is abstract, you cannot instantiate it directly.."
    $ bin/rails s
    Calling `DidYouMean::SPELL_CHECKERS.merge!(error_name => spell_checker)' has been deprecated. Please call `DidYouMean.correct_error(error_name, spell_checker)' instead.

[WARNING] Could not load command "rails/commands/server/server_command". Error: Rails::Engine is abstract, you cannot instantiate it directly..

@claudiug
claudiug / losing_my_mind_on_concurrency.rb
Created May 11, 2021 07:13 — forked from noteflakes/losing_my_mind_on_concurrency.rb
A script showing how to use Polyphony to perform HTTP requests concurrently from a queue of jobs
# "I'm losing my mind on concurrency"
# https://www.reddit.com/r/ruby/comments/lmg8xo/im_losing_my_mind_on_concurrency/
require 'polyphony'
require 'httparty'
require 'json'
CHANNELS = ['list', 'of', 'channels']
JOB_QUEUE = Queue.new
TIMEOUT = 60 # 60 second timeout
@claudiug
claudiug / idb-backup-and-restore.md
Created March 31, 2021 16:35 — forked from loilo/idb-backup-and-restore.md
Back up and restore an IndexedDB database

Back up and restore an IndexedDB database

This gist is an ES module which provides functions to import and export data from an IndexedDB database as JSON. It's based on Justin Emery's indexeddb-export-import package, but applies some adjustments that reflect better on the current browser landscape (i.e. better developer ergonomics but no support for Internet Explorer).

Usage

For each of the provided functionalities, you need a connected IDBDatabase instance.

Export Data

import { idb } from 'some-database'

Faster Rails tests

Feedback loop speed in one of the biggest contributing factors to overall development time. The faster you get results, the faster you can move on to other things. A fast enough test suite is therefore critical to teams' success, and is worth investing some time at the beginning to save in the long run.

Below is a list of techniques for speeding up a Rails test suite. It is not comprehensive, but should definitely provide some quick wins. This list of techniques assumes you're using minitest, but most everything should translate over to rspec by simply replacing test/test_helper.rb with spec/spec_helper.rb.

module Slug
def self.customize(field: :name)
Module.new do
define_method :to_param do
public_send(field).downcase.gsub /\W+/, '-'
end
end
end
end
@claudiug
claudiug / missing_indices.sql
Created April 7, 2020 20:35 — forked from consti/missing_indices.sql
Find missing indices in postgres
SELECT relname, seq_scan-idx_scan AS too_much_seq, case when seq_scan-idx_scan>0 THEN 'Missing Index?' ELSE 'OK' END, pg_relation_size(relname::regclass) AS rel_size, seq_scan, idx_scan
FROM pg_stat_all_tables
WHERE schemaname='public' AND pg_relation_size(relname::regclass)>80000 ORDER BY too_much_seq DESC;