Skip to content

Instantly share code, notes, and snippets.

View humphreyja's full-sized avatar

Jake Humphrey humphreyja

View GitHub Profile
@humphreyja
humphreyja / RelayEnvironment.js
Created November 24, 2025 19:12
How does Relay work?
/*
Relay handles state by pretty much a context like store (or an external context like Redux).
It's flat storage so every object must have a unique id (or key) or else it will overwrite other values.
There's also an "optimistice" state that represents optimistic updates. These values are shown in priority
to what is actually in the state but can also easily be rolled back.
This is obviously a dumbed down version of relay so I didn't fully implement optimistic updates, but I know relay
has the ability to roll back optimistic updates for a specific mutation instead of all updates like I have here.
Also my "setter" is really rudimentary, it would cause a rerender on every value change instead of batching the updates.
@humphreyja
humphreyja / calendar.html.erb
Created January 5, 2021 00:46
Hotwire Server Rendered Popup Date Picker
<% frame_name = 'calendar_interface' %>
<% frame_name = "#{frame_name}_#{@target}" if @target %>
<% start_of_month = @display_date.beginning_of_month %>
<% cache "ui/calendar/#{frame_name}/#{start_of_month}--#{@selected_date}" do %>
<% end_of_month = @display_date.end_of_month %>
<% dow_start = @display_date.beginning_of_month.days_to_week_start + 2 %>
<% dow_start = dow_start >= 7 ? dow_start - 7 : dow_start %>
<% dow_start = dow_start <= 0 ? dow_start + 7 : dow_start %>
<% today = Date.today %>
@humphreyja
humphreyja / active_admin.rb
Created November 13, 2019 19:58
Include react components into active admin using React On Rails.
# config/initializers/active_admin.rb
#...
module ActiveAdmin
module Views
# Defines a `react_component` active admin component. It allows you
# to include react components into active admin using React On Rails.
# Example:
#
// Copyright (c) 2016 - 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
@humphreyja
humphreyja / favicon.rake
Created October 24, 2018 05:17
Simple rails task that will take a `template.png` image and generate all the favicons you need for your app. It also generates a view partial that you can include. Uses Image Magick under the hood. Would recommend later polishing up each image but this gets you set up pretty quickly.
namespace :favicons do
desc "Build favicons for all platforms"
task build: :environment do
if /Version/m =~ (`convert -version`)
# Main Icon Sizes
ico_sizes = %w(16 32)
apple_sizes = %w(57 60 72 76 114 120 129 144 152)
apple_precomposed_sizes = %w(120 129 152)
ms_tile_sizes = %w(144)
@humphreyja
humphreyja / _info.html.erb
Created January 29, 2017 23:22
Info Message Box for Websites.
<div id="vue-info">
<div class="info-message info-message-1" :style="{top: html.top, left: html.left, right: html.right, bottom: html.bottom, position: html.position, height: html.height, width: html.width, maxHeight: html.maxHeight, maxWidth: html.maxWidth, display: containerOpen ? 'block' : 'none'}">
<svg class="container" viewbox="0 0 800 700" :style="{top: 0, left: 0, height: svgHeight, width: svgWidth, display: mobile ? 'none' : 'block'}" :class="[svgOpen ? 'open' : '']">
<defs>
<filter id="filter-dropshadow" x="0" y="0" width="140%" height="140%">
<feOffset result="offOut" in="SourceAlpha" dx="6" dy="3" />
<feColorMatrix result="matrixOut" in="offOut" type="matrix"
values="0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
@humphreyja
humphreyja / message_decrypter.ex
Created November 17, 2016 07:17
Decrypting the Rails session with Elixir
@moduledoc """
This uses the secret key base, the defined salt and signed salt from your Rails app to decrypt the Rails session. Checkout: https://github.com/cconstantin/plug_rails_cookie_session_store for a full session encrypter/decrypter for Phoenix. Below is how you would call these functions.
def get_session(conn) do
# Preload the cookies (If you are not in the browser pipeline
conn = Plug.Conn.fetch_cookies(conn)
# Get the cookies by the key defined in your Rails app 'config/initializers/session_store.rb'
cookie = Map.get(conn.cookies, "_yourappname_session")