Skip to content

Instantly share code, notes, and snippets.

View jaydorsey's full-sized avatar
💊
Ruby on Rails

Jay Dorsey jaydorsey

💊
Ruby on Rails
View GitHub Profile
@synth
synth / DbMaxLengthValidator.rb
Last active October 19, 2022 13:51
Custom Validator for Max Length based on database adapter
module DbMaxLengthValidatorConcern
extend ActiveSupport::Concern
included do
cattr_accessor :column_varchar_max_lengths
self.column_varchar_max_lengths = {}
end
module ClassMethods
@itkrt2y
itkrt2y / association.rb
Last active June 29, 2024 12:45
Association dataloader with graphql-ruby
# official docs: https://graphql-ruby.org/dataloader/sources.html
# app/graphql/sources/association.rb
class Sources::Association < ::GraphQL::Dataloader::Source
def initialize(association_name, scope = nil)
@association_name = association_name
@scope = scope
end
def fetch(records)
# BAD each iteration loads invoice model
class Company < ApplicationRecord
has_many :invoices
end
class Invoice < ApplicationRecord
belongs_to :company
end
@palkan
palkan / README.md
Created January 24, 2022 13:29
Rails boot time profiling

Add the following to application.rb:

$icallbacks = []
$icallbacks.define_singleton_method(:print) do
  puts sort_by { |(a, b)| -b }.map { |(a, b)| "#{b}\t\t#{a}" }.join("\n")
end

ActiveSupport::Notifications.subscribe("load_config_initializer.railties") do |event|
 $icallbacks &lt;&lt; [event.payload[:initializer], event.duration]
@brenogazzola
brenogazzola / custom_active_storage_urls.rb
Last active October 17, 2022 15:34
Creating a custom ActiveStorage controller to generate "pretty urls" or help migration from another lib
# This demonstrates how we create a SEO friendly url for the previews of the artworks we sell.
#
# This is the URL we want:
# https://festalab.com.br/image/invitation/birthday/carnival.jpg
#
# First, the route.
#
# "model" is the name of the active storage model that has the preview.
# "classification" and "identifier" are together a unique key for the records.
@ykpythemind
ykpythemind / spec_splitter.rb
Created May 20, 2021 14:22
GitHub Action + Rails test example
# テストの並列実行用にspecファイルを分割する
require 'optparse'
options = {}
OptionParser.new do |o|
o.on('--glob=OPT', 'glob') { |v| options[:glob] = v }
o.on('--node-index=OPT', 'node-index') { |v| options[:node_index] = v.to_i }
o.on('--node-count=OPT', 'node-count') { |v| options[:node_count] = v.to_i }
end.parse!(ARGV.dup)
@ykpythemind
ykpythemind / test.yml
Last active May 15, 2024 04:49
GitHub Action + Rails test example
# .github/workflows/test.yml
name: test
on: [push]
env:
RUBY_VERSION: 2.7.2
NODE_VERSION: 14.15.5
RAILS_ENV: test
@benkoshy
benkoshy / ransack-post-form-pagy-2.md
Last active May 17, 2024 02:29
Pagy Documentation - Using Stimulus JS

Using Stimulus JS to POST

Whenever you use pagy links that require interception, you will need to reinitialise any javascript that you need to run. If you submit a form, and use a turbo frame to render those search results, and to also render the pagination links to those results, how are you going to reinitialize your javascript code to intercept those pagy page links?

Stimulus JS is very handy for reinitialising javascript code, and is useful if you are using a library like hotwire (by Basecamp) where changes are made to the DOM.

// pagy_controller.js - a stimulus JS controller
import { Controller } from "@hotwired/stimulus"
@invalidusrname
invalidusrname / install_dash_gem_docs.rb
Created August 26, 2020 12:23 — forked from jasonnoble/install_dash_gem_docs.rb
Installs the Dash docs for all the Ruby Gems in your Gemfile
#!/usr/bin/env ruby
#
# Prerequisites:
# gem install bundler
# bundle install
dependencies = `bundle show | grep '*' | awk '{print $2, $3}' | sed -e 's/(//' -e 's/)//'`.split("\n")
dependencies.each do |dependency|
(gem_name, version) = dependency.split
@carlosramireziii
carlosramireziii / allow_content_type.rb
Last active September 22, 2023 21:39
A validator and RSpec matcher for restricting an attachment’s content type using Active Storage
require "rspec/expectations"
RSpec::Matchers.define :allow_content_type do |*content_types|
match do |record|
matcher.matches?(record, content_types)
end
chain :for do |attr_name|
matcher.for(attr_name)
end