Skip to content

Instantly share code, notes, and snippets.

@jmmastey
Last active June 8, 2021 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmmastey/b5e25c64d907c98d1401a880fa0bb182 to your computer and use it in GitHub Desktop.
Save jmmastey/b5e25c64d907c98d1401a880fa0bb182 to your computer and use it in GitHub Desktop.
custom development commands
# Railsy Utilities
# To use these, move this file into your home directory as .rails_utils.sh
# and add the following to your ~/.zshrc or ~/.bashrc
#
# source ~/.rails_utils.sh
#
# Usage: Navigate your terminal to the source code of any gem in your bundle,
# specifically the correct _version_ of that gem.
#
# export/mealhand (main) > bcd dotenv
# .gem/ruby/2.7.3/gems/dotenv-2.7.5 >
#
function bcd() { cd $(bundle info "$@" | grep -e "Path: [^\S]*" | sed -e 's/^[^/]*//') ;}
# Usage: Open the webpage for any gem in your bundle. Almost always this is
# github. Opens in whatever your default browser is.
#
# export/mealhand (main) > bweb dotenv
#
function bweb() {
curl https://rubygems.org/api/v1/gems/"$@".json 2>/dev/null > /tmp/bweb
url=`jq -r '.source_code_uri // .homepage_uri' /tmp/bweb`
open $url
;}
# Usage: My setup seems to lose DB functions a _weird_ number of times. Just
# invokes the rake command to reinstall them.
alias dfl='RAILS_ENV=test bundle exec db:functions:load'
# Usage: depends on the above dfl command. Runs the Most Possible Profiling
# for a given spec. Includes counting DB usage, factory usage, and a bunch
# of other specific stats.
#
# export/mealhand (main) > profile spec/test_spec.rb
# export/mealhand (main) > vim tmp/profile/ar_most_recent.txt
# export/mealhand (main) > vim tmp/profile/fb_most_recent.txt
#
function profile() { echo 'dfl' && dfl; date; FACTORY_DEETS=1 be rspec $@ --require rspec-activerecord-formatter --require rails_helper --format ActiveRecordProgressFormatter -p ;}
# Usage: Open a console to a remote heroku host.
#
#
# export/mealhand (main) > console internal-staging-1
# export/mealhand (main) > console prod --size=private-l --sandbox
#
function console() { heroku run console -r $@ ;}
# Usage: Same as above, but opens a psql prompt instead. I feel like I'm the
# only person who develops directly in PSQL instead of using some app as a
# runner, but whatever :)
function pgpsql() { heroku pg:psql -r $@ ;}
# To make use of this functionality, put this file into your home directory
# as `.pryrc`. Then any console you open will have these methods immediately
# available.
# Usage: Send a string or array of strings to this method to open it in
# the TextEdit app. Nice sometimes for inspecting large chunks of data such
# as CSVs you've generated.
#
# pry(main)> str = User.last(50).to_json
# => "buncha_json_here"
#
# pry(main)> textpad(str)
#
require 'tempfile'
puts "use textpad(str) to export a string to a text app"
def textpad(str, prefix = "textpad")
str = Array(str).join("\n")
file = Tempfile.new(prefix)
file.write str
file.close
`open #{file.path}`
end
# Usage: Makes it possible to use FactoryBot in development mode:
#
# pry(main)> load_factories
# pry(main)> create(:user, :complete)
#
def load_factories
require 'factory_bot'
FactoryBot.find_definitions
include FactoryBot::Syntax::Methods
if defined? Rails
Dir[Rails.root.join("spec/support/**/*.rb")].each { |file| require file rescue NameError }
end
puts 'Loaded factories, syntax helpers, and support files if available!'
end
# Usage: There's a cool command in Pry that lets you edit a file in your
# favorite editor, and then when you close the file, all the commands will be
# executed. I use vim, but you can use e.g. atom.
#
# pry(main)> edit
#
Pry.config.editor = "vim"
# Usage: I hardcoded my user ID because I'm tired of typing
# find_by(email: "whatever") in development consoles.
def me
User.find(5975067)
end
# Usage: If you want to see how many queries your code executes (for instance,
# for work doing optimization), this command lets you keep track of those
# queries over time:
#
# pry(main)> ar_counts
# Current count is 0, last count was 0
# 0 queries since last count
#
# pry(main)> User.last
# => #<User id: 13052821 ..>
#
# pry(main)> ar_counts
# Current count is 12, last count was 0
# 12 queries since last count
#
def ar_counts(force: false)
$ar_counter = nil if force
$ar_counter ||= ArCounter.new
$ar_counter.report
end
class ArCounter # necessary for the above
def initialize
@last_count = 0
@current_count = 0
subscribe
end
def subscribe
puts "Subscribing to active record counts"
ActiveSupport::Notifications.subscribe("sql.active_record") do
@current_count += 1
end
end
def report
diff = @current_count - @last_count
puts "Current count is #{@current_count}, last count was #{@last_count}"
puts "#{diff} queries since last count"
@last_count = @current_count
diff
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment