Skip to content

Instantly share code, notes, and snippets.

View walterdavis's full-sized avatar

Walter Lee Davis walterdavis

View GitHub Profile
@andynu
andynu / show_method_history.rb
Created August 26, 2022 18:56
Given a ruby file and method name shows you all the different versions across the git history.
#!/usr/bin/env ruby
# Given a file and method_name
# Show all the different implementations across the git history (first commit per implementation).
#
# show_method_history <file> <method_name> --html
#
# e.g. show_method_history test/test_helper.rb sign_in --html
#
# WARNING: the --html output just dumps html files into your current folder.
#
@alvincrespo
alvincrespo / application_helper.rb
Created July 21, 2022 11:02
TailwindUI + will_paginate renderer
module ApplicationHelper
def will_paginate(coll_or_options = nil, options = {})
if coll_or_options.is_a? Hash
options = coll_or_options
coll_or_options = nil
end
options = options.merge renderer: TailwindUIPaginationRenderer unless options[:renderer]
super(*[coll_or_options, options].compact)
end
end
@crawler
crawler / what_i_serialize.rb
Last active January 10, 2024 16:59
WHAT I SERIALIZE?
# frozen_string_literal: true
###############################################################################################
# WHAT I SERIALIZE? #
###############################################################################################
# This scrip can help you to find what object types you need to witelist after CVE-2022-32224 update
# AD: If you using StimulusJS then checkout my gem stimulus_tag_helper
# https://rubygems.org/gems/stimulus_tag_helper
# https://github.com/crawler/stimulus_tag_helper
@andynu
andynu / routes_check.rake
Last active June 1, 2022 01:40
Parses production logs and checks which routes are used out of all the routes that are defined.
desc 'Show basic controller usage stats'
task :controllers => :environment do
logfiles = Dir['log/%s.log*' % Rails.env].sort
logs_gz, logs_txt = logfiles.partition{|f| Pathname.new(f).extname == '.gz' }
results = `ag Started -A 1 #{logs_txt.join(' ')}`
unless logs_gz.empty?
results << `zcat #{logs_gz.join(' ')} |ag Started -A 1`
end
Event = Struct.new(:http_method, :uri_path, :client_ip, :requested_at_str, :controller_name, :controller_action, :format) do
def requested_at
@natematykiewicz
natematykiewicz / unroutable_routes.rake
Last active June 9, 2022 19:47
Find routes that will raise a routing error when requested
desc 'Find routes that will raise a routing error when requested'
task unroutable_routes: :environment do
# A lot of this code was taken from how `rake routes` works
# https://github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4/railties/lib/rails/commands/routes/routes_command.rb
require 'action_dispatch/routing/inspector'
unroutables = Rails.application.routes.routes.
map { |r| ActionDispatch::Routing::RouteWrapper.new(r) }.
reject { |r| r.internal? || r.engine? || r.path.starts_with?('/rails/') || !r.controller }.
@andynu
andynu / example.sh
Last active January 19, 2023 16:40
rails-upgrade script, a helper for moving pins and doing other checks during ruby upgrades
# I have lots of projects. As I do the rails upgrades I collect the pin changes and other adjustments here
# towards the end of upgrading all the projects it gets easier and easier because of these commands.
❯ ./rails-upgrade
Commands:
rails-upgrade apple_touch # add blank apple-touch icons
rails-upgrade application_record # adds ApplicationRecord and replaces all references to ActiveRecord::Base with it
rails-upgrade asset_precompile_check # look for assets that need to be added to the assets initializer precompile list
rails-upgrade assigns_check # the assigns method has been extracted to a gem, check if it is used, and add the gem
rails-upgrade before_filter # change before_filter to before_action in controllers
@dalezak
dalezak / application_controller.rb
Last active February 23, 2022 11:31
Lazy Load CanCanCan Abilities In Rails
def current_ability
@current_ability ||= begin
current_ability = Ability.new(current_user)
controller_names.to_a.each do |controller_name|
model_name = controller_name.classify
model_ability = "#{model_name}Ability".constantize rescue nil
if model_ability.present? && model_abilities[model_ability].nil?
model_abilities[model_ability] = model_ability.new(current_user)
current_ability.merge(model_abilities[model_ability])
end
@tbuehlmann
tbuehlmann / comment.rb
Last active February 13, 2021 15:18
Flaky!
class Comment < ActiveRecord::Base
belongs_to :post, counter_cache: true
end
// 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).
@rahulramfort
rahulramfort / pre-commit
Created August 19, 2020 16:02
Pre-commit hook - Validate Syntax for Ruby files
#!/usr/bin/env ruby
=begin
A hook to make sure that all staged .rb files are syntatically correct.
The hook tries to run `ruby -c file_path` for all those files and
prints outs the errors if any and halts the commit.
If you want to use this pre-commit, simply copy the code and create a
file called 'pre-commit' inside your .git/hooks directory.