Skip to content

Instantly share code, notes, and snippets.

View dnch's full-sized avatar

Dan Cheail dnch

  • melbourne, au
View GitHub Profile
@dnch
dnch / derp.js
Created July 17, 2012 02:12
Uh...
(function() {
$(function() {
return $('a[data-toggle="tab"]').on('shown', function(e) {
var persistentTabs;
persistentTabs = localStorage.getObject("persistentTabs") || {};
persistentTabs[location.pathname] = e.target.hash;
return localStorage.setObject("persistentTabs", persistentTabs);
});
});
@dnch
dnch / gist:1643764
Created January 19, 2012 23:42
Ode To The Splat Operator
def test_exclusions(str)
exclusions = ['a', /^I-/]
case str
when *exclusions then
"Excluded"
else
"Accepted"
end
end
# Given a big (~25MB) text file, I want to extract
# and process lines that are prefixed with a certain
# string. In the actual case, known_prefixes is
# approximately 50 elements long with potential for growth
known_prefixes = %w(aa ab ax fy fx)
regex = "^#{known_prefixes.join("|")} "
IO.foreach(big_ass_text_file).each |row|
if row =~ regex
@dnch
dnch / babies_controller.rb
Created December 22, 2011 22:49 — forked from ptagell/babies_controller.rb
Update controller
def update
if @baby.update_attributes(params[:baby])
if params[:notify] == true
flash[:notice] = "Done"
else
redirect_to(root_url(:host => with_subdomain(@baby.subdomain)), :notice => 'Your baby was successfully updated and everyone has been told the good news.')
end
else
render :action => "edit"
end
class FoosController < AbstractController::Base
def update
@foo = Foo.find(params[:id])
# If our changed object saves...
if @foo.update_attributes(params[:attributes])
# Now, we check for changes. In some situations, we might
# use @foo.notify_changed? but given that update_attributes
# clobbers the dirty state of your instance, we need to
@dnch
dnch / user.rb
Created December 19, 2011 22:50 — forked from jimsynz/user.rb
WTF!
class User
include Mongoid::Document
field :email
validates_format_of :email, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i, :message => "is not a valid email adress."
end
class FancyMiddleware
def initialize(app)
@app = app
@excluded_paths = []
end
def call(env)
# env variable may be wrong here
if @excluded_paths.include(env["REQUEST_PATH"])
# do magic
[server] administrator:~$ nc -zvw 1 207.97.227.239 22
github.com [207.97.227.239] 22 (ssh) : No route to host
[server] administrator:~$ nc -zvw 1 207.97.227.239 80
github.com [207.97.227.239] 80 (www) open
module ResourcefulRequestHelper
# Iterates through each route defined for the current controller, yielding
# each one to allow the same set of assertions / expectations to be tested.
#
# Usage:
# ======
# describe UsersController do
# let(:user) { double('user') }
#
@dnch
dnch / gist:1350453
Created November 9, 2011 05:02 — forked from damncabbage/gist:1350367
Calculating Mass-Assignable ActiveRecord Attributes
class Article < ActiveRecord::Base
# Has attributes: :title, :body, :active
attr_protected :active
end
class Image < ActiveRecord::Base
# Has attributes: :title, :filename, :active
attr_accessible :title