Skip to content

Instantly share code, notes, and snippets.

View bassemawhoob's full-sized avatar

Bassem Mawhoob bassemawhoob

View GitHub Profile
@bassemawhoob
bassemawhoob / jsonb_nested_column.sql
Created January 17, 2024 14:12
Set a dynamically nested jsonb key by key type
UPDATE visual_settings
SET calendar = jsonb_set(cast(calendar as jsonb), '$.**.firstDay', '"new_value"', false)
WHERE json_typeof(jsonb_path_query_first(calendar, '$.**.firstDay')::json) = 'string'
@bassemawhoob
bassemawhoob / html_to_plain_text.rb
Created January 8, 2024 10:04
Convert HTML to Plain Text
# Excerpt from https://github.com/alexdunae/premailer/blob/master/lib/premailer/html_to_plain_text.rb
# Initially authored by Premailer
# coding: utf-8
require 'htmlentities'
module HtmlToPlainText
# Returns the text in UTF-8 format with all HTML tags removed
#
@bassemawhoob
bassemawhoob / factory_bot_console.rb
Created August 23, 2023 10:59
Using FactoryBot in Rails console
# Source: https://stackoverflow.com/questions/18195851/how-do-i-use-factories-from-factorybot-in-rails-console
require 'factory_bot'
# P.S. For fabrication gem you can load the definitions in the rails console with:
# Fabrication.manager.load_definitions
FactoryBot.find_definitions
include FactoryBot::Syntax::Methods
@bassemawhoob
bassemawhoob / factory_bot_conditional_traits.md
Last active August 23, 2023 11:00
FactoryBot - Conditional Traits
traits = [(:with_shipment if condition?)].compact
create(:advice, *traits)

Or inline:

create(:advice, *[(:with_shipment if condition?)].compact)
@bassemawhoob
bassemawhoob / linkedin_finder.js
Created December 1, 2022 10:50
Find Linkedin URLs with Google Sheets
// Source: https://www.lemlist.com/ghseet-hack?mtm_campaign=5020&mtm_source=organic&mtm_medium=linkedin
/**
* Find a Linkedin profile from company name and job title
*
* @param {string} companyName company where your prospect is working
* @param {string} jobTitle job you are targeting
* @return if found the linkedinURL + Name of the prospect
* @customfunction
*/
@bassemawhoob
bassemawhoob / s3_local.rb
Created November 17, 2022 10:55
Production S3 to local ActiveStorage on Rails
# Reference: https://dev.to/lucaskuhn/syncing-files-from-aws-s3-to-local-storage-on-rails-3639
# Main goal: Sync a specific model's S3 images locally to have productions images working in development
# Assuming I have a model called `ProductImage` with an attachment called `artwork`
s3_bucket = "YOUR_S3_BUCKET"
access_key_id = Rails.application.credentials.dig(:aws, :access_key_id)
secret_access_key = Rails.application.credentials.dig(:aws, :secret_access_key)
storage_folder = Rails.root.join('storage')
storage_folder.mkpath
@bassemawhoob
bassemawhoob / median.sql
Last active May 1, 2023 08:42
PostgreSQL Median Function
-- Source: https://wiki.postgresql.org/wiki/Aggregate_Median
CREATE OR REPLACE FUNCTION _final_median(numeric[])
RETURNS numeric AS
$$
SELECT AVG(val)
FROM (
SELECT val
FROM unnest($1) val
ORDER BY 1
@bassemawhoob
bassemawhoob / cohort.sql
Last active April 12, 2024 18:15
Revenue Retention Cohort Analysis - PostgreSQL
-- Based on: https://medium.com/quick-code/how-to-write-sql-to-calculate-user-cohort-retention-a1b3b57c7a2f
-- Uses DATE_PART instead of DATE_DIFF as it is not supported by PostgreSQL
-- (branch_id, scheduled_for, total_cents)
WITH activities AS (
SELECT branch_id, scheduled_for, total_cents
FROM orders
WHERE orders.status = 'complete'
),
-- (branch_id, cohort_month): cohort month is the first order date