Commit 323c2feb by Rocky Duan

using mongoid and some vote/tree gem; initialize models

parent f198cb4e
......@@ -19,6 +19,11 @@ gem 'mongo'
gem 'bson'
gem 'bson_ext'
gem 'mongoid'
gem 'voteable_mongo'
gem 'mongoid-tree'
group :test do
gem 'rspec'
gem 'rack-test', :require => "rack/test"
......
require 'active_record'
require 'ancestry'
require 'thumbs_up'
class Comment < ActiveRecord::Base
attr_accessible :body, :title, :user_id, :course_id, :endorsed, :comment_thread_id
has_ancestry :cache_depth => true
class Comment
include Mongoid::Document
include Mongoid::Tree
include Mongo::Voteable
field :ancestry, type:String
field :body, type: String
field :user_id, type: String
field :course_id, type: String
belongs_to :comment_thread
acts_as_voteable
validates_presence_of :title, :unless => Proc.new{|c| c.is_root? or not c.body.blank?}
validates_presence_of :body, :unless => Proc.new{|c| c.is_root? or not c.title.blank?}
validates_presence_of :user_id, :unless => :is_root?
validates_presence_of :course_id, :unless => :is_root?
validates_presence_of :comment_thread_id
def self.hash_tree(nodes)
nodes.map {|node, sub_nodes| node.to_hash.merge(:children => hash_tree(sub_nodes).compact)}
end
def to_hash_tree(args=nil)
if args and args[:to_depth]
self.class.hash_tree(self.subtree(to_depth: args[:to_depth]).arrange(:order => "updated_at DESC"))
else
self.class.hash_tree(self.subtree.arrange(:order => "updated_at DESC"))
end
end
def to_hash
attributes.merge(:votes => {:up => votes_for, :down => votes_against, :plusminus => plusminus}, :children => [])
end
def to_json
to_hash.to_json
end
end
require 'active_record'
class CommentThread < ActiveRecord::Base
has_one :super_comment, :class_name => "Comment", :dependent => :destroy
# Ensures that each thread is associated with a commentable object
validates_presence_of :commentable_type, :commentable_id
# Ensures that there is only one thread for each commentable object
validates_uniqueness_of :commentable_id, :scope => :commentable_type
# Helper class method to create a new thread with the corresponding super comment
#def self.find_or_build(commentable_type, commentable_id)
# comment_thread = CommentThread.find_or_create_by_commentable_type_and_commentable
# Create a super comment which does not hold anything itself, but points to all comments of the thread
after_create :create_super_comment
def create_super_comment
comment = Comment.create! :comment_thread_id => self.id
end
def root_comments
super_comment.children
end
def comments
super_comment.descendants
end
def json_comments(args=nil)
super_comment.to_hash_tree(args).first[:children].to_json
end
class CommentThread
include Mongoid::Document
field :title, type: String
field :body, type: String
field :course_id, type: String
field :commentable_id, type: String
field :commentable_type, type: String
belongs_to :author, class_name: "User"
has_many :comments
end
require 'active_record'
require 'thumbs_up'
class User < ActiveRecord::Base
acts_as_voter
class User
include Mongoid::Document
include Mongo::Voter
field :external_id, type: String
has_many :comments
end
require 'active_record'
class Vote < ActiveRecord::Base
scope :for_voter, lambda { |*args| where(["voter_id = ? AND voter_type = ?", args.first.id, args.first.class.base_class.name]) }
scope :for_voteable, lambda { |*args| where(["voteable_id = ? AND voteable_type = ?", args.first.id, args.first.class.base_class.name]) }
scope :recent, lambda { |*args| where(["created_at > ?", (args.first || 2.weeks.ago)]) }
scope :descending, order("created_at DESC")
belongs_to :voteable, :polymorphic => true
belongs_to :voter, :polymorphic => true
attr_accessible :vote, :voter, :voteable
# Comment out the line below to allow multiple votes per user.
validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id]
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment