Skip to content

Instantly share code, notes, and snippets.

View abitdodgy's full-sized avatar
😃
Out to lunch!

Mohamad El-Husseini abitdodgy

😃
Out to lunch!
View GitHub Profile
@abitdodgy
abitdodgy / gist:4007431
Created November 3, 2012 13:50 — forked from lucasfais/gist:1207002
Sublime Text 2 - Useful Shortcuts

Sublime Text 2 – Useful Shortcuts (Mac OS X)

General

⌘T go to file
⌘⌃P go to project
⌘R go to methods
⌃G go to line
⌘KB toggle side bar
⌘⇧P command prompt
@abitdodgy
abitdodgy / jquery.counter.js
Created November 27, 2012 16:37
Lightweight jQuery character counter plugin.
(function ($) {
// Author: Mohamad El-Husseini, www.mohamad.im
// Light weight character counter for jQuery.
// Example: http://jsfiddle.net/yHPg7/6/
"use strict";
$.fn.counter = function (options) {
options = $.extend($.fn.counter.defaults, options);
@abitdodgy
abitdodgy / jquery.counter.js.coffee
Created November 27, 2012 18:57
A CoffeeScript version of my lightweight character counter for jQuery.
(($) ->
# Author: Mohamad El-Husseini, www.mohamad.im
# Light weight character counter for jQuery.
# Example: http://jsfiddle.net/yHPg7/6/
"use strict"
$.fn.counter = (options) ->
defaultText = ->
options.defaultText.replace /minSize/g, options.minSize
count = (elem) ->
@abitdodgy
abitdodgy / Ubuntu Setup Guide for Rails Deployment.md
Last active January 29, 2018 16:02
A guide for setting up an Ubuntu server for Rails deployment

Rails Deployment with Ubuntu 12.10, RVM, Postgresql, Nginx, and Unicorn.

This setup was used successfully on a DigitalOcean VPS. After much trial and error, and following a number of disparate guides and tutorials, I was able to distil the process to the information found in this gist.

Before getting started, you should create a new server droplet. Login to verify that everything is working correctly.

ssh root@xxx.xxx.xx.xx
@abitdodgy
abitdodgy / hot_algorithm.md
Last active December 18, 2015 18:39
ActiveRecord "Hot" algorithm.

A sorting algorithm for sorting by "hotness", roughly based on Reddit's algorithm with a few tweeks.

scope :order_by_hot, ->
  { order("round((votes_count - 1) / POW(DATE_PART('day', Now() - created_at) * 24 + DATE_PART('hour', Now() - created_at) + 2, 1.5)::numeric, 8) DESC, votes_count DESC") }

Friendly URLs

By default, Rails applications build URLs based on the primary key -- the id column from the database. Imagine we have a Person model and associated controller. We have a person record for Bob Martin that has id number 6. The URL for his show page would be:

/people/6

But, for aesthetic or SEO purposes, we want Bob's name in the URL. The last segment, the 6 here, is called the "slug". Let's look at a few ways to implement better slugs.

@abitdodgy
abitdodgy / s3_pre_signed_url.rb
Created January 10, 2014 16:01
Generate a pre-signed url for S3 direct upload.
class SignedUrlsController < ApplicationController
def s3
# Create client and set bucket
client = AWS::S3.new
bucket = client.buckets[ENV['S3_BUCKET']]
# Extract file extension since we're renaming the file
extension = File.extname(params[:image_name]).downcase
# Get mime-type from extension
@abitdodgy
abitdodgy / creating_a_chef_kitchen.md
Last active August 29, 2015 13:58
Creating a chef recipe.

Creating a New Chef Kitchen

A kitchen is a collection of recipes associated with a deployment. For example, a kitchen can contain cookbooks for installing Ruby, MySQL, and nginx.

knife init solo kitchen_name

This will create the necessary stucture for the kitchen.

@abitdodgy
abitdodgy / email_address.rb
Created January 29, 2015 18:15
EmailAddress class with unit tests.
require "i18n"
class EmailAddress
VALID_EMAIL_ADDRESS_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
def self.malformed?(email)
email !~ VALID_EMAIL_ADDRESS_REGEX
end
def self.valid?(email)
@abitdodgy
abitdodgy / _flash.html.erb
Last active August 29, 2015 14:14
An application.html.erb layout file for Rails 4 to use with Sublime Text's Insert Gist feature
<% flash.each do |key, message| %>
<%= content_tag :div, class: "alert alert-full-page alert-#{key} alert-dismissable", role: "alert" do %>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<%= message %>
<% end %>
<% end %>