Skip to content

Instantly share code, notes, and snippets.

Alvin Liang aliang

  • Burlingame, CA
View GitHub Profile
View virtus-multiparameter_attributes.rb
# Rails datetime_select and similar use multiparameter attributes which are
# these dawful things from the bowels of activerecord and actionpack. This
# module extends virtus models to coerce multiparameter attributes back together
# before assigning attributes.
#
# Here's the implementation for ActiveRecord:
#
# https://github.com/rails/rails/blob/11fd052aa815ae0255ea5b2463e88138fb3fec61/activerecord/lib/active_record/attribute_assignment.rb#L113-L218
#
# and DataMapper:
@aliang
aliang / shared_examples_for_active_model.rb
Created Jul 7, 2016
ActiveModel::Lint tests for MiniTest 5
View shared_examples_for_active_model.rb
shared_examples_for "ActiveModel" do
require 'minitest/assertions'
require 'active_model/lint'
include MiniTest::Assertions
include ActiveModel::Lint::Tests
# This is the main difference between MiniTest 4 and 5.
# We must keep a counter of assertions.
attr_accessor :assertions
View sequence_counter_ar_fix.rb
models = ActiveRecord::Base.descendants
models.each do |m|
last_id = m.last.id
m.connection.execute("ALTER SEQUENCE #{m.table_name}_id_seq RESTART WITH #{last_id}")";
end
@aliang
aliang / vanity_phone_number_converter.rb
Created Sep 2, 2014
Vanity phone number converter
View vanity_phone_number_converter.rb
# Converts a phone number with letters to a phone number with only numbers.
# Leaves any other characters untouched.
class VanityPhoneNumberConverter
LETTER_TO_NUMBER = {
'a' => '2', 'b' => '2', 'c' => '2',
'd' => '3', 'e' => '3', 'f' => '3',
'g' => '4', 'h' => '4', 'i' => '4',
'j' => '5', 'k' => '5', 'l' => '5',
'm' => '6', 'n' => '6', 'o' => '6',
'p' => '7', 'q' => '7', 'r' => '7', 's' => '7',
@aliang
aliang / disable-leaflet-map-events.js
Created Jun 4, 2014
Disable all (or almost all!) leaflet map events
View disable-leaflet-map-events.js
// Assumes your L.Map is called "map". Untested!
map.dragging.disable();
map.touchZoom.disable();
map.doubleClickZoom.disable();
map.scrollWheelZoom.disable();
map.boxZoom.disable();
map.keyboard.disable();
if (map.tap) {
map.tap.disable();
}
@aliang
aliang / io_ext.rb
Created May 7, 2014
Select random line from IO object
View io_ext.rb
# See http://stackoverflow.com/questions/11007111/ruby-whats-an-elegant-way-to-pick-a-random-line-from-a-text-file
class IO
# Selects a random "line" from a file (might not be a line if different separator is passed).
# @param *args Same arguments as IO.foreach
# @return A random line from the IO object
def self.random_line(*args)
chosen_line = nil
self.foreach(*args).each_with_index do |line, number|
chosen_line = line if rand < 1.0/(number+1)
end
@aliang
aliang / mandrill_signature_verifier.rb
Last active Dec 17, 2019
Mandrill webhook verifier, in Ruby
View mandrill_signature_verifier.rb
class MandrillSignatureVerifier
def initialize(key, url, params, signature)
@key = key
@url = url
@params = params
@signature = signature
end
# Return true if the signature matches
def verified?
@aliang
aliang / country_codes.js
Created Apr 17, 2014
Really stupid iso-3166 country code converter. As stupid as possible!
View country_codes.js
var CountryCodes = (function() {
// Codes taken from Wikipedia as of Wed Apr 16 2014
var codes = {"AFG": "AF", "ALA": "AX", "ALB": "AL", "DZA": "DZ", "ASM": "AS", "AND": "AD", "AGO": "AO", "AIA": "AI", "ATA": "AQ", "ATG": "AG", "ARG": "AR", "ARM": "AM", "ABW": "AW", "AUS": "AU", "AUT": "AT", "AZE": "AZ", "BHS": "BS", "BHR": "BH", "BGD": "BD", "BRB": "BB", "BLR": "BY", "BEL": "BE", "BLZ": "BZ", "BEN": "BJ", "BMU": "BM", "BTN": "BT", "BOL": "BO", "BES": "BQ", "BIH": "BA", "BWA": "BW", "BVT": "BV", "BRA": "BR", "IOT": "IO", "BRN": "BN", "BGR": "BG", "BFA": "BF", "BDI": "BI", "KHM": "KH", "CMR": "CM", "CAN": "CA", "CPV": "CV", "CYM": "KY", "CAF": "CF", "TCD": "TD", "CHL": "CL", "CHN": "CN", "CXR": "CX", "CCK": "CC", "COL": "CO", "COM": "KM", "COG": "CG", "COD": "CD", "COK": "CK", "CRI": "CR", "CIV": "CI", "HRV": "HR", "CUB": "CU", "CUW": "CW", "CYP": "CY", "CZE": "CZ", "DNK": "DK", "DJI": "DJ", "DMA": "DM", "DOM": "DO", "ECU": "EC", "EGY": "EG", "SLV": "SV", "GNQ": "GQ", "ERI": "ER", "EST": "EE", "ETH": "ET",
@aliang
aliang / publicize_methods.rb
Created Dec 12, 2013
Expose private methods for testing. Can accept a block, or just call in RSpec before/after blocks
View publicize_methods.rb
class Class
def publicize_methods
@_saved_private_instance_methods = self.private_instance_methods
self.class_eval { public *@_saved_private_instance_methods }
if block_given?
yield
reprivatize_methods
end
end
View user_form.html
<form>
Name: <input type="text" name="name"/>
Age: <input type="text" name="age"/>
<input type="submit" value="Submit"></input>
</form>
You can’t perform that action at this time.