Skip to content

Instantly share code, notes, and snippets.

View jaynetics's full-sized avatar

Janosch Müller jaynetics

View GitHub Profile
@jaynetics
jaynetics / find_unused_routes.rake
Last active May 15, 2023 19:38
Find rails routes with no corresponding controller action
desc 'Finds unused routes (routes with no corresponding controller action)'
task find_unused_routes: :environment do
checked_actions = %i[create destroy edit index new show update]
Rails.application.eager_load!
endpoints = Rails.application.routes.routes.map do |route|
ActionDispatch::Routing::RouteWrapper.new(route).endpoint
end
desc 'Finds unused partials (i.e. view files starting with "_"). '\
'May find false positives (programmatically chosen partials).'
task :find_unused_partials do
UnusedPartialFinder.run
end
module UnusedPartialFinder
module_function
RENDER_CALL_REGEX = %r{\Wrender[^'"]*(['"])/?((?:(?!\1).)+)\1}
desc 'Finds unused views (i.e. view files not starting with "_"). '\
'May find false positives (programmatically chosen views).'
task find_unused_views: :environment do
UnusedViewFinder.run
end
module UnusedViewFinder
module_function
ALLOWLISTED_PATHS_REGEX = %r{
@jaynetics
jaynetics / factory_bot_spec.rb
Created September 26, 2022 10:30
find broken factory_bot factories that don't work or create excessive records
FactoryBot.factories.each do |factory|
name = factory.names.count == 1 ? factory.names.first : fail
klass = factory.build_class
next unless ActiveRecord::Base.in?(klass.ancestors)
RSpec.describe "factory :#{name}" do
it "creates a single #{klass} and no insane number of other records" do
record_counts = -> do
ActiveRecord::Base.descendants.to_h { |c| [c.name.to_sym, (c.count rescue 0)] }
end
@jaynetics
jaynetics / fetch_gem_version_release_dates.rb
Last active December 28, 2022 18:53
fetch release dates of gem versions in your gemfile / bundle
require 'json'
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'excon'
gem 'fortschritt'
end
# get list of gems from bundle
@jaynetics
jaynetics / swap_credentials
Created November 29, 2021 11:06
Script to swap between two rubygems credentials files
#!/usr/bin/env ruby
# put in credentials dir, e.g. ~/.local/share/gem/swap_credentials
require 'fileutils'
active_path = "#{__dir__}/credentials"
memo_path = "#{__dir__}/memo"
current = File.exist?(memo_path) && File.read(memo_path)
Dir["#{__dir__}/credentials_*"].each do |cred_path|
@jaynetics
jaynetics / scanner.rb
Created December 25, 2020 20:24
regexp_parser scanner.rb example (compiled from scanner.rl)
# -*- warn-indent:false; -*-
# line 1 "~/code/regexp_parser/lib/regexp_parser/scanner/scanner.rl"
# line 700 "~/code/regexp_parser/lib/regexp_parser/scanner/scanner.rl"
# THIS IS A GENERATED FILE, DO NOT EDIT DIRECTLY
# This file was generated from lib/regexp_parser/scanner/scanner.rl
@jaynetics
jaynetics / subarray_combinations.rb
Created April 29, 2019 09:59
subarray / 2d-array / cross-array combinations in ruby
# subarray_combinations([['a', 'b'], [1, 2]])
# # => [['a', 1], ['a', 2], ['b', 1], ['b', 2]]
def subarray_combinations(array_of_arrays, index = 0)
return [] if index == array_of_arrays.size
subarray = array_of_arrays[index]
tails = subarray_combinations(array_of_arrays, index + 1)
if tails.empty?
require 'stringex'
require 'sexy_slug'
require 'benchmark'
string = (0..0xD799).map { |cp| cp.chr('utf-8') }.join
Benchmark.bmbm do |x|
x.report { string.to_url }
x.report { SexySlug.from(string) }
end