Created
October 28, 2010 23:33
-
-
Save JonKernPA/652557 to your computer and use it in GitHub Desktop.
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 Blog | |
include MongoMapper::Document | |
# Attributes :::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
key :title, String | |
key :category_id, ObjectId | |
key :archive_after, Time | |
# Assocations ::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
belongs_to :category | |
many :comments | |
# Validations ::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
# Callbacks ::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
before_save :set_archive | |
after_save :log_save | |
def to_s | |
puts "\n----------\n#{title}" | |
comments.each do |c| | |
c.to_s if c.comment_id.nil? | |
end | |
end | |
private | |
def log_save | |
puts "Blog created, will be archived on #{archive_after.strftime("%d %b %Y") unless archive_after.nil?}" | |
end | |
def set_archive | |
puts "set archive" | |
self.archive_after = Time.now + 30.days | |
end | |
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
class Comment | |
include MongoMapper::Document | |
key :text, String | |
many :comments | |
key :comment_id, ObjectId | |
belongs_to :comment | |
key :blog_id, ObjectId | |
belongs_to :blog | |
def to_s(depth = 1) | |
prefix = "\t"*depth | |
puts "#{prefix}#{text}" | |
comments.each do |c| | |
c.to_s(depth+1) | |
end | |
end | |
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
# Be sure to have mongod running | |
# Below is an attempt to show one way to have nested comments on a blog post | |
# I did not thoroughly examine whether it is bulletproof or performant. But it seemed to work :-) | |
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/../app/model") | |
require 'rubygems' | |
require 'mongo_mapper' | |
require 'blog' | |
require 'comment' | |
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017) | |
MongoMapper.database = 'demo' | |
col = Blog.all() | |
col.each {|c| c.destroy } | |
col = Comment.all() | |
col.each {|c| c.destroy } | |
blog = Blog.create(:title => "Greatest Idea Ever" ) | |
c1 = Comment.create( :text => "Comment 1", :blog => blog) | |
c2 = Comment.create(:text => "Comment 2", :blog => blog) | |
c11 = Comment.create(:text => "Comment 1 - 1", :blog => blog, :comment => c1) | |
c12 = Comment.create(:text => "Comment 1 - 2", :blog => blog, :comment => c11) | |
blog.to_s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment