Skip to content

Instantly share code, notes, and snippets.

@tylerhunt
tylerhunt / active_record_decorator.rb
Created March 19, 2015 18:52
An abstract decorator that allows decorated Active Record records to be assigned to associations without raising an ActiveRecord::AssociationTypeMismatch error.
require 'delegate'
# An abstract decorator useful for decorating Active Record objects.
class ActiveRecordDecorator < SimpleDelegator
# A proxy for the decorator class to allow the delegation of certain class
# methods to the decorated object's class.
class ClassProxy < SimpleDelegator
def initialize(decorator_class, decorated_class)
super decorator_class
self.decorated_class = decorated_class
@agendor
agendor / hapijs-rest-api-tutorial.md
Last active August 31, 2021 08:31
A practical introduction to building a RESTful API with the hapi.js server framework for Node.js
@cerebrl
cerebrl / 1-securing-express.md
Last active August 2, 2023 22:48
Securing ExpressJS

tl;dr

  1. Don't run as root.
  2. For sessions, set httpOnly (and secure to true if running over SSL) when setting cookies.
  3. Use the Helmet for secure headers: https://github.com/evilpacket/helmet
  4. Enable csrf for preventing Cross-Site Request Forgery: http://expressjs.com/api.html#csrf
  5. Don't use the deprecated bodyParser() and only use multipart explicitly. To avoid multiparts vulnerability to 'temp file' bloat, use the defer property and pipe() the multipart upload stream to the intended destination.
@weimeng
weimeng / gist:4527826
Last active December 11, 2015 02:08
Semantic helpers for responsive website implementations using bootstrap-sass. Specifically adds responsive replacements for the makeRow() and makeColumn() mixins included in Twitter Bootstrap.
@mixin row() {
margin-left: $gridGutterWidth * -1;
@media (max-width: 767px) { margin-left: 0; }
@media (min-width: 768px) and (max-width: 979px) { margin-left: $gridGutterWidth768 * -1; }
@media (min-width: 1200px) { margin-left: $gridGutterWidth1200 * -1; }
@include clearfix();
}
@mixin column($columns: 1, $offset: 0) {
float: left;
@mperham
mperham / after.rb
Created July 4, 2012 19:30
Thread-friendly shared connection
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || ConnectionPool::Wrapper.new(:size => 1) { retrieve_connection }
end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
@indygreg
indygreg / find_old_lines.pl
Created June 17, 2012 20:17
Find oldest lines in git repository
#!/usr/bin/perl
# This script parses Git blame's "porcelain" output format and
# ascertains the oldest lines of code seen.
#
# If you want to perform a custom report, just define your own callback
# function and invoke parse_porcelain() with it.
#
# The expected input format is slightly modified from raw `git blame
# -p`. Here is an example script for producing input:
@thechrisoshow
thechrisoshow / dynamic_validations.rb
Created March 29, 2012 11:56
How to add validations to a specific instance of an active record object?
class Banana < ActiveRecord::Base; end
banana = Banana.new
banana.valid? #=> true
banana.singleton_class.validates_presence_of :name
banana.valid? #=> true - why did the validation not work?
banana.class.validates_presence_of :name
banana.valid? #=> false - as we'd expect...but now...
@romanbsd
romanbsd / colored_logger.rb
Created December 22, 2011 11:10
Color logging for Mongoid
require 'logger'
class ColoredLogger < Logger
WHITE = "\e[37m"
CYAN = "\e[36m"
MAGENTA = "\e[35m"
BLUE = "\e[34m"
YELLOW = "\e[33m"
GREEN = "\e[32m"
RED = "\e[31m"
BLACK = "\e[30m"
@RiANOl
RiANOl / gist:1077760
Last active March 20, 2024 16:12
AES128 / AES256 CBC with PKCS7Padding in Ruby
require "openssl"
require "digest"
def aes128_cbc_encrypt(key, data, iv)
key = Digest::MD5.digest(key) if(key.kind_of?(String) && 16 != key.bytesize)
iv = Digest::MD5.digest(iv) if(iv.kind_of?(String) && 16 != iv.bytesize)
aes = OpenSSL::Cipher.new('AES-128-CBC')
aes.encrypt
aes.key = key
aes.iv = iv
@martinisoft
martinisoft / jquery_setup.rake
Created July 6, 2010 01:24
Rake task to setup a Rails 3 app for jQuery instead of Prototype
# Rails 3 jQuery Install Rakefile
# by Aaron Kalin
# Compiled from http://www.railsinside.com/tips/451-howto-unobtrusive-javascript-with-rails-3.html
#
# Note: this assumes you use git, if not then use the optional usage
#
# Usage: rake install_query
#
# Optional usage: rake install_jquery[nogit]
#