This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
gem "minitest" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node | |
attr_accessor :parent, :left, :right, :value | |
def is_leaf? | |
left.nil? && right.nil? | |
end | |
def has_left_child? | |
!left.nil? | |
end |
OlderNewer