Skip to content

Instantly share code, notes, and snippets.

View ntamvl's full-sized avatar
🏠
Working from home

Tam Nguyen ntamvl

🏠
Working from home
View GitHub Profile
@ntamvl
ntamvl / controller-concerns-in-rails-4.md
Last active May 12, 2022 14:34
Controller Concerns in Rails 4

Controller Concerns in Rails 4

If you setup a Rails 4 app, you’ll notice the app/models/concerns and app/controllers/concerns directories. Concerns are modules that can be mixed into your models and controllers to share code between them.

Some developers falsely classify mixins as composition when they are actually a form of inheritance. When you include a module in a class, that module’s methods are added to the inheritance chain just like a parent class’ methods are added to a subclass. So, don’t think you’ve solved the problem of inheritance by simply splitting your inherited code into separate files!

That being said, mixins can be a valuable tool to share code between classes that are otherwise unrelated. Here’s an example of how I chose to use it recently.

I am adding admin reporting features to an app I’m hoping to launch soon. I have an admin controller with a simple before filter to redirect if the current user is not an administrator.

class AdminController < ApplicationController
@ntamvl
ntamvl / ffmpeg-convert-edit-video-via-command-line.md
Last active January 18, 2023 03:29
FFMPEG: Convert & Edit Video via Command Line

FFMPEG: Convert & Edit Video via Command Line

FFMPEG is a free software that lets you create/edit/convert videos via command line. You can download and install FFMPEG for Linux, Windows, and Mac Operating System.

FFMPEG provides a lot of features that you can apply to an video like:

– get detailed information of the video
– record video
– convert video to audio
– convert video from one format to another
@ntamvl
ntamvl / example-use-aws-sdk-mock-to-test-aws-ssm.md
Last active February 17, 2023 15:56
Example: use aws-sdk-mock to test AWS SSM

Use aws-sdk-mock to test AWS SSM

Example:

const AWSMock = require("aws-sdk-mock");
import AWS = require("aws-sdk");
AWSMock.setSDKInstance(AWS);

import "mocha";
@ntamvl
ntamvl / create-ruby-gem-that-adds-rake-tasks.md
Last active February 16, 2024 19:13
How to create a Ruby gem that adds Rake tasks

How to create a Ruby gem that adds Rake tasks

Create a gem

One way to do this is to use bundler to scaffold our gem:

bundler gem my_gem

Add rake tasks to our gem

I prefer to put tasks meant to manage the gem itself in lib/tasks, and tasks the gem is meant to provide to gem users in lib/my_gem/tasks.

@ntamvl
ntamvl / how-to-update-add-a-file-in-the-docker-image.md
Last active February 21, 2024 11:09
How to update/add a file in the Docker Image

How to update/add a file in the Docker Image

The post discusses how to alter a standard docker image pulled from a Public repository in Docker hub as per your need. For the example of this post, we will pull a latest CentOS docker image and add a test directory test_dir and create a test file test_file into it.

Adding a directory and image in the docker image

  1. First step is to pull a latest CentOS image from docker hub.
# docker pull centos
Using default tag: latest
@ntamvl
ntamvl / remove-full-path-from-terminal.md
Last active February 21, 2024 11:09
Remove full path from terminal

Remove full path from terminal

The part before the $ in a shell is called prompt. It can be configured by changing the variable $PS1. There are is a similar question with good answeres.

The man page (see "Bash" and there "PROMPTING") says:

      \w     the  current working directory, with $HOME
             abbreviated with a tilde (uses the value of the
             PROMPT_DIRTRIM variable)
 \W the basename of the current working directory,
@ntamvl
ntamvl / docker-remove-all-images-containers.md
Last active February 21, 2024 11:09
Docker: Remove All Images and Containers

Docker: Remove All Images and Containers

Starting over in this case means wiping clean my Docker images and containers, just to make sure there are no conflicts or duplicates. The following commands delete all containers and images:

Delete every Docker containers Must be run first because images are attached to containers

docker rm -f $(docker ps -a -q)

Delete every Docker image

@ntamvl
ntamvl / [ReactJS] Detect Scrolls To Bottom.md
Last active February 21, 2024 11:10 — forked from enqtran/[ReactJS] Detect Scrolls To Bottom
[ReactJS] Detect Scrolls To Bottom
constructor(props) {
    super(props);
    this.state = {
        height: window.innerHeight,
        message: 'not at bottom'
    };
    this.handleScroll = this.handleScroll.bind(this);
}
@ntamvl
ntamvl / postgresql-show-size-of-all-databases.md
Last active February 21, 2024 11:10
PostgreSQL: Show size of all databases

PostgreSQL: Show size of all databases

SELECT pg_database.datname as "database_name", pg_database_size(pg_database.datname)/1024/1024 AS size_in_mb FROM pg_database ORDER by size_in_mb DESC;
SELECT
    pg_database.datname,
    pg_size_pretty(pg_database_size(pg_database.datname)) AS size
@ntamvl
ntamvl / rabbit_publisher.rb
Last active February 21, 2024 11:10
RabbitMQ Ruby Example
# an initializer
# gem install bunny
# written by: Tam Nguyen (twitter: @nguyentamvn)
require 'bunny'
require 'json'
class RabbitPublisher
def initialize(options = {})
bunny_config = {