Skip to content

Instantly share code, notes, and snippets.

View majedbojan's full-sized avatar
We turn coffee into code

MaJeD BoJaN majedbojan

We turn coffee into code
View GitHub Profile
@majedbojan
majedbojan / kill-server-process.sh
Created April 5, 2022 21:03
Solving `Address already in use - bind(2) for 127.0.0.1:4000 (Errno::EADDRINUSE)` issue
# When you didn't shutdown the server wisely,
# you will encounter error that tells you the certain port is in use
# specially when running rails server
kill -9 $(lsof -i tcp:4000 -t)
# Note:
Change the `4000` upon on the port you are running on it
@majedbojan
majedbojan / authentication_helper.rb
Last active November 1, 2021 11:00
Add user Authentication to Rspec request
# frozen_string_literal: true
# Add `AuthenticationHelper` in spec/support/authentication_helper.rb
module AuthenticationHelper
def generate_token(user)
payload = { id: user.id.to_s }
JWT.encode payload, Rails.application.credentials.secret_key_base, 'HS256'
end
def sign_in(user)

We used the official Capistrano recipe for Puma. capistrano-puma. It has monit scripts built-in to monitor puma server processes

OS level


Install monit

sudo apt-get update
# Create a proxy server that forwards requests to thirdparty
# 1) Create a file in `/etc/nginx/sites-available` and name it any name
# 2) Copy the script replace `server_name` with your server domain and `proxy_pass` with third party domain.
server {
listen 80;
server_name YourDomain.com;
location / {
proxy_pass https://ThirdPartyDomain.com;
#proxy_ssl_certificate /etc/nginx/ssl;
@majedbojan
majedbojan / RubyOnRailsSetup.sh
Last active February 8, 2021 07:46
Install Ruby On Rails on ubuntu & macOS
# This document will help you setting up a Ruby on Rails development environment on Ubuntu and macOS.
# ---------------------------------------- Ubuntu ------------------------------------------------------ #
# To make sure we have everything necessary for Webpacker support in Rails,
# we're first going to start by adding the Node.js and Yarn repositories to our system before installing them.
- sudo apt-get update
- sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn
# Next we're going to be installing Ruby using one of RVM.
@majedbojan
majedbojan / zsh_corrupt_history.sh
Created September 15, 2020 06:54
Occasionally you may find you have a corrupt zsh history file preventing you from using the `fc` command or searching the history. Here's how to fix it.
1) cd ~
2) mv .zsh_history .zsh_history_bad
3) strings .zsh_history_bad > .zsh_history
4) fc -R .zsh_history # reload history
module Attachable
extend ActiveSupport::Concern
included do
## -------------------- Associations -------------------- ##
if const_defined?('STORAGE_TYPES')
self::STORAGE_TYPES.each do |k, v|
v == :one ? has_one_attached(k.to_sym) : has_many_attached(k.to_sym)
end
end
@majedbojan
majedbojan / sample_encryption_decryption_with_aes_for_ruby.rb
Last active June 10, 2020 09:30
sample encryption and decryption data with Advanced Encryption Standard (AES) for ruby
# Encrypts and Decrypts block of data given an encryption key and an
# initialization vector (iv). Keys, iv's, and the data returned
# are all binary strings. Cipher algorithm should be array [256, CBC],
# or any of the cipher types supported by OpenSSL otherwise default will be used.
# Pass nil if the encryption type doesn't use iv's (like ECB) or ignore it if you using.
# USAGE:
# encrypt_to_hex_format(
# 'ThisPasswordIsReallyHardToGuess', 'initializervector', 'Hey! please encrypt me'
# )
@majedbojan
majedbojan / _alerts.html.erb
Created February 24, 2020 14:21 — forked from ali-sheiba/_alerts.html.erb
Setup Rails 6 with bootstrap
# app/views/shared/_alerts.html.erb
<% flash.each do |key, value| %>
<div class="alert alert-<%= alert_style(key) %>"><%= value %></div>
<% end %>