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
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
# https://observablehq.com/@asg017/introducing-sqlite-vss
# https://observablehq.com/@asg017/making-sqlite-extension-gem-installable
@kddnewton
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 "https://rubygems.org"
gem "json_pure"
gem "benchmark-ips"
end
@skatkov
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
end
View routes.rb
resources :media, shallow: true,
constraints: ->(req) do
current_user = req.env["warden"].user(:user)
Flipper.enabled?(:media_uploads, current_user)
end
View chart.rb
class Chart < ApplicationRecord
validate do |chart|
schema = Rails.cache.fetch("vega_lite_schema/v5") do
Faraday.get("https://vega.github.io/schema/vega-lite/v5.json").body
end
JsonSchemaValidator.new(chart, schema: schema, json: vega_json, field: :vega_json).validate
end
end
@sunny
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 https://gist.github.com/ta1kt0me/6a7058d16621785d4f7038bde6cd3b98
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
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
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? %>
</section>