Skip to content

Instantly share code, notes, and snippets.

@camallen
camallen / dynamic_document.rb
Created November 22, 2011 12:25
Extend the fields of a Mongoid document from a .yml file
module ExtendFields
require "yaml"
private
def extend_fields
dynamic_fields.each { |name, type| self.class.field name, type: type }
end
def dynamic_fields
YAML.load_file( File.join( Rails.root, 'config', 'class_fields', "#{self.class.name.underscore}.yml" ) ) rescue {}
@camallen
camallen / gist:3795872
Created September 27, 2012 19:17
Simple Form: Array field custom input
#Simple form Input class for arrays
class ArrayInput < SimpleForm::Inputs::Base
def input
#override the attribute name for arrays to allow rails to handle array forms
input_html_options.merge!({:name => "#{self.object_name}[#{attribute_name}][]"})
@builder.text_field(attribute_name, input_html_options)
end
end
@camallen
camallen / gist:3803552
Created September 29, 2012 09:24
Custom simple form Hash field input
#Simple form Input class for a hash
class HashInput < SimpleForm::Inputs::Base
def input
key = input_html_options.delete(:key)
#override the params to be in the format that rails will auto correlate back to an
# attribute hash with key: value
input_html_options.merge!({:name => "#{self.object_name}[#{attribute_name}][#{key}]"})
@builder.text_field(attribute_name, input_html_options)
end
end
@camallen
camallen / gist:3844542
Created October 6, 2012 10:14
Nil Boolean Mongoid custom serialization with blank strings
class TriBoolean
attr_reader :value
def initialize(value)
@value = value
end
# Converts an object of this instance into a database friendly value.
def mongoize
@camallen
camallen / gist:5364140
Last active December 16, 2015 02:39
CSS - Responsive tables using media queries for small viewports
/*
* CSS Media queries for responsive tables
* http://stackoverflow.com/questions/9746979/tables-in-a-responsive-web-design
* and http://css-tricks.com/responsive-data-tables/
*
* http://jsfiddle.net/camallen/gvExW/5/
/*
@media all and (max-width:640px) {
table,thead,tbody,tfoot,th,td,tr{display:block}
@camallen
camallen / gist:9482975
Created March 11, 2014 10:16
Uninstall all Rbenv gems from shell
for i in `gem list --no-versions`; do gem uninstall -aIx $i; done
@camallen
camallen / gist:b7a804c0abe88eddf7cb
Created May 28, 2014 16:16
project example with has_many workflows
require 'spec_helper'
describe Project, type: :model do
let(:project) { build(:project) }
it "should have a valid factory" do
expect(project).to be_valid
end
@camallen
camallen / penguin_project_json_file_list.rb
Created June 2, 2014 15:23
Example project JSON manifest script
#!/usr/bin/env ruby
# Create a JSON file of all the penguin subject images in the s3 bucket
# combined with the per site/filename provided metadata in CSV files.
require 'aws-sdk'
require 'json'
require 'csv'
require 'active_support'
require 'zooniverse_data'
require 'pry'
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>SoundCloud OAuth 2 User Agent Authentication Flow Demo</title>
<script type="text/javascript" charset="utf-8" src="javascript/jquery-1.4.2.js"></script>
<script type="text/javascript" charset="utf-8">
$(function () {
var extractToken = function(hash) {
function dethrottle {
sudo ipfw delete 1
}
function throttle {
if test $# -eq 0
then
echo 'throttle <port> <bandwidth (KB)>'
elif test $# -eq 1
then