Skip to content

Instantly share code, notes, and snippets.

View rubendinho's full-sized avatar

Ruben Izmailyan rubendinho

View GitHub Profile
@mjackson
mjackson / redirects-in-react-router-v6.md
Last active November 12, 2023 07:32
Notes on handling redirects in React Router v6, including a detailed explanation of how this improves on what we used to do in v4/5

Redirects in React Router v6

An important part of "routing" is handling redirects. Redirects usually happen when you want to preserve an old link and send all the traffic bound for that destination to some new URL so you don't end up with broken links.

The way we recommend handling redirects has changed in React Router v6. This document explains why.

Background

In React Router v4/5 (they have the same API, you can read about why we had to bump the major version here) we had a <Redirect> component that you could use to tell the router when to automatically redirect to another URL. You might have used it like this:

@KonnorRogers
KonnorRogers / capybara_read_spec.rb
Created April 22, 2021 21:26
Clipboard reading in Capybara
it "should read clipboard text" do
page.driver.browser.execute_cdp(
"Browser.setPermission",
{
origin: page.server_url,
permission: { name: "clipboard-read" },
setting: "granted",
})
clip_text = page.evaluate_async_script("navigator.clipboard.readText().then(arguments[0])")
expect(clip_text).to eq("copied text")
@arlogilbert
arlogilbert / cal2sheets.js
Last active June 19, 2020 19:12
Google Calendar to Google Sheets
/*
This is a Google Sheets Script. Create a script on the sheet you want to store your calendar entries in, save it, and set up a trigger.
Set this up as a timed trigger in the morning against a google sheet to have an always up to date list of who you have met with.
For the initial run you may want to set the daysBack to a large number like 999
*/
@ssnickolay
ssnickolay / base_mutation.rb
Created April 10, 2020 20:32
Ensuring Policies are Used in Graphql Mutation
class BaseMutation < GraphQL::Schema::RelayClassicMutation
include Graphql::ResolverCallbacks
include ActionPolicy::GraphQL::Behaviour
def current_user
context[:current_user]
end
# Enforce mutation authorization.
# This callbacks verifies that `authorize!` method has been
@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 / 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

@rubenmoya
rubenmoya / middleware.rb
Created April 22, 2019 14:41
Rails middleware to convert request params and response data from camelCase to snake_case
module CaseConverter
class Transformations
class << self
def transform(value)
case value
when Array then value.map { |item| transform(item) }
when Hash then value.deep_transform_keys! { |key| transform(key) }
when String then camelize(value)
else value
end
@seanlinsley
seanlinsley / chromedriver.rb
Created March 14, 2019 00:55
Ensures that chromedriver always matches the version of Chrome that's currently installed
# https://github.com/flavorjones/chromedriver-helper/issues/79
# http://chromedriver.storage.googleapis.com/index.html
require 'open-uri'
require 'shellwords'
executable = if RUBY_PLATFORM =~ /darwin/
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
else
'google-chrome'
@carlosramireziii
carlosramireziii / attached_validator.rb
Last active December 21, 2020 12:47
A validator and RSpec matcher for requiring an attachment using Active Storage
class AttachedValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors.add(attribute, :attached, options) unless value.attached?
end
end
@hopsoft
hopsoft / prefetch.js
Last active December 27, 2023 02:45
Turbolinks Prefetching
const hoverTime = 400
const fetchers = {}
const doc = document.implementation.createHTMLDocument('prefetch')
function fetchPage (url, success) {
const xhr = new XMLHttpRequest()
xhr.open('GET', url)
xhr.setRequestHeader('VND.PREFETCH', 'true')
xhr.setRequestHeader('Accept', 'text/html')
xhr.onreadystatechange = () => {