Skip to content

Instantly share code, notes, and snippets.

Avatar

Weston Ganger westonganger

View GitHub Profile
View sqlite_optimizations.rb
conn = ActiveRecord::Base.connection
if conn.adapter_name.downcase == "sqlite"
### Write-Ahead Logging - https://sqlite.org/wal.html
### - Pros: Better Concurrency, Faster
### - Cons: All DB access must be from the same host
### TODO: Future Improvements (2019/2020)
### WAL2 & 'BEGIN CONCURRENT'
### https://stackoverflow.com/a/54475279/3068360
@westonganger
westonganger / generate_params_query_string.js
Last active December 10, 2021 19:35
Javascript JSON to Params Query String Generator
View generate_params_query_string.js
window.generate_params_query_string = function(json){
if(typeof json == 'string'){
json = JSON.parse(json);
}
var params_str_array = [];
var parse_plain_value = function(current_obj, current_prefix){
var strParam = current_prefix + "=" + encodeURIComponent(current_obj);
params_str_array.push(strParam);
@westonganger
westonganger / base_form.rb
Created November 4, 2021 19:29
Simple Rails form objects
View base_form.rb
class Forms::BaseForm
include ActiveModel::Validations
def initialize(attrs={})
attrs ||= {}
attrs.each do |k,v|
self.send("#{k}=", v) ### Use send so that it checks that attr_accessor has already defined the method so its a valid attribute
end
end
View yaml_translation_tasks.rb
### USE YAML BACKEND FOR THIS SCRIPT
I18n.backend = I18n::Backend::Simple.new
namespace :translations do
task yaml_export: :environment do
base_path = Rails.root.join("config/locales/export/")
FileUtils.mkdir_p(base_path)
@westonganger
westonganger / i18n_custom_backend.rb
Last active May 3, 2022 13:03
Good Rails I18n Backend Setup
View i18n_custom_backend.rb
module I18n
class CustomExceptionHandler < ExceptionHandler
def call(exception, locale, key, options)
if exception.is_a?(MissingTranslation) && key.to_s != 'i18n.plural.rule'
raise exception.to_exception
else
super
end
end
end
View i18n_side_by_side.rb
### 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
@westonganger
westonganger / ruby_hash_deep_set.rb
Last active October 25, 2021 20:16
Ruby Hash deep_set
View ruby_hash_deep_set.rb
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)
@westonganger
westonganger / auto_iframe_height.js
Created September 28, 2021 23:33
Auto IFRAME Height
View auto_iframe_height.js
// 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 / stripe_checkout_requirements.md
Last active November 24, 2021 22:33
Stripe Checkout Requirements (2021)
View stripe_checkout_requirements.md
@westonganger
westonganger / get_html_snapshot_jquery.js
Last active September 29, 2021 17:06
Get HTML Snapshot of Current Page with Javascript
View get_html_snapshot_jquery.js
/* 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(){