Skip to content

Instantly share code, notes, and snippets.

View bokmann's full-sized avatar

David Bock bokmann

View GitHub Profile
@bokmann
bokmann / gist:1915372
Created February 26, 2012 08:48
Mel's solution
# Mel,
#
# Sorry, I read too much of a previous problem I had into your question and
# probably spun you off in the wrong direction. Jim's comment points you in
# the right direction, but its only going to get you so far because of a
# limitation in combining polymorphism and HasManyThrough. You will run into
# this error trying to do what you want to do:
#
# http://rubydoc.info/github/rails/rails/master/ActiveRecord/HasManyThroughAssociationPolymorphicSourceError
#
@bokmann
bokmann / gist:1912350
Created February 26, 2012 02:38
Another example for Mel...
# Mel, Here is another gist for you, perhaps slightly better than the last.
# First, the migrations are exactly the same, but this improves the left-right
# situation slightly.
# kit.rb
class Kit < ActiveRecord::Base
has_many :peer_kit_relations,
:class_name => "KitRelation",
:foreign_key => :left_kit_id
@bokmann
bokmann / gist:1912278
Created February 26, 2012 02:07
notes on Mel's Peer Kit relationships...
# Mel, This is fugly, but it has the benefit of only using
# activerecord relations - here's why:
#
# Using the has_many :through technique, we need to account for the
# possibility of the 'peer' being in either of the two 'belongs_to'
# relationships in the relation. To keep ourselves mentally straight
# in this, the code refers to them as the 'left' and 'right' side.
# We need to do two queries and join them to get all the possible peers.
# There is a potentially better, but more complex solution to I'll
# outline below:
@bokmann
bokmann / no_such_thing.rb
Created February 23, 2012 15:43
The code of the talk from my Feb 22nd Arlington Ruby talk 'There is No Such Thing as Metaprogramming'.
# This is the code from my 'There is No Such Thing as Metaprogramming' talk,
# which premiered at the Arlington, VA Ruby Users Group on Feb 22nd.
# Without the deliver and walk-through to the solution below this example
# will be missing quite an important bit of content (mainly the tracking of
# 'self' while developing the solution, but it still a useful read.
# Here is the Toddler with no metajuju. Note that the developer, as well as
# the code, is completely unuaware of the interpreter. A developer with a
# background in compiled languages would be comfortable looking at this.
@bokmann
bokmann / README
Created April 27, 2011 16:52 — forked from ncr/README
Code
$("button").single_double_click(function () {
alert("Try double-clicking me!")
}, function () {
alert("Double click detected, I'm hiding")
$(this).hide()
})
@bokmann
bokmann / everytimezone.coffee
Created April 27, 2011 16:49 — forked from madrobby/everytimezone.coffee
First conversion of http://everytimezone.com/ JavaScript code to CoffeeScript
# Two things are important to note:
#
# 1) The code is fugly, because it was a JavaScript/iPad experiment.
# I know that it is ugly, so pretty please don't comment on that part.
# 2) I tried to use as many CoffeeScript features as possible,
# including but not limited to list comprehensions,
# heredocs, destructuring assignment and also the "do" operator
#
# I welcome comments about stuff that is not CoffeeScripty enough, or what I should
# write differently.
@bokmann
bokmann / business_time_examples.rb
Created April 25, 2010 22:12
text for my blog entry showing how to use business_time
# require the libraries to make it all work
require 'rubygems'
require 'active_support'
require 'business_time'
# What time is it now? Of course, this will be different
# for you, so your relative answrs below will vary
# accordingly:
Time.now
class YourTypicalWorkitem < ActiveRecord::Base
include StonePath
stonepath_workitem do
aasm_initial_state :data_entry
aasm_state :data_entry
aasm_state :data_validation
aasm_state :further_processing
def create
unless @commentable.nil?
@comment = @commentable.comments.create!(params[:comment])
respond_to do |format|
format.html { redirect_to @commentable }
format.json { @commentable.to_json }
format.partial { render :partial => "comments/comment.html.erb",
:object => @comment,
:layout => false }
end
Mime::Type.register "text/html-partial", :partial