Skip to content

Instantly share code, notes, and snippets.

View tyrauber's full-sized avatar

Ty Rauber tyrauber

View GitHub Profile
@tyrauber
tyrauber / gist:ed4e0f6053d49608bee7
Created March 4, 2016 21:33
HSTORE Model View Statistics
Let's say you got a Job model and you want to track index_count, show_count, etc.
Why not use Hstore?
Add a migration:
class AddHstoreCountsOnJobs < ActiveRecord::Migration
def change
add_column :jobs, :index_count, :hstore, default: {}
add_column :jobs, :show_count, :hstore, default: {}
@tyrauber
tyrauber / gist:5de42bf4f66c4084b99e
Created September 16, 2015 23:18
HTML5 Safari Form Validation Polyfill
validate_form = ->
$('form').submit (event) ->
$.each $(this).find('input'), (i,el) ->
validate_field(el);
$(el).on 'blur', -> validate_field(el) ;
window.validate_form = validate_form
validate_field = (el) ->
if ($(el)[0].checkValidity())
@tyrauber
tyrauber / Postgres time_ago_in_words(date) function
Created October 27, 2014 22:43
Postgres time_ago_in_words(date) function
CREATE OR REPLACE FUNCTION time_ago_in_words(timestamp with time zone)
RETURNS text
LANGUAGE SQL
AS $$
SELECT CASE
WHEN date_part('year', age(current_timestamp, $1)) = 1 THEN concat(date_part('year', age(current_timestamp, $1)), ' year ago')
WHEN date_part('year', age(current_timestamp, $1)) > 1 THEN concat(date_part('year', age(current_timestamp, $1)), ' years ago')
WHEN date_part('month', age(current_timestamp, $1)) = 1 THEN concat(date_part('month', age(current_timestamp, $1)), ' month ago')
WHEN date_part('month', age(current_timestamp, $1)) > 1 THEN concat(date_part('month', age(current_timestamp, $1)), ' months ago')
WHEN date_part('day', age(current_timestamp, $1)) = 1 THEN concat(date_part('day', age(current_timestamp, $1)), ' day ago')
# config/initializer/notification_center.rb
NotificationCenter.queue
NotificationCenter.thread
ActiveSupport::Notifications.subscribe /(.)+\.notification/i do |*args|
NotificationCenter.queue << args
end
@tyrauber
tyrauber / s3_field_field error with active_admin stack trace
Created December 1, 2013 03:55
s3_field_field error with active_admin stack trace.
activesupport (3.2.12) lib/active_support/core_ext/string/output_safety.rb:24:in `html_escape'
actionpack (3.2.12) lib/action_view/helpers/tag_helper.rb:151:in `block in tag_options'
actionpack (3.2.12) lib/action_view/helpers/tag_helper.rb:138:in `each_pair'
actionpack (3.2.12) lib/action_view/helpers/tag_helper.rb:138:in `tag_options'
actionpack (3.2.12) lib/action_view/helpers/tag_helper.rb:66:in `tag'
actionpack (3.2.12) lib/action_view/helpers/active_model_helper.rb:24:in `tag'
actionpack (3.2.12) lib/action_view/helpers/form_helper.rb:1046:in `to_input_field_tag'
s3_file_field (1.0.4) lib/s3_file_field/form_helper.rb:24:in `s3_file_field'
s3_file_field (1.0.4) lib/s3_file_field/form_helper.rb:7:in `s3_file_field'
app/views/assets/partials/_avatar_form.html.erb:4:in `_app_views_assets_partials__avatar_form_html_erb__818076104424137440_70152587222680'
@tyrauber
tyrauber / gist:3723999
Created September 14, 2012 19:03
Riak cluster setup script
### ruby riak.rb number_of_nodes
require "rubygems"
# `killall epmd` ## to kill running riaks
@riak_url = "http://s3.amazonaws.com/downloads.basho.com/riak/CURRENT/osx/10.4/"
@riak_filename = "riak-1.2.0-osx-x86_64.tar.gz"
@port = 8098
@tyrauber
tyrauber / html5_js_geolocation_reverse_geolocation
Created August 14, 2012 18:30
HTML5 JS Geolocation Reverse-Geolocation
var latlng;
var address;
geocoder = new google.maps.Geocoder();
function success(position) {
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
address = results[1].formatted_address;
console.log(results[1]);
@tyrauber
tyrauber / gist:3350666
Created August 14, 2012 16:35
CrossFilter with lineChart
<!DOCTYPE html>
<meta charset="utf-8">
<title>Crossfilter</title>
<style>
/** CrossFilter LineChart example
The current CrossFilter demo, available here: http://square.github.com/crossfilter/
... Only renders data as bar charts using d3.js. D3.js is an awesome visualization library that provides the ability to generate many different styles of visualization. This GIST is an attempt to document rendering cross filter data as a line chart instead of a bar chart.
@tyrauber
tyrauber / gist:3269339
Created August 6, 2012 02:46
autocomplete snippet
function submitForm(){
$.ajax({
type: 'POST',
url: "admin",
data: {
utf8: "✓",
authenticity_token: $("meta[name='csrf-token']").attr('content'),
search: {
q: $('#search_q').val()
},
class Census2010
require 'csv'
include Ripple::Document
property :id, Integer, :presence => true
property :sumlevel, Integer, :presence => true
property :state_id, Integer
property :stusab, String
property :logrecno, Integer