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 / gist:9860a4fc5d8065427a79
Created December 23, 2015 03:57
nginx + unicorn + performance tweaks: nginx.conf example
# nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
timer_resolution 500ms;
@ntamvl
ntamvl / gist:8bcf8ab6c700cca6346d
Created December 23, 2015 03:58
nginx + unicorn + performance tweaks: myapp.conf example
# myapp
upstream unicorn_myapp {
server unix:/var/rails/myapp/current/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
server_name myapp.com www.myapp.com;
root /var/rails/myapp/current/public;
@ntamvl
ntamvl / rspec_html.rake
Created January 11, 2016 07:03 — forked from jonahoffline/rspec_html.rake
Rake Task for generating a RSpec report in html.
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = '--format html --out reports/rspec_results.html'
end
namespace :rspec_report do
desc 'Run all specs and generate RSpec report in HTML'
task :html => :spec
@ntamvl
ntamvl / elasticsearch.md
Created March 4, 2016 04:00 — forked from dominicsayers/elasticsearch.md
Configuring ElasticSearch to use less memory

What I actually did

/etc/security/limits.conf

elasticsearch hard memlock 100000

/etc/default/elasticsearch

@ntamvl
ntamvl / exclude_some _stylesheets_in_Rails_4.md
Last active February 21, 2024 11:17
Exclude Some Stylesheets in Rails 4

Rails 4 uses asset pipeline to concatenate and minify or compress JavaScript and CSS assets. It creates a default app/assets/stylesheets/application.css file which contains these lines:

/*
*= require_self
*= require_tree .
*/

The require_tree directive tells Sprockets to recursively include all CSS files in the specified directory into the output.

There are times that for some layout pages that we would like to exclude a couple of CSS files. Following two ways help us to accomplish this:

@ntamvl
ntamvl / Using_UUIDs_in_Rails.md
Last active February 21, 2024 11:17
Using UUIDs in Rails

Why use UUIDs?

It may seem like a trivial decision at first glance, but using UUIDs can have an intricate effect on your applications design structure. If you are building an application and have an id:integer column that auto increments with the creation of every item you add to that table you could end up with some sever issues. As applications grow in size we end up having to seperate parts of the application into different servers: a cluster of database servers, background, jobs, cache server, an httpd server, and throw in a few servers for actual application and you’ve got a whole cluster of services to deal with.

Lets say you’ve got 3 servers running the actual application, each one chugging along running serveral instances of your web application. Now say it’s a basic CRUD application if you’ve got several instances writing to the database at once you will have a conflict. If you’ve got a table named books and a few rows are being added at once by seperate visitors, you’re auto incrimenting the rows

@ntamvl
ntamvl / Default_UUID_In_Rails.md
Created May 11, 2016 06:39
Default UUID's In Rails

UUID’s

I love UUID’s. You should use them. Rails 4 makes it simple to get setup and use UUID’s throughout your project.

First, we enable UUID’s:

rails g migration enable_uuid_extension

This creates the following migration:

class EnableUuidExtension < ActiveRecord::Migration
@ntamvl
ntamvl / how-to-start-using-uuid-in-activerecord-with-postgresql.md
Created May 11, 2016 08:05
How to start using UUID in ActiveRecord with PostgreSQL

Although it may be obvious for many developers, there are still some that are not aware of great features that PostgreSQL allows to use with ActiveRecord. This tutorial is intended to reveal UUID type, which we can use in our Rails applications, especially as models’ attributes.

Special Postgre’s data types are available in our databases by enabling so called extensions. According to the documentation:

PostgreSQL is extensible because its operation is catalog-driven. The catalogs appear to the user as tables like any other. PostgreSQL stores much more information in its catalogs like data types, functions and access methods. These tables can be extended by the user. Traditional database systems can only be extended by changing source code or by loading modules written by the DBMS vendor. So not only we get a possibility to create our own extensions, but we get a bunch of useful features out of the box as well. Let’s see one of them in action right now.

@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 / install-squid-proxy-server-on-ubuntu-14-04.md
Last active May 14, 2016 08:27
Install Squid proxy server on Ubuntu 14.04

Install Squid proxy server on Ubuntu 14.04

Squid is a web proxy and cache server which primarily provides proxy and cache services for the HTTP protocol. In this tutorial we are going to show you how to install and configure Squid proxy server on a Linux VPS.

Before we start with installing and configuring Squid, let’s update all the system software to the latest version available:

sudo apt-get update && sudo apt-get -y upgrade

The update should take few moments. Once it is done, you are ready to proceed with the other steps of this tutorial. Since Squid is available in the Ubuntu repositories, the installation of Squid proxy server on an Ubuntu VPS is pretty straightforward. To install it on your server you need to run the following command:

sudo apt-get install squid