Permalink
Browse files

mongo_collection (for posts.comments)

  • Loading branch information...
1 parent 5652c0e commit 0777f754f1ad37f770aa6cdd59ab5ea7536af1c2 Arthur PETRY committed May 31, 2011
Showing with 109 additions and 6 deletions.
  1. +1 −1 app/controllers/posts_controller.rb
  2. +12 −2 app/models/comment.rb
  3. +9 −1 app/models/post.rb
  4. +25 −0 lib/mongo_collection.rb
  5. +50 −0 lib/mongo_embedded_record.rb
  6. +12 −2 lib/mongo_record.rb
@@ -1,7 +1,7 @@
class PostsController < ApplicationController
def index
- @posts = Post.all
+ @posts = Post.all.sort(['_id','desc'])
@comment = Comment.new
end
View
@@ -8,8 +8,18 @@ class Comment
# key :body, String, :required => true
# timestamps
- def to_param
- id
+ include MongoEmbeddedRecord
+ key :author_id
+ key :body
+ key :created_at
+
+ def author
+ @author ||= User.find(author_id)
+ end
+
+ def author=(author)
+ self.author_id = author && author.id
+ @author = author
end
end
View
@@ -8,12 +8,20 @@ class Post
key :title #, String, :required => true
key :body # String, :required => true
key :author_id #, String, :required => true
+
+ key :created_at
+ key :updated_at
+
# belongs_to :author, :class_name => "User"
- # many :comments
+ many :comments
# before_save :timestamps_in_comments
+ def author
+ @author ||= User.find(author_id)
+ end
+
def timestamps_in_comments
current_time = Time.now.utc
comments.each do |comment|
View
@@ -0,0 +1,25 @@
+class MongoCollection
+
+ def initialize(parent,target_name,target_class)
+ @parent = parent
+ @target_name = target_name
+ @target_class = target_class
+ end
+
+ def attributes_array
+ @parent.attributes[@target_name.to_s] ||= []
+ end
+
+ def target
+ attributes_array.map{|attr| @target_class.new(attr)}
+ end
+
+ def <<(object)
+ attributes_array << object.attributes
+ end
+
+ def method_missing(name, *args, &block)
+ target.send(name, *args, &block)
+ end
+
+end
@@ -0,0 +1,50 @@
+module MongoEmbeddedRecord
+ extend ActiveSupport::Concern
+
+ # make ActionPack happy : include ActiveModel::Naming and define instance method to_key
+ included do
+ extend ActiveModel::Naming
+ end
+
+ module ClassMethods
+
+ def key(attribute_name)
+ define_method attribute_name do
+ attributes[attribute_name.to_s]
+ end
+ define_method "#{attribute_name}=" do |v|
+ attributes[attribute_name.to_s] = v
+ end
+ end
+
+ end
+
+ module InstanceMethods
+
+ def initialize(attributes={})
+ @attributes = attributes.stringify_keys!
+ end
+
+ def attributes; @attributes; end
+
+ def id
+ attributes['_id']
+ end
+
+ def to_param
+ id.to_s
+ end
+
+ def to_key
+ [self.class.to_s.underscore]
+ end
+
+ def attributes=(new_attributes)
+ new_attributes.each do |attr,v|
+ send("#{attr}=",v)
+ end
+ end
+
+ end
+
+end
View
@@ -63,11 +63,11 @@ def all
end
def first()
- find().limit(1).first
+ find().sort(['_id', 'asc']).limit(1).first
end
def last
- all.last # FIXME
+ find().sort(['_id', 'desc']).limit(1).first
end
def key(attribute_name)
@@ -78,6 +78,16 @@ def key(attribute_name)
attributes[attribute_name.to_s] = v
end
end
+
+ def many(collection_name)
+ define_method collection_name do
+ collection = instance_variable_get("@#{collection_name}")
+ return collection if collection
+ collection = MongoCollection.new(self, collection_name, collection_name.to_s.singularize.camelcase.constantize)
+ instance_variable_set("@#{collection_name}", collection)
+ return collection
+ end
+ end
end
module InstanceMethods

0 comments on commit 0777f75

Please sign in to comment.