Skip to content

Instantly share code, notes, and snippets.

View ghiculescu's full-sized avatar

Alex Ghiculescu ghiculescu

View GitHub Profile
# i wish i did this in JS
require 'sinatra'
require 'fileutils'
require 'json'
require 'date'
require 'time'
require 'byebug'
get "/devices" do
@ghiculescu
ghiculescu / translation_helper.rb
Created June 3, 2017 03:49
Monkey patch ActionView's TranslationHelper to add a custom prefix for keys accessed via lazy lookup (http://guides.rubyonrails.org/i18n.html#looking-up-translations). Use this if you don't want a top level i18n key for each controller in your app - instead all keys sit under a "views" parent (and presumably you'd have other top level keys for o…
# config/initializers/translation_helper.rb
module ActionView
# = Action View Translation Helpers
module Helpers
module TranslationHelper
private
def scope_key_by_partial_with_views_prefix(key)
if key.is_a?(String) && key.first == "."
key = scope_key_by_partial_without_views_prefix(key)
"views.#{key}"
translations:
- file: "app/assets/javascripts/i18nautogen/i18n-%{locale}.js"
only: ['*.js']
@ghiculescu
ghiculescu / tooltipable.rb
Created June 3, 2017 03:55
If you say `placeholder: true` on a form element, Rails gets it from i18n. Do the same for `tooltip: true`
# based on https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/tags/placeholderable.rb
# config/initializers/tooltipable.rb
module ActionView
module Helpers
module Tags # :nodoc:
module Tooltipable # :nodoc:
def initialize(*)
super
@ghiculescu
ghiculescu / Gemfile
Last active November 1, 2017 23:32
SMS you a reminder to shave when you clock in to work
source 'https://rubygems.org'
gem 'sinatra'
gem 'aws-sdk', '~> 3'
gem 'httparty'
gem 'twilio-ruby'
@ghiculescu
ghiculescu / translatable.rb
Created March 23, 2018 04:03
moment.js locale whitelist
def moment_js_locale
# to generate this list, go to https://cdnjs.com/libraries/moment.js
# and run this in the console:
# $('.library-url').map((index, cell) => "'" + $(cell).text().replace("https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/locale/", "").replace(".js", "") + "'").get().join(", ")
valid_moment_js_locales = Set.new(['af', 'ar-dz', 'ar-kw', 'ar-ly', 'ar-ma', 'ar-sa', 'ar-tn', 'ar', 'az', 'be', 'bg', 'bm', 'bn', 'bo', 'br', 'bs', 'ca', 'cs', 'cv', 'cy', 'da', 'de-at', 'de-ch', 'de', 'dv', 'el', 'en-au', 'en-ca', 'en-gb', 'en-ie', 'en-il', 'en-nz', 'eo', 'es-do', 'es-us', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr-ca', 'fr-ch', 'fr', 'fy', 'gd', 'gl', 'gom-latn', 'gu', 'he', 'hi', 'hr', 'hu', 'hy-am', 'id', 'is', 'it', 'ja', 'jv', 'ka', 'kk', 'km', 'kn', 'ko', 'ky', 'lb', 'lo', 'lt', 'lv', 'me', 'mi', 'mk', 'ml', 'mr', 'ms-my', 'ms', 'mt', 'my', 'nb', 'ne', 'nl-be', 'nl', 'nn', 'pa-in', 'pl', 'pt-br', 'pt', 'ro', 'ru', 'sd', 'se', 'si', 'sk', 'sl', 'sq', 'sr-cyrl', 'sr', 'ss'
require 'English'
namespace :db do # rubocop:disable Metrics/BlockLength
task :prevent_disparate_pg_dump_versions => :environment do
allowed_pg_dump_version = '9.6.10'.freeze
`pg_dump --version`
pg_dump_exit_status = $CHILD_STATUS.exitstatus
locally_installed_version = `pg_dump --version`.chomp[/(\d.*)/,1] rescue nil
if rand > 0.5
"do this"
else
"do that"
end
@ghiculescu
ghiculescu / active_record_5_adapter_with_default_scopes_spec.rb
Last active September 3, 2019 17:18
How to replicate https://github.com/CanCanCommunity/cancancan/pull/600 - switch to master, add this test file, then run the test command below. It should fail for you too!
# frozen_string_literal: true
require 'spec_helper'
if CanCan::ModelAdapters::ActiveRecordAdapter.version_greater_or_equal?('5.0.0')
describe CanCan::ModelAdapters::ActiveRecord5Adapter do
context 'with postgresql' do
before :each do
connect_db
ActiveRecord::Migration.verbose = false

srb rbi suggest-typed internally runs srb tc --typed=strict --error-white-list=7022 --suggest-typed -a

--error-white-list means "only output errors of this kind", and 7022 is the error that is used to suggest a type. therefore, this runs sorbet at typed: strict across the board, and then outputs errors with type suggestions where appropriate.

THE PROBLEM is that when we run this on our (big) codebase, a bunch of files get downgraded to typed: ignore. but if we run srb tc on the same codebase, with higher sigils (eg. typed: true) on those files, there's no errors.

the type sorbet suggests is one level lower than the file's current min error level: https://github.com/sorbet/sorbet/blob/c33b7e2e41657b482a631e2edb931258995406bc/main/realmain.cc#L525

errors have a minLevel that comes from their what, which is an ErrorClass: https://github.com/sorbet/sorbet/blob/09fc62528d3b25357e297b594e699ab096d8b12d/core/Error.h#L132.