Skip to content

Instantly share code, notes, and snippets.

View timm-oh's full-sized avatar
🐰

Tim McCarthy timm-oh

🐰
View GitHub Profile
@timm-oh
timm-oh / application_helper.rb
Last active December 17, 2023 21:04
Rails 5.2.3: Cache Active Storage Blobs and Variants through Cloudfront
# app/helpers/application_helper.rb
module ApplicationHelper
# active_storage_item could be a blob or variant object
def proxy_url(active_storage_item, options = {})
options.merge!(host: ENV['ASSETS_HOST']) if ENV['ASSETS_HOST'].present?
# proxy: 'true' allows you to stil have the original functionality while
# being able to proxy through a CDN. You've got to ensure that your CDN
# forwards this param otherwise active storage will always do the default
# behavior which is a redirect to the service.
@timm-oh
timm-oh / application_record.rb
Last active May 19, 2024 11:08
Rails 5.2.3: Monkey patch update_all and update_columns to always include updated_at
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
# Didn't change #update_column because it uses #update_columns under the hood.
def update_columns(attrs)
new_attrs = attrs.symbolize_keys
new_attrs[:updated_at] ||= Time.current if self.class.column_names.include?('updated_at')
super(new_attrs)
end
@timm-oh
timm-oh / readme.md
Last active September 27, 2020 10:06
Simple time zone support

Basic Usage

This is a super easy way to ensure that timestamps/datetimes etc render correctly according to the timezone that the object is in. In our usage we have a time_zone attribute on our models that stores the time zone in a format that we can find with active support. Example American Samoa, for more examples run bundle exec rake time:zones:all in your rails project.

We weren't aiming to display the datetime in a format relative to the user, but rather just display that the time was in X time zone

# lets assume we have an attribute called start_time
@timm-oh
timm-oh / readme.md
Created September 27, 2020 10:05
Rails - Remove field_with_error

By default Rails wraps form errors in a div with the class field_with_errors.

There are 2 ways to disable this.

First way is in the application.rb environment file (doesn't make sense just to disable it on a specific environment?):

# config/application.rb

module ApplicationName
@timm-oh
timm-oh / readme.md
Last active September 28, 2020 12:50
aws regions
  • us-east-2
  • us-east-1
  • us-west-1
  • us-west-2
  • ap-east-1
  • ap-south-1
  • ap-northeast-3
  • ap-northeast-2
  • ap-southeast-1
  • ap-southeast-2
@timm-oh
timm-oh / hash_helper.rb
Created December 26, 2020 17:07
Helper method to convert from dot notation to hash
def dot_to_hash(value)
return value unless value.is_a?(Hash)
value.deep_stringify_keys.each_with_object({}) do |(k,v), result|
root, child = k.split('.')
if child
result[root] ||= {}
result[root][child] = dot_to_hash(v)
else
result[root] = dot_to_hash(v)
end
@timm-oh
timm-oh / snippet.rb
Last active January 13, 2021 04:52
Ozow integration
# refer to https://ozow.com/integrations/ for more information
# Ordering with the params matters, see the link above for more information
ozow_params = {
'SiteCode': 'SOME_SITE_CODE', # find this here https://dash.ozow.com/MerchantAdmin/Site
'CountryCode': 'ZA', # only supports ZA currently
'CurrencyCode': 'ZAR', # only supports ZAR currently
'Amount': 1000, # this is R1000, not working well for floats though
'TransactionReference': 'SOME_TEST', # your internal reference to match against
'BankReference': "Nice Reference", # the reference that the customer will see on their bank statement
@timm-oh
timm-oh / decoder.rb
Last active July 3, 2021 09:34
Peach Webhook Decrypter
class PeachPayments::Decoder
def initialize(body:, auth_tag:, iv:, key:)
@body = body
@key = key
@auth_tag = auth_tag
@iv = iv
end
def decode
packed_key, packed_iv, packed_auth_tag, packed_body = *[
@timm-oh
timm-oh / parallel
Created January 26, 2022 13:30 — forked from mjambon/parallel
bash: Run parallel commands and fail if any of them fails
#! /usr/bin/env bash
#
# Run parallel commands and fail if any of them fails.
#
set -eu
pids=()
for x in 1 2 3; do
@timm-oh
timm-oh / 500.html.erb
Created March 13, 2022 16:30
ERB in error templates without a custom controller
<!-- app/views/errors/500.html.erb -->
<!DOCTYPE html>
<html>
<head>
<title><%= "SUPER COOL TITLE" %></title>
</head>
</html>