Skip to content

Instantly share code, notes, and snippets.

@maxcal
maxcal / validate.js
Created June 28, 2012 08:56
jQuery Validate file upload
// assume that form is bound to $form and file input is named image
// @requires: jQuery, jQuery validate
// an array of valid file types
var whitelist = ['png','jpe?g','gif'];
$form.validate(
rules : {
image : {
@maxcal
maxcal / CookieObject.js
Created August 3, 2012 12:43
CookieObject
/*
* Simple implemtation of storing objects in a cookie.
*
* Requires native JSON support or
* https://github.com/douglascrockford/JSON-js/blob/master/json2.js
* for cross browser JSON support.
*
* @param string key - [req] the key of the cookie to create or read.
* @param object data - [opt] the object to store
* @param int days - [opt] How many days before cookie expires
@maxcal
maxcal / watch.rb
Created September 30, 2012 18:42 — forked from appbackr/watch.rb
watchr.rb
$exec = "phpunit"
watch('./(.*).php') { |m| changed(m[0]) }
def changed(file)
run "phpunit"
end
def run(cmd)
result = `#{cmd}`
@maxcal
maxcal / User editable slug - with defaults
Last active December 28, 2015 13:09
Proposed addition to friendly_id guide
User editable slug - with defaults
----------------------------------
Case:
We want our post class to have slug and title attribues that are editable by the user.
The user should be able to edit the title and slug independently.
The slug should default to a slugged version of the title.
example output:
```ruby
@maxcal
maxcal / gist:8e7e0ee4ff83a3ad0c8a
Last active March 30, 2019 01:53 — forked from ismasan/gist:8217804
Rspec examples for HTTP caching
# Given a controller that looks like this
class ModelsController < ApplicationController
load_and_authorize_resource #CanCan
def show
if stale? @model
respond_to do |format|
format.json do
@model
end
sourceMapper = require 'source-map-support'
Dot = require '../../../node_modules/grunt-mocha/node_modules/mocha/lib/reporters/dot.js'
module.exports = Dot
##
parseLine = (line) ->
[_, file, row] = line.match /file:\/\/\/(.*):(\d*)/
frame =
getFileName: -> file
@maxcal
maxcal / multipart_email.rb
Created September 15, 2014 11:20
Testing multipart emails with rspec/capybara
# Sets up `text` and `html` variables that can be used to test the different parts of a multipart email
RSpec.shared_context 'multipart email' do
let(:html) do
Capybara::Node::Simple.new( mail.body.parts.find {|p| p.content_type.match /html/}.body.raw_source )
end
let(:text) { mail.body.parts.find {|p| p.content_type.match /plain/}.body.raw_source }
end
body { background-color: #eeeeee; padding: 0; margin: 5px; overflow-y: scroll; }
#HTMLReporter { font-size: 11px; font-family: Monaco, "Lucida Console", monospace; line-height: 14px; color: #333333; }
#HTMLReporter a { text-decoration: none; }
#HTMLReporter a:hover { text-decoration: underline; }
#HTMLReporter p, #HTMLReporter h1, #HTMLReporter h2, #HTMLReporter h3, #HTMLReporter h4, #HTMLReporter h5, #HTMLReporter h6 { margin: 0; line-height: 14px; }
#HTMLReporter .banner, #HTMLReporter .symbolSummary, #HTMLReporter .summary, #HTMLReporter .resultMessage, #HTMLReporter .specDetail .description, #HTMLReporter .alert .bar, #HTMLReporter .stackTrace { padding-left: 9px; padding-right: 9px; }
#HTMLReporter #jasmine_content { position: fixed; right: 100%; }
#HTMLReporter .version { color: #aaaaaa; }
#HTMLReporter .banner { margin-top: 14px; }
require 'benchmark'
@language = 'english'
string = 'Hello world'
Benchmark.bmbm do |x|
x.report { 100000.times { a = Hash[@language, string] } }
x.report { 100000.times { a = { @language => string } } }
x.report { 100000.times { a = { "{@language}" => string } } }
end
class Parent < ActiveRecord::Base
has_many :children
has_many :skills, through: :children
def self.with_skill(skill_type)
eager_load(:skills, :children).map do |p|
p.children = [] unless p.skills.any? { |s| s.skill_type = skill_type }
p
end
end
end