Skip to content

Instantly share code, notes, and snippets.

View amitpatelx's full-sized avatar

Amit Patel amitpatelx

View GitHub Profile
@joeldrapper
joeldrapper / fingerprinting.rb
Created January 10, 2024 14:30
Rails request fingerprinting concern
# frozen_string_literal: true
module Fingerprinting
def full_fingerprint
generate_fingerprint(
ip_fingerprint,
browser_fingerprint
)
end
@skatkov
skatkov / forever.sh
Created November 28, 2023 10:48
run tests forever (until they fail)
while bundle exec rake test; do :; done
@veekaybee
veekaybee / normcore-llm.md
Last active May 6, 2024 16:10
Normcore LLM Reads

Anti-hype LLM reading list

Goals: Add links that are reasonable and good explanations of how stuff works. No hype and no vendor content if possible. Practical first-hand accounts of models in prod eagerly sought.

Foundational Concepts

Screenshot 2023-12-18 at 10 40 27 PM

Pre-Transformer Models

@tannakartikey
tannakartikey / complete_reset.rake
Last active August 11, 2023 16:08
Destroy the schema and re-create the database from scratch
namespace :db do
desc 'reset the database by dropping the schema'
task complete_reset: :environment do
raise unless Rails.env.local?
FileUtils.rm_f('db/schema.rb')
Rake::Task['db:drop'].invoke
Rake::Task['db:create'].invoke
Rake::Task['db:migrate'].invoke
Rake::Task['db:seed'].invoke
# Prompt:
What’s your most elegant Ruby code that does this:
* takes an array of strings and a target string
* if the target string is not in the array, return the array
* if the target string is in the array, return the array with the target string moved from its position to the end of the array.
What’s your most elegant Ruby code that does this:
* takes an array of strings and a target string
@alexrudall
alexrudall / #ChatGPT Streaming.md
Last active May 3, 2024 02:54
ChatGPT streaming with ruby-openai, Rails 7, Hotwire, Turbostream, Sidekiq and Tailwind!

How to add ChatGPT streaming to your Ruby on Rails 7 app!

This guide will walk you through adding a ChatGPT-like messaging stream to your Ruby on Rails 7 app using ruby-openai, Rails 7, Hotwire, Turbostream, Sidekiq and Tailwind. All code included below!

Alt Text

First, add the ruby-openai gem! Needs to be at least version 4. Add Sidekiq too.

@okuramasafumi
okuramasafumi / included.rb
Created April 17, 2023 13:27
Tell me if this module is included by the same class more than once
def self.included(base)
base.instance_variable_set(:@count, 0) unless base.instance_variable_defined?(:@count)
base.instance_variable_set(:@count, base.instance_variable_get(:@count) + 1)
puts base.name if base.instance_variable_get(:@count) > 1
end
@CyJimmy264
CyJimmy264 / enum_validatable.rb
Created February 28, 2023 09:39
ActiveRecord::Enum validation in Rails API
# frozen_string_literal: true
# ActiveRecord::Enum validation in Rails API
# https://medium.com/nerd-for-tech/using-activerecord-enum-in-rails-35edc2e9070f
module EnumValidatable
extend ActiveSupport::Concern
class_methods do
def validatable_enum(*enums_to_fix)
enums_to_fix.each do |element|
on:
workflow_call:
inputs:
deploy_target:
description: 'Deploy target'
required: true
type: string
default: 'production'
secrets:
GH_TOKEN:
@Envek
Envek / login_helpers.rb
Created October 11, 2021 06:42
Signing-in user for integration tests via cookie-only session with Rails, Devise, Capybara, and Cuprite
# spec/system/support/login_helpers.rb
# See this blog post for setup guide: https://evilmartians.com/chronicles/system-of-a-test-setting-up-end-to-end-rails-testing
module LoginHelpers
def login_as(user)
# Craft session cookie to make request authenticated (to pass even routing constraints)
# Compilation of these:
# - https://dev.to/nejremeslnici/migrating-selenium-system-tests-to-cuprite-42ah#faster-signin-in-tests
# - https://turriate.com/articles/2011/feb/how-to-generate-signed-rails-session-cookie
# - https://github.com/rails/rails/blob/43e29f0f5d54294ed61c31ddecdf76c2e1a474f7/actionpack/test/dispatch/cookies_test.rb#L350