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


49
50
51
52
53
# File 'app/models/comments_proxy/base.rb', line 49

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
29
30
31
32
33
34
35
36
37
38
# 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


45
46
47
# File 'app/models/comments_proxy/base.rb', line 45

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.


41
42
43
# File 'app/models/comments_proxy/base.rb', line 41

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