Skip to content

Instantly share code, notes, and snippets.

View westonganger's full-sized avatar

Weston Ganger westonganger

View GitHub Profile
@westonganger
westonganger / Adjust Rails Date or Time Formatting Defaults
Created March 26, 2023 03:47
Adjust Rails Date or Time Formatting Defaults
DATE_FORMAT = "%F".freeze ### Rails default is "%m/%d/%Y"
TIME_FORMAT = "#{DATE_FORMAT} %l:%M%p %Z".freeze
format_defaults = {
date_time12: TIME_FORMAT,
date_time24: "#{DATE_FORMAT} %H:%M %Z".freeze,
}.freeze
Date::DATE_FORMATS.merge!(format_defaults.merge({default: DATE_FORMAT}))
Time::DATE_FORMATS.merge!(format_defaults.merge({default: TIME_FORMAT}))
@westonganger
westonganger / Fix Arel.sql deprecation warnings,errors in Rails 5.2,6+
Created March 26, 2023 03:38
Fix Arel.sql deprecation warnings & errors in Rails 5.2/6+
### config/initializers/arel_sql_monkey_patch.rb
ActiveRecord::Relation.class_eval do
### FIX ERRORS FOR Arel.sql in Rails 6+
### FIX DEPRECATION WARNINGS FOR Arel.sql in Rails =5.2
if Rails::VERSION::STRING.to_f >= 5.2
def pluck(*args)
if args.any?{|x| x.is_a?(String) }
new_args = args.map{|x| x.is_a?(String) ? Arel.sql(x) : x }
@westonganger
westonganger / Puma Rails Config
Created March 26, 2023 03:25
Puma Rails Config
app_path = File.expand_path(File.dirname(__FILE__) + '/..')
log_tmp_path = File.expand_path(app_path + '/..')
#directory '/home/deploy/my_app'
### TO VIEW PUMA LOGS USE, `journalctl -u puma`
### TO SEND PUMA LOGS TO FILE SEE BELOW
#stdout_redirect '/home/deploy/tmp/puma-stdout.log', '/home/deploy/tmp/puma-stderr.log', true
# Puma can serve each request in a thread from an internal thread pool.
@westonganger
westonganger / Rails Spec Helpers
Last active March 26, 2023 03:23
Rails Spec Helpers
def sample_pdf_upload
sample_path = Rails.root.join("spec/fixtures/sample_file.pdf")
#fixture_file_upload(sample_path, 'application/pdf') ### MINITEST
Rack::Test::UploadedFile.new(sample_path) ### RSPEC
end
def sample_csv_upload
sample_path = Rails.root.join("spec/fixtures/sample_file.csv")
#fixture_file_upload(sample_path, 'text/csv') ### MINITEST
Rack::Test::UploadedFile.new(sample_path) ### RSPEC
@westonganger
westonganger / Capybara config
Created March 26, 2023 03:19
Capybara config
require 'capybara/rails'
require 'capybara/rspec'
require 'rack/handler/puma'
require 'selenium/webdriver'
# Default cache time of 24 hours before webdrivers checks for new versions
Webdrivers.cache_time = 24.hours.to_i
# Logging levels are :info (more verbosity), :warn (default), :error, etc
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
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 / Simple Rails Form Object
Last active October 11, 2023 18:23
Simple Rails Form Objects
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
### 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
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