This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The steps are taken out of https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-ubuntu-18-04 | |
# Steps to download and run this on your machine: | |
## Download the script | |
# wget https://gist.githubusercontent.com/binarygit/de4ba791bae94c633411d0fb51333728/raw/e3845300fe16fb8751a29757db2719d4cbecb591/install-ruby-and-rails.bash | |
## Source to run it because running the script like a command | |
## will run it in a new process. This new process won't be able | |
## to access the updated bashrc when we update it when installing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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. | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Comment < ActiveRecord::Base | |
belongs_to :post, counter_cache: true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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). | |
NewerOlder