Skip to content

Instantly share code, notes, and snippets.

# I was working on a client project not long back and the client expressed an interest in having thread-able comments, where someone can reply to one comment and it starts off a new thread of comments, which could then be branched even further by other replies. It occurred to me that I already had a polymorphic association with my comments so that different resources could be commented on (articles, photos, statuses, etc...). It seemed plausible I could just go ahead and use this on a self-referential basis as well.
# With polymorphic associations, instead of having a foreign key like, article_id, you have a type column representing the table and an id column for whatever type table that is. For instance:
:commendable_type => 'Article', :commendable_id => 1
# Would be comparable to a foreign key reference of
:article_id => 1
#thus polymorphic models can be associated with any other model type, including themselves it turns out.
:commendable_type => 'Comment'
# Represents a comment that belongs to a comme
@jaeming
jaeming / Gemfile
Last active September 26, 2016 20:36
source "https://rubygems.org"
gem "minitest"
@jaeming
jaeming / split.rb
Created October 9, 2016 19:51
split one liner
def split(str, key)
(list, word = [], '') and str.chars.to_a.map { |i| i == key ? (list << word and word = '') : word << i } and list << word
end
const Node = {
value: null,
parent: null,
left: null,
right: null,
create: function(props={}) {
let node = Object.create(this);
Object.keys(this).forEach( (key)=> {
node[key] = props[key] || this[key]
})
class Node
attr_accessor :parent, :left, :right, :value
def is_leaf?
left.nil? && right.nil?
end
def has_left_child?
!left.nil?
end