Skip to content

Instantly share code, notes, and snippets.

View bassemawhoob's full-sized avatar

Bassem Mawhoob bassemawhoob

View GitHub Profile
@bassemawhoob
bassemawhoob / chewy_raw.rb
Created October 28, 2024 13:48
Import raw hash data directly into Chewy without ActiveRecord
import_payload = [
{
index: {
_id: id,
data: {
# ...
}
}
}
]
@bassemawhoob
bassemawhoob / create_or_update.rb
Last active July 19, 2024 13:42
An ActiveRecord extension to update a record if found, otherwise create it
# frozen_string_literal: true
module ActiveRecord
class Base
# Create or update a record by field (or fields). This will look for each field name as a key in the
# options hash.
# @example
# # If a user with that email exists then update his name and birthdate, else create him
# attributes={name: "Bassem Mawhoob", email: "[email protected]", :birthdate=>'1980/01/01'}
# User.create_or_update_by(:email, attributes)
@bassemawhoob
bassemawhoob / pg_failed_sql_transaction.rb
Created July 5, 2024 12:19
Debug PG::InFailedSqlTransaction
# Source: https://stackoverflow.com/questions/73718680/debug-pginfailedsqltransaction
puts ActiveRecord::Base.connection.raw_connection.error_message
@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