Skip to content

Instantly share code, notes, and snippets.

View full-sized avatar
I may be slow to respond.

Adrien Poly adrienpoly

I may be slow to respond.
View GitHub Profile
peterc / embedding_store.rb
Last active September 14, 2023 01:45
Using SQLite to store OpenAI vector embeddings from Ruby
View embedding_store.rb
# Example of using SQLite VSS with OpenAI's text embedding API
# from Ruby.
# Note: Install/bundle the sqlite3, sqlite_vss, and ruby-openai gems first
# OPENAI_API_KEY must also be set in the environment
# Other embeddings can be used, but this is the easiest for a quick demo
# More on the topic at
kddnewton / json.rb
Last active August 7, 2023 19:54
Faster JSON parser in Ruby
View json.rb
# frozen_string_literal: true
require "bundler/inline"
gemfile do
source ""
gem "json_pure"
gem "benchmark-ips"
skatkov / test.rb
Last active August 12, 2023 10:11
speed up testsuit by using unlogged tables in PG
View test.rb
# config/environments/test.rb
ActiveSupport.on_load(:active_record_postgresqladapter) do
# For this optimization to work, you need to recreate your test database
self.create_unlogged_tables = true
View routes.rb
resources :media, shallow: true,
constraints: ->(req) do
current_user = req.env["warden"].user(:user)
Flipper.enabled?(:media_uploads, current_user)
View chart.rb
class Chart < ApplicationRecord
validate do |chart|
schema = Rails.cache.fetch("vega_lite_schema/v5") do
end, schema: schema, json: vega_json, field: :vega_json).validate
sunny / add_frozen_string_literal_comment.rb
Last active November 23, 2022 17:03 — forked from ta1kt0me/add_frozen_string_literal_comment.rb
Add frozen string literal comment into generated files in Rails 7
View add_frozen_string_literal_comment.rb
# frozen_string_literal: true
# Via
module RailsGeneratorFrozenStringLiteralPrepend
RUBY_EXTENSIONS = %w[.rb .rake].freeze
def render
return super unless RUBY_EXTENSIONS.include?(File.extname(destination))
"# frozen_string_literal: true\n\n#{super}"
davidlormor / example_resource.rb
Last active January 25, 2023 16:11
Avo resource view helpers
View example_resource.rb
# frozen_string_literal: true
class ExampleResource < Avo::BaseResource
include ResourceExtensions
field :id, as: :id
field :name, as: :text
index do
field :some_field, as: :text
View prefetch_controller.js
import { Controller } from "stimulus";
import { get } from "@rails/request.js";
import { PageSnapshot } from "@hotwired/turbo";
export default class extends Controller {
static values = { hoverTime: Number };
connect() {
this.element.addEventListener("mouseover", this.prefetch.bind(this));
this.element.addEventListener("touchstart", this.prefetch.bind(this));
dhh / pagination_controller.js
Last active March 9, 2023 12:19
HEY's Stimulus Pagination Controller
View pagination_controller.js
ERB template chunk from The Feed's display of emails:
<section class="postings postings--feed-style" id="postings"
data-controller="pagination" data-pagination-root-margin-value="40px">
<%= render partial: "postings/snippet", collection: @page.records, as: :posting, cached: true %>
<%= link_to(spinner_tag, url_for(page: @page.next_param),
class: "pagination-link", data: { pagination_target: "nextPageLink", preload: @page.first? }) unless @page.last? %>