Skip to content

Instantly share code, notes, and snippets.

View MaherSaif's full-sized avatar

Maher Saif MaherSaif

View GitHub Profile
@MaherSaif
MaherSaif / build_insert_query.rb
Created March 15, 2024 20:32 — forked from hopsoft/build_insert_query.rb
Get the SQL for an insert statement from ActiveRecord
# Builds an SQL insert query for a given record
#
# @param record [ActiveRecord::Base] Record used to build the SQL insert query
# @return [String] SQL insert query
def build_insert_query(record)
columns = record.class.columns.reject { |col| col.name == record.class.primary_key }
values = columns.map { |col| record[col.name] }
insert_manager = Arel::InsertManager.new
insert_manager.into(record.class.arel_table)
insert_manager.insert(columns.zip(values)).to_sql
@MaherSaif
MaherSaif / inspect_queries.rb
Created January 22, 2024 20:05 — forked from fatkodima/inspect_queries.rb
Inspect db queries
# Make sure to have prepared statements enabled.
# config/database.yml
# test:
# database: ...
# prepared_statements: true
# Add this to the top scope of test/test_helper.rb or spec/rails_helper.rb and
# run tests as usual.
queries = Hash.new(0)
@MaherSaif
MaherSaif / Dockerfile
Created September 24, 2023 07:58 — forked from robzolkos/Dockerfile
Kamal / SQLite3 issue
# syntax = docker/dockerfile:1
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.2
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
# Rails app lives here
WORKDIR /rails
# Set production environment
@MaherSaif
MaherSaif / render_flash_now_for_xhr.rb
Created May 18, 2023 19:16 — forked from goulvench/render_flash_now_for_xhr.rb
Force Rails to render flash messages for XHR responses
# To display flash messages inside XHR responses,
# place this file in app/controllers/concerns/
# then include it in ApplicationController
module RenderFlashNowForXhr
extend ActiveSupport::Concern
private
# Flash messages are not directly available for XHR requests
@MaherSaif
MaherSaif / active_link_to.rb
Created March 26, 2023 08:13 — forked from dcyoung-dev/active_link_to.rb
An `active_link_to` helper that adds the `.active` class and sets `aria-current="page"`
@MaherSaif
MaherSaif / test.rb
Created February 24, 2023 12:56 — forked from skatkov/test.rb
speed up testsuit by using unlogged tables in PG
# config/environments/test.rb
ActiveSupport.on_load(:active_record_postgresqladapter) do
# For this optimization to work, you need to recreate your test database
self.create_unlogged_tables = true
end
@MaherSaif
MaherSaif / url_validator.rb
Created February 16, 2023 09:59 — forked from virolea/url_validator.rb
Rails Custom URL validator
# Use this validator like this
#
# class User < ApplicationRecord
# validates :profile_link, url: true
# end
class UrlValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless valid_url?(value)
record.errors.add(attribute, :invalid_url)
@MaherSaif
MaherSaif / rsync-retry.sh
Created November 20, 2022 17:03 — forked from iangreenleaf/rsync-retry.sh
rsync with retries
#!/bin/bash
### ABOUT
### Runs rsync, retrying on errors up to a maximum number of tries.
### Simply edit the rsync line in the script to whatever parameters you need.
# Trap interrupts and exit instead of continuing the loop
trap "echo Exited!; exit;" SIGINT SIGTERM
MAX_RETRIES=50
@MaherSaif
MaherSaif / tmux-cheatsheet.markdown
Created November 20, 2022 17:02 — forked from MohamedAlaa/tmux-cheatsheet.markdown
tmux shortcuts & cheatsheet

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@MaherSaif
MaherSaif / timeline.php
Created November 20, 2022 17:02 — forked from codemasher/timeline.php
Fetch your twitter timeline via the unofficial adaptive search API
<?php
/**
* Twitter timeline backup
*
* Required:
* - PHP 8.1+
* - cURL extension enabled
* - Windows:
* - download https://windows.php.net/downloads/releases/php-8.1.12-Win32-vs16-x64.zip (or a newer available version)
* - unzip to a folder of your choice