Skip to content

Instantly share code, notes, and snippets.

View dja's full-sized avatar

Daniel Jacob Archer dja

View GitHub Profile
class Video < ActiveRecord::Base
validates_presence_of :title
named_scope :finished, :conditions => { :encoded_state => "finished" }
has_attached_file :video,
:url => ":class/:id/:style/:basename.:extension",
:path => ":class/:id/:style/:basename.:extension",
:storage => :s3
validates_attachment_presence :video
@henrik
henrik / nullify_blank_attributes.rb
Created November 27, 2010 10:11
ActiveRecord mixin to save NULL instead of empty strings to database.
module NullifyBlankAttributes
def write_attribute(attr_name, value)
new_value = value.presence
super(attr_name, new_value)
end
end
@greg-barnett
greg-barnett / gist:878958
Created March 21, 2011 03:15
Delayed::Job belongs_to owner
# db/migrate/#{timestamp}_add_owner_to_delayed_jobs
class AddOwnerToDelayedJobs < ActiveRecord::Migration
def self.up
add_column(:delayed_jobs, :owner_type, :string)
add_column(:delayed_jobs, :owner_id, :integer)
end
def self.down
remove_column(:delayed_jobs, :owner_type)
remove_column(:delayed_jobs, :owner_id)
@afeld
afeld / gist:1254889
Created September 30, 2011 20:28
YouTube video ID regex
# Parses YouTube URLs directly or from iframe code. Handles:
# * Address bar on YouTube url (ex: http://www.youtube.com/watch?v=ZFqlHhCNBOI)
# * Direct http://youtu.be/ url (ex: http://youtu.be/ZFqlHhCNBOI)
# * Full iframe embed code (ex: <iframe src="http://www.youtube.com/embed/ZFqlHhCNBOI">)
# * Old <object> tag embed code (ex: <object><param name="movie" value="http://www.youtube.com/v/ZFqlHhCNBOI">...)
/(youtu\.be\/|youtube\.com\/(watch\?(.*&)?v=|(embed|v)\/))([^\?&"'>]+)/
$5 #=> the video ID
# test it on Rubular: http://rubular.com/r/eaJeSMkJvo
@mikeyk
mikeyk / gist:1329319
Created October 31, 2011 22:56
Testing storage of millions of keys in Redis
#! /usr/bin/env python
import redis
import random
import pylibmc
import sys
r = redis.Redis(host = 'localhost', port = 6389)
mc = pylibmc.Client(['localhost:11222'])
@paulirish
paulirish / data-markdown.user.js
Last active October 22, 2024 14:19
*[data-markdown] - use markdown, sometimes, in your HTML
// ==UserScript==
// @name Use Markdown, sometimes, in your HTML.
// @author Paul Irish <http://paulirish.com/>
// @link http://git.io/data-markdown
// @match *
// ==/UserScript==
// If you're not using this as a userscript just delete from this line up. It's cool, homey.
@peteboere
peteboere / jquery.alterclass.js
Created December 24, 2011 12:49
jQuery alterClass plugin: Remove element classes with wildcard matching. Optionally add classes.
/**
* jQuery alterClass plugin
*
* Remove element classes with wildcard matching. Optionally add classes:
* $( '#foo' ).alterClass( 'foo-* bar-*', 'foobar' )
*
* Copyright (c) 2011 Pete Boere (the-echoplex.net)
* Free under terms of the MIT license: http://www.opensource.org/licenses/mit-license.php
*
*/
@rujmah
rujmah / authmongoid.md
Created April 17, 2012 16:35
Mongoid implementation for authentication from scratch

Mongoid implementation of Ryan Bates authentication-from-scratch lesson on Railscasts.

Also uses 'username' rather than 'email' for validation

Commandline:

rails new authmongoid -O
cd authmongoid/

Gemfile:

@linjunpop
linjunpop / README.md
Created August 21, 2012 01:15
Rails flash messages with AJAX requests
@nikcub
nikcub / README.md
Created October 4, 2012 13:06
Facebook PHP Source Code from August 2007