Class: CommentsProxy::Base

Inherits:
Object
  • Object
show all
Defined in:
app/models/comments_proxy/base.rb

Overview

Base object handling comment proxies. Subclasses should implement #request_ids

Direct Known Subclasses

Plate, Tube

Instance Method Summary collapse

Constructor Details

#initialize(commentable) ⇒ Base

Returns a new instance of Base


10
11
12
# File 'app/models/comments_proxy/base.rb', line 10

def initialize(commentable)
  @commentable = commentable
end

Instance Method Details

#add_comment_to_submissions(comment) ⇒ Object


39
40
41
# File 'app/models/comments_proxy/base.rb', line 39

def add_comment_to_submissions(comment)
  Submission.where(id: submission_ids).find_each { |submission| submission.add_comment(comment.description, comment.user, comment.title) }
end

#comment_assnObject

Keep all this away from the comment class itself.

  • Finds any comments associated with the asset

  • OR with any requests returned by request_ids

  • Then group them together to perform de-duplication


18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/comments_proxy/base.rb', line 18

def comment_assn
  @comment_assn ||= Comment.where(commentable_type: 'Request', commentable_id: request_ids)
                           .or(Comment.where(commentable: @commentable))
                           .create_with(commentable: @commentable)
                           .select(
                             # We need to describe how we select values which aren't included in the group by
                             # This is required with default configurations of MySQL 5.7 and ensures reproducible
                             # queries with other set-ups.
                             ['MIN(id) AS id', :title, :user_id, :description, 'MIN(created_at) AS created_at', 'MIN(updated_at) AS updated_at']
                           ).group(:description, :title, :user_id)
end

#count(*_args) ⇒ Object


35
36
37
# File 'app/models/comments_proxy/base.rb', line 35

def count(*_args)
  comment_assn.count(:all).length
end

#size(*args) ⇒ Object

We're using group above, resulting in size and count returning a hash, not a count.


31
32
33
# File 'app/models/comments_proxy/base.rb', line 31

def size(*args)
  comment_assn.size(*args).length
end