Commit 9f6b0ad7 by Clinton Blackburn

Cleaned CommentThread model

- Removed dead code
- Replaced unnecessary double quotes with single quotes
- Reformatted one-line methods
parent 96ae039c
...@@ -7,6 +7,8 @@ class CommentThread < Content ...@@ -7,6 +7,8 @@ class CommentThread < Content
include Mongoid::Timestamps include Mongoid::Timestamps
include Mongoid::Attributes::Dynamic include Mongoid::Attributes::Dynamic
include ActiveModel::MassAssignmentSecurity include ActiveModel::MassAssignmentSecurity
include Tire::Model::Search
include Tire::Model::Callbacks
extend Enumerize extend Enumerize
voteable self, :up => +1, :down => -1 voteable self, :up => +1, :down => -1
...@@ -30,8 +32,6 @@ class CommentThread < Content ...@@ -30,8 +32,6 @@ class CommentThread < Content
index({author_id: 1, course_id: 1}) index({author_id: 1, course_id: 1})
include Tire::Model::Search
include Tire::Model::Callbacks
index_name Content::ES_INDEX_NAME index_name Content::ES_INDEX_NAME
...@@ -50,12 +50,12 @@ class CommentThread < Content ...@@ -50,12 +50,12 @@ class CommentThread < Content
indexes :commentable_id, type: :string, index: :not_analyzed, included_in_all: false indexes :commentable_id, type: :string, index: :not_analyzed, included_in_all: false
indexes :author_id, type: :string, as: 'author_id', index: :not_analyzed, included_in_all: false indexes :author_id, type: :string, as: 'author_id', index: :not_analyzed, included_in_all: false
indexes :group_id, type: :integer, as: 'group_id', index: :not_analyzed, included_in_all: false indexes :group_id, type: :integer, as: 'group_id', index: :not_analyzed, included_in_all: false
indexes :id, :index => :not_analyzed indexes :id, :index => :not_analyzed
indexes :thread_id, :analyzer => :keyword, :as => "_id" indexes :thread_id, :analyzer => :keyword, :as => '_id'
end end
belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true#, autosave: true belongs_to :author, class_name: 'User', inverse_of: :comment_threads, index: true
has_many :comments, dependent: :destroy#, autosave: true# Use destroy to envoke callback on the top-level comments TODO async has_many :comments, dependent: :destroy # Use destroy to invoke callback on the top-level comments
has_many :activities, autosave: true has_many :activities, autosave: true
attr_accessible :title, :body, :course_id, :commentable_id, :anonymous, :anonymous_to_peers, :closed, :thread_type attr_accessible :title, :body, :course_id, :commentable_id, :anonymous, :anonymous_to_peers, :closed, :thread_type
...@@ -71,24 +71,12 @@ class CommentThread < Content ...@@ -71,24 +71,12 @@ class CommentThread < Content
before_create :set_last_activity_at before_create :set_last_activity_at
before_update :set_last_activity_at, :unless => lambda { closed_changed? } before_update :set_last_activity_at, :unless => lambda { closed_changed? }
after_update :clear_endorsements after_update :clear_endorsements
before_destroy :destroy_subscriptions before_destroy :destroy_subscriptions
scope :active_since, ->(from_time) { where(:last_activity_at => {:$gte => from_time}) } scope :active_since, ->(from_time) { where(:last_activity_at => {:$gte => from_time}) }
scope :standalone_context, ->() { where(:context => :standalone) } scope :standalone_context, ->() { where(:context => :standalone) }
scope :course_context, ->() { where(:context => :course) } scope :course_context, ->() { where(:context => :course) }
def self.new_dumb_thread(options={})
c = self.new
c.title = options[:title] || "title"
c.body = options[:body] || "body"
c.commentable_id = options[:commentable_id] || "commentable_id"
c.course_id = options[:course_id] || "course_id"
c.author = options[:author] || User.first
c.save!
c
end
def activity_since(from_time=nil) def activity_since(from_time=nil)
if from_time if from_time
activities.where(:created_at => {:$gte => from_time}) activities.where(:created_at => {:$gte => from_time})
...@@ -97,13 +85,21 @@ class CommentThread < Content ...@@ -97,13 +85,21 @@ class CommentThread < Content
end end
end end
def activity_today; activity_since(Date.today.to_time); end def activity_today
activity_since(Date.today.to_time)
end
def activity_this_week; activity_since(Date.today.to_time - 1.weeks); end def activity_this_week
activity_since(Date.today.to_time - 1.weeks)
end
def activity_this_month; activity_since(Date.today.to_time - 1.months); end def activity_this_month
activity_since(Date.today.to_time - 1.months)
end
def activity_overall; activity_since(nil); end def activity_overall
activity_since(nil)
end
def root_comments def root_comments
Comment.roots.where(comment_thread_id: self.id) Comment.roots.where(comment_thread_id: self.id)
...@@ -127,24 +123,25 @@ class CommentThread < Content ...@@ -127,24 +123,25 @@ class CommentThread < Content
def to_hash(params={}) def to_hash(params={})
as_document.slice(*%w[thread_type title body course_id anonymous anonymous_to_peers commentable_id created_at updated_at at_position_list closed context]) as_document.slice(*%w[thread_type title body course_id anonymous anonymous_to_peers commentable_id created_at updated_at at_position_list closed context])
.merge("id" => _id, "user_id" => author_id, .merge('id' => _id,
"username" => author_username, 'user_id' => author_id,
"votes" => votes.slice(*%w[count up_count down_count point]), 'username' => author_username,
"abuse_flaggers" => abuse_flaggers, 'votes' => votes.slice(*%w[count up_count down_count point]),
"tags" => [], 'abuse_flaggers' => abuse_flaggers,
"type" => "thread", 'tags' => [],
"group_id" => group_id, 'type' => 'thread',
"pinned" => pinned?, 'group_id' => group_id,
"comments_count" => comment_count) 'pinned' => pinned?,
'comments_count' => comment_count)
end end
def comment_thread_id def comment_thread_id
#so that we can use the comment thread id as a common attribute for flagging #so that we can use the comment thread id as a common attribute for flagging
self.id self.id
end end
private private
def set_last_activity_at def set_last_activity_at
self.last_activity_at = Time.now.utc unless last_activity_at_changed? self.last_activity_at = Time.now.utc unless last_activity_at_changed?
...@@ -165,5 +162,4 @@ private ...@@ -165,5 +162,4 @@ private
def destroy_subscriptions def destroy_subscriptions
subscriptions.delete_all subscriptions.delete_all
end end
end 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