Skip to content

Instantly share code, notes, and snippets.

View mdchaney's full-sized avatar

Michael Chaney mdchaney

View GitHub Profile
@mdchaney
mdchaney / _form.html.erb.tt
Created July 11, 2024 17:26
Bootstrap-styled view generators for Ruby on Rails
<%%= form_for([<%= model_resource_name %>]) do |form| %>
<%%= show_errors_for form.object %>
<% attributes.each do |attribute| -%>
<div class="mb-3">
<% if attribute.password_digest? -%>
<%%= form.label :password, class: "form-label" %>
<%%= form.password_field :password, class: "form-control", required: form.object.new_record? %>
</div>
@mdchaney
mdchaney / fix_encoding.rb
Created June 25, 2024 16:09
fix_encoding_2
module FixEncoding
def FixEncoding.remove_bom(str)
if str.byteslice(0..2) == "\xEF\xBB\xBF".b
return str.byteslice(3..-1)
else
return str
end
end
def FixEncoding.has_utf8?(str)
@mdchaney
mdchaney / fix_encoding.rb
Last active June 25, 2024 04:16
Fix encoding of dubious string in Ruby
module FixEncoding
def FixEncoding.fix_encoding(str)
# The "b" method returns a copied string with encoding ASCII-8BIT
str = str.b
# Strip UTF-8 BOM if it's at start of file
if str =~ /\A\xEF\xBB\xBF/n
str = str.gsub(/\A\xEF\xBB\xBF/n, '')
end
if str =~ /([\xc0-\xff][\x80-\xbf]{1,3})+/n
# String has actual UTF-8 characters
@mdchaney
mdchaney / name_split.rb
Created March 5, 2024 19:21
Split name into first/last name
# words that indicate last name
#
# da
# de
# de la
# del
# di
# dos
# la
# van der
@mdchaney
mdchaney / audio_time.rb
Last active January 10, 2024 00:13
Original audio_time.rb
module AudioTime
# AIFF stores the samples per second as an IEEE 80-bit float, big-endian.
# This code will convert it to a 32-bit integer. This is not generalized
# code for this operation as it ignores the sign bit, part of the mantissa,
# etc.
def AudioTime.ieee80_to_long(raw_number)
discard, mantissa, hidden, exponent, sign = raw_number.reverse.unpack('b80').first.unpack('a32 a31 a1 a15 a1')
# Get the real exponent - the exponent is really exponent + 1
exponent = [exponent].pack('b15').unpack('v').first - 16382
# Now, use the exponent to pull the proper bits from the mantissa.
@mdchaney
mdchaney / _line_item.html.erb
Created September 28, 2023 04:02
Stimulus-based subform
<!--
Note that when showing this line if the object is marked_for_destruction? it
should be hidden.
-->
<tr class="line_item subform fields" id="line_item_<%= f.object.id %>">
<td>
<%= f.text_field :field_1, required: true %>
</td>
<td>
<%= f.text_field :field_2, required: true %>
@mdchaney
mdchaney / card_utilities.js
Created July 28, 2022 02:31
Modernization of Stripe's jquery.payment - almost drop-in replacement with no jQuery
// Based on Stripe's jquery.payment, offers a simple set of utilities
// for handling credit card numbers, expirations, and CVC/CVV2 codes
//
// cc_format_card_number(field)
// cc_format_card_expiry(field)
// cc_format_card_cvc(field)
//
// "field" may be either the actual DOM element reference or a string
// with the ID of the DOM element. This will add event handlers to
// the field to handle input.
@mdchaney
mdchaney / README
Last active February 6, 2024 22:20
Fix string encoding in Ruby
This function takes a string that is in Latin-1 (ISO-8859-1), UTF-8, or plain ASCII and returns the string properly encoded as UTF-8. In addition, any Microsoft smart quotes (stupid quotes) are replaced with plain ASCII equivalents. This is useful for reading CSV files or other text files from a web upload and getting to a "known good" state.
@mdchaney
mdchaney / README
Last active April 10, 2020 16:18
Bootstrap 4 multi-select replacement
This code will replace <select multiple...> boxes with a readonly text input that shows all items selected. When clicked, a modal will appear giving the user the ability to select items with check boxes. In the page load function, just call setup_selection_popups(). The select elements should have a class of "replaceable", and of course the multiple attribute must be present. Everything is contained in one javascript file, and a css file. Bootstrap 4 uses jquery, so it must be present. However, jquery is used only to show the modal.
@mdchaney
mdchaney / recursive_delete.rb
Created October 1, 2018 13:08
Recursively remove data from tables in PGSQL, considering foreign keys
#!/usr/bin/env ruby
@verbose = true
tables_to_clear = ARGV.dup.freeze
# Alternately, clear out everything but these tables
#UNCLEARED_TABLES=%w{ ar_internal_metadata schema_migrations }
#tables_to_clear = (ActiveRecord::Base.connection.tables.sort - UNCLEARED_TABLES).freeze