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 / digital_ocean_setup.md
Created December 15, 2015 16:45 — forked from ChuckJHardy/digital_ocean_setup.md
DigitalOcean Ubuntu 14.04 x64 + Rails 4 + Nginx + Unicorn + PostgreSQL + Capistrano 3 Setup Instructions

DigitalOcean Ubuntu 14.04 x64 + Rails 4 + Nginx + Unicorn + PostgreSQL + Capistrano 3

SSH into Root

$ ssh root@123.123.123.123

Change Root Password

@ntamvl
ntamvl / mina_deploy_example.rb
Last active February 21, 2024 11:17 — forked from jbonney/deploy.rb
Mina deployment file to setup new host for Rails applications. Creates the folder structure, fill up the database.yml file, create the associated DB and user and set up new Apache virtual host file.
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rvm'
# Usually mina focuses on deploying to one host and the deploy options are therefore simple.
# In our case, there is a number of possible servers to deploy to, it is therefore necessary to
# specify the host that we are targeting.
server = ENV['server']
# Since the same host can have multiple applications running in parallel, it is necessary to
@ntamvl
ntamvl / deploy.rake
Created December 21, 2015 08:25 — forked from shmatov/deploy.rake
rails + mina + unicorn
# lib/tasks/deploy.rake
namespace :deploy do
desc 'Deploy to staging environment'
task :staging do
exec 'mina deploy -f config/deploy/staging.rb'
end
end
@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 / 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.