Skip to content

Instantly share code, notes, and snippets.

@canweriotnow
canweriotnow / array1.sql
Created April 8, 2014 15:11
Gists for Rails 4 PostgreSQL talk
CREATE TABLE test (
id integer,
title varchar(255),
body text,
child_ids integer[]); -- This is an array
@canweriotnow
canweriotnow / handler.clj
Created January 8, 2014 18:54
speclj crash with luminus
(ns hedgehog.handler
(:require [compojure.core :refer [defroutes]]
[hedgehog.routes.home :refer [home-routes]]
[noir.util.middleware :as middleware]
[compojure.route :as route]
[taoensso.timbre :as timbre]
[com.postspectacular.rotor :as rotor]
[selmer.parser :as parser]
[environ.core :refer [env]]
[hedgehog.routes.cljsexample :refer [cljs-routes]]))
App = new Marionette.Application();
App.addRegions {
"headerRegion": "#header"
"topMenuRegion": "#top-menu"
"mainRegion" : "#main"
}
App.on 'initialize:after', ->
Backbone.history.start()
# db/migrate/XXXXXXXXXXXXX_add_authentication_token_to_users.rb
class AddAuthenticationTokenToUsers < ActiveRecord::Migration
def change
add_column :users, :authentication_token, :string
add_index :users, :authentication_token, :unique => true
end
end
@canweriotnow
canweriotnow / testes.rb
Created December 6, 2013 16:00
Quickie test server for testing things
require 'sinatra'
require 'json'
post '/good' do
content_type :json
[201, {message: "Successfully registered."}.to_json]
end
post '/bad' do
content_type :json
@canweriotnow
canweriotnow / sanderlingshare.html
Last active December 26, 2015 20:28
Share to Sanderling button
<span style="background-color:#A8BE38;display:inline-block;padding:4px;border-radius:5px;">
<a href="#" class="sanderling-share-button btn" onClick="sanderlingShare();" style="font-family: Helvetica, Arial, sans-serif;font-size: small;text-decoration: none; color: black;">
<div id="innerling">
<img src="https://s3.amazonaws.com/AnEstuary/brand/birdie-sm.png" width="20px"/>
Share on Sanderling
</div>
</a>
</span>
<script type="text/javascript">
var sanderlingShare = function() {
#!/usr/bin/env ruby
puts "How much IP do you have?"
ip_have = STDIN.gets.chomp #don't really need the STDIN
puts "Which champion do you want to buy?"
champion_want = STDIN.gets.chomp
lucian = #{lucian_value} #The #{} construct is for inside double-quoted strings, otherwise # just indicates a comment.
lucian_value = 6300
puts "#{champion_want} is #{champion_want} IP, so this means you need #{lucian_value} - #{ip_have}" #The math has to happen inside the #{} block... also, ip_have is read as a String, need to convert to an integer.

Polymorphic Associations reversed

It's pretty easy to do polymorphic associations in Rails: A Picture can belong to either a BlogPost or an Article. But what if you need the relationship the other way around? A Picture, a Text and a Video can belong to an Article, and that article can find all media by calling @article.media

This example shows how to create an ArticleElement join model that handles the polymorphic relationship. To add fields that are common to all polymorphic models, add fields to the join model.

@canweriotnow
canweriotnow / action_dispatch.rb
Created July 24, 2012 21:08
How to keep requests non-local
class ::ActionDispatch::Request
def local?
if Rails.env == 'production'
false
else
super
end
end
@canweriotnow
canweriotnow / Gemfile
Created July 24, 2012 18:33
Pieces of my app...
source 'http://rubygems.org'
gem 'rake', '0.8.7'
gem 'rails', '~> 3.0.7'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
# New Relic monitoring