Skip to content

Instantly share code, notes, and snippets.

@anujbiyani
anujbiyani / ensure_stable_schema_migrations.rb
Last active April 16, 2024 23:04
A script to make sure new Rails migrations cleanly migrate forwards and backwards.
#!/usr/bin/env ruby
require "pathname"
common_ancestor_sha = `git merge-base HEAD origin/master`.strip
commit_messages = `git log --pretty=format:"%B" #{common_ancestor_sha}..HEAD`
if commit_messages.include?("[skip schema checks]")
puts "Found [skip schema checks] tag, skipping db migration check."
exit
@anujbiyani
anujbiyani / pre-commit
Last active June 18, 2020 18:01
A pre-commit hook to run Rubocop. Adapted from another gist (see comment on line 3), but updated to run autocorrect for you and commit the changes.
#!/usr/bin/env ruby
# adapted from https://gist.github.com/stevenharman/f5fece7fd39356e57ce3c710911f714f
RED = "\033[0;31m"
GREEN = "\033[0;32m"
NO_COLOR = "\033[0m"
if `command -v bundle exec rubocop` == ""
puts "💀#{RED} Install Rubocop and be sure it is available on your PATH#{NO_COLOR}"
@anujbiyani
anujbiyani / generate_cop.rake
Created November 11, 2019 23:12
Generate scaffold for new cop
desc "Create a RuboCop rule using a template. You can use snake_case or CamelCase for the cop name; either way it will be converted to CamelCase."
task :generate_cop, [:name] do |_t, args|
require "active_support/inflector/methods"
camel_case_name = ActiveSupport::Inflector.camelize(args[:name], true)
snake_case_name = ActiveSupport::Inflector.underscore(camel_case_name)
cop_path = "lib/rubocop/cop/<company name>/#{snake_case_name}.rb"
spec_path = "spec/rubocop/cop/<company name>/#{snake_case_name}_spec.rb"
raise ArgumentError, "Cannot create cop -- file <#{cop_path}> already exists." if File.exist?(cop_path)
raise ArgumentError, "Cannot create spec -- file <#{spec_path}> already exists." if File.exist?(spec_path)
@anujbiyani
anujbiyani / check_cops_documentation_generated.sh
Created November 11, 2019 23:08
Ensure cops documentation has been committed
set -e
bundle exec rake generate_cops_documentation
if [ -n "$(git status --porcelain)" ]; then
echo "rake generate_cops_documentation resulted in a dirty tree; you probably need to generate and check in documentation";
git status
git --no-pager diff
exit 1
fi
@anujbiyani
anujbiyani / hie-solution.rb
Created September 24, 2019 17:55
Tricking PaperTrail into saving attributes that didn't change - solution
patient_preferences = create(
:patient_preferences,
health_information_exchange_opt_in: true,
health_information_exchange_legal_text_version: 1
)
patient_preferences.reload
# Add the following two lines
patient_preferences.health_information_exchange_opt_in_will_change!
@anujbiyani
anujbiyani / hie-problem.rb
Last active September 24, 2019 18:02
Tricking PaperTrail into saving attributes that didn't change - problem
patient_preferences = create(
:patient_preferences,
health_information_exchange_opt_in: true,
health_information_exchange_legal_text_version: 1
)
patient_preferences.health_information_exchange_opt_in = true
patient_preferences.health_information_exchange_legal_text_version = 2
puts patient_preferences.changes
@anujbiyani
anujbiyani / .bash_profile
Created September 21, 2018 00:32
Add this to your ~/.bash_profile to get the git branch (and some coloring for your prompt)
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
## ANSI color codes
RS="\[\033[0m\]" # reset
HC="\[\033[1m\]" # hicolor
UL="\[\033[4m\]" # underline
INV="\[\033[7m\]" # inverse background and foreground
FBLK="\[\033[30m\]" # foreground black
FRED="\[\033[31m\]" # foreground red
@anujbiyani
anujbiyani / gist:2b3f5ee0ff59fa1f852f74d9031968b7
Last active March 5, 2017 18:45
Clearing out WiFi networks on Android
adb shell
su
rm /data/misc/wifi/wpa_supplicant.conf
@anujbiyani
anujbiyani / fc_academy.rb
Created February 28, 2017 10:28
Code for re-arranging and visualizing students and teachers and their preferences
require 'csv'
require 'json'
STUDENTS_CSV_PATH = 'students.csv'
TEACHERS_CSV_PATH = 'teachers.csv'
EMAIL_KEY = 'Email address'
LANGUAGE_KEY = 'What would you like to learn?'
EXPERIENCE_KEY = 'How experienced are you?'
TEACH_KEY = 'What do you want to teach?'
@anujbiyani
anujbiyani / feedback_presentation.html
Last active November 30, 2016 16:40
Feedback Presentation (uses Remark)
<!DOCTYPE html>
<html>
<head>
<title>Feedback</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);