Skip to content

Instantly share code, notes, and snippets.

View dpaluy's full-sized avatar

David Paluy dpaluy

  • Majestic Labs
  • Austin, TX
  • X @dpaluy
View GitHub Profile
@dpaluy
dpaluy / rate_limiter.rb
Created July 30, 2022 23:27
Rate Limiter middleware
# SOURCE: https://dev.to/lorankloeze/how-to-create-middleware-in-your-rails-application-ep
#
# frozen_string_literal: true
class RateLimiter
MAX_PER_WINDOW = 50
WINDOW_SIZE = 1.minute
def initialize(app)
@app = app
@dpaluy
dpaluy / cents.rb
Last active September 6, 2022 02:20
a custom type that defines a cast method which will branch on the not-completely-bulletproof heuristic of assuming Numeric values are already in cents and any other values will need to be converted from dollars.
class Cents < ActiveRecord::Type::Integer
def cast(value)
return super if value.is_a?(Numeric)
price_in_dollars = value.to_s.delete("$").to_d
price_in_cents = (price_in_dollars * 100).to_i
super(price_in_cents)
end
def self.dollarize(value)
@dpaluy
dpaluy / bash.sh
Created November 30, 2022 04:39
Mac Goodies
# 𝗙𝗮𝘀𝘁𝗲𝗿 𝗗𝗼𝗰𝗸 𝗛𝗶𝗱𝗶𝗻𝗴
defaults write com.apple.dock autohide-delay -float 0; defaults write com.apple.dock autohide-time-modifier -int 0.05;killall Dock
# Dismiss unmount notifications
sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.DiskArbitration.diskarbitrationd.plist DADisableEjectNotification -bool YES && sudo pkill diskarbitrationd
@dpaluy
dpaluy / logos.html
Created November 30, 2022 17:27
CSS tips for working with inconsistently sized logos
<style>
.photos img {
width: 15%;
aspect-ration: 3/2;
object-fit: contain;
mix-blend-mode: color-burn;
}
.photos {
background: #dfdfdf;
@dpaluy
dpaluy / dashboard.html.erb
Created December 2, 2022 03:22
Render partial within a Tab in Rails
<%= turbo_frame_tag "tabs", target: "tab-content" do %>
<%= link_to 'Tab 1', tab_1_path %>
<%= link_to 'Tab 2', tab_2_path %>
<% end -%>
<%= turbo_frame_tag "tab-content", src: tab_1_path, loading: "eager" %>
# Read more: https://turbo.hotwired.dev/handbook/frames
@dpaluy
dpaluy / modal.html
Created December 2, 2022 05:23
StimulusJS controller that will close a modal when the user clicks outside of it
<div data-controller="modal" data-action-"click@document->modal#close">
<div class="modal" data-target="modal.modal">
... modal content goes here
</div>
</div>
@dpaluy
dpaluy / color_palette.rb
Created December 15, 2022 20:36
color-pallette
# frozen_string_literal: true
class ColorPalette
DEFAULT_BASE = 3
DEFAULT_SATURATION = 0.5
DEFAULT_LIGHTNESS_MIN = 0.4
DEFAULT_LIGHTNESS_MAX = 0.8
DEFAULT_LIGHTNESS_DECAY = 20
# Generate a color scheme with a given number of colors.
@dpaluy
dpaluy / device_password_reset.rb
Created January 4, 2023 15:56
Manually reset password with Device
raw, hashed = Devise.token_generator.generate(User, :reset_password_token)
user = User.find_by(email: '[email protected]')
user.reset_password_token = hashed
user.reset_password_sent_at = Time.now.utc
user.save
# https://mysaas.com/users/password/edit?reset_password_token=raw
@dpaluy
dpaluy / referrable.rb
Created February 12, 2023 19:14
Referrable Rails
module Referrable
extend ActiveSupport: :Concern
included do
has_one referral, foreign_key: "referred_user_id", dependent: : destroy has_one referring_user, through: :referral alias_method :referred_by, :referring_user
has_many :referrals, foreign_key: "referring_user_id", dependent: :nullify has_many :referred_users, through: referrals
validates :referral_code, uniqueness: true, allow_nil: true
end
end
@dpaluy
dpaluy / http_debug.rb
Created April 29, 2023 03:46
Debug Net HTTP
module Net::HTTPWithDebug
def initialize(*args)
super
set_debug_output(STDERR)
end
end
Net::HTTP.prepend(Net::HTTPWithDebug)