Skip to content

Instantly share code, notes, and snippets.

View adrienpoly's full-sized avatar
💭
I may be slow to respond.

Adrien Poly adrienpoly

💭
I may be slow to respond.
View GitHub Profile
@skatkov
skatkov / test.rb
Last active January 3, 2024 18:20
speed up testsuit by using unlogged tables in PG
# 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
resources :media, shallow: true,
constraints: ->(req) do
current_user = req.env["warden"].user(:user)
Flipper.enabled?(:media_uploads, current_user)
end
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
# 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 April 2, 2024 16:21
Avo resource view helpers
# 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
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 April 24, 2024 10:53
HEY's Stimulus Pagination Controller
/*
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>
@adrienpoly
adrienpoly / application.html.erb
Last active March 22, 2024 08:14
Capybara / Stimulus test helper to ensure JS is ready when test starts
<!DOCTYPE html>
<html>
<head>
...
</head>
<body data-controller="js">
<%= yield %>
</body>
</html>
// This code is to be used with https://turbo.hotwire.dev. By default Turbo keeps visited pages in its cache
// so that when you visit one of those pages again, Turbo will fetch the copy from cache first and present that to the user, then
// it will fetch the updated page from the server and replace the preview. This makes for a much more responsive navigation
// between pages. We can improve this further with the code in this file. It enables automatic prefetching of a page when you
// hover with the mouse on a link or touch it on a mobile device. There is a delay between the mouseover event and the click
// event, so with this trick the page is already being fetched before the click happens, speeding up also the first
// view of a page not yet in cache. When the page has been prefetched it is then added to Turbo's cache so it's available for
// the next visit during the same session. Turbo's default behavior plus this trick make for much more responsive UIs (non SPA).