Skip to content

Instantly share code, notes, and snippets.

View westonganger's full-sized avatar

Weston Ganger westonganger

View GitHub Profile
@westonganger
westonganger / ci_string.rb
Created August 31, 2021 03:53
Ruby Case-Insentive Strings
################################################## CIString (Case-Insensitive String) - HELPFUL FOR STRING COMPARISONS WHERE WE NEED TO IGNORE CASE ERRORS
class CIString < String
def initialize(*args)
if args[0].nil?
args[0] = ""
end
super
end
def ==(val)
val.is_a?(String) ? self.casecmp?(val) : false
@westonganger
westonganger / custom_i18n_exception_handler.rb
Created August 31, 2021 03:56
Rails I18n Exception Handler
### CUSTOM I18N EXCEPTION HANDLER
### 1. This exception handler enforces that an exception is raised whenever a translation is not found rather than showing "translation missing" to user
### 2. YOU CAN USE I18n.t(key, fallback: true) and it will now handle missing translations as follows:
###
### If a specific key is used ie. 'fisheries.locations.asd' and the translation is not found then it will look for:
### - 'common.asd'
###
module I18n
@westonganger
westonganger / spec_file_helpers.rb
Created August 31, 2021 03:58
Rails Test/Spec File Helpers
def create_tempfile(str_data, file_name: nil)
file_basename = file_name.to_s.split('.').first || "Example File"
file_ext = file_name.to_s.split('.').last || "csv"
### Must seperate basename and extension to array for tempfile new syntax
### https://ruby-doc.org/stdlib-3.0.2/libdoc/tempfile/rdoc/Tempfile.html#method-c-new
tmp_file = Tempfile.new([file_basename, ".#{file_ext}"], binmode: true)
tmp_file << str_data
@westonganger
westonganger / stripe_payment_intent_api.rb
Last active September 2, 2021 17:16
How to use Stripe Payment Intent API within your Rails or Ruby app
module StripePayment
### https://stripe.com/docs/payments
TEST_PUBLIC_KEY = "pk_test_foo".freeze
TEST_SECRET_KEY = "sk_test_bar".freeze
def self.create_payment_intent!(
api_secret_key:,
payment_method_id:, ### we use payment method id for this so we can track billing details
amount_in_cents:,
@westonganger
westonganger / js_error_handler.js
Last active September 29, 2021 17:45
Javascript Error Handler
// Links
// https://stackoverflow.com/questions/951791/javascript-global-event-mechanism
// https://stackoverflow.com/a/49560222/3068360
// https://stackoverflow.com/questions/5328154/catch-all-javascript-errors-and-send-them-to-server
window.addEventListener('error', function(event) {
// Instead of window.onerror we use addEventListener with capture option set to true to get the most errors
// window.onerror = function(message, file, line, col, error){
// https://stackoverflow.com/q/54209739/3068360
@westonganger
westonganger / get_html_snapshot_jquery.js
Last active September 29, 2021 17:06
Get HTML Snapshot of Current Page with Javascript
/* Note: The html string returned fron this method will not contain any actual remotely referenced JS/CSS just the HTML as in the source */
window.get_html_snapshot = function(hideItems, hideClasses){
hideItems = hideItems || false;
hideClasses = hideClasses || ".modal, .modal-backdrop";
var input_types_to_skip = ["button", "submit", "file", "image", "password"];
// First write all input[type=text] field value so they can be seen when HTML is loaded
$('input').each(function(){
@westonganger
westonganger / stripe_checkout_requirements.md
Last active November 24, 2021 22:33
Stripe Checkout Requirements (2021)
@westonganger
westonganger / auto_iframe_height.js
Created September 28, 2021 23:33
Auto IFRAME Height
// AUTOMATIC IFRAME HEIGHT
document.querySelector('iframe#my-iframe').addEventListener('load', function(){
this.style.height = this.contentWindow.document.body.offsetHeight + "px";
this.style.width = '100%';
this.style.maxWidth = '1200px';
});
@westonganger
westonganger / ruby_hash_deep_set.rb
Last active October 25, 2021 20:16
Ruby Hash deep_set
Hash.class_eval do
def deep_set(keys_array, val)
keys_array[0...-1].inject(self){|result, key|
if !result[key].is_a?(Hash)
result[key] = {}
end
result[key]
}.send(:[]=, keys_array.last, val)
### Adapted from below, custom changes marked with "CUSTOM"
### https://github.com/electric-feel/i18n-backend-side_by_side/blob/master/lib/i18n/backend/side_by_side.rb
require 'i18n'
require 'i18n/core_ext/hash'
module I18n
module Backend
using HashRefinements