Class: CommentHandler

Inherits:
Object
  • Object
show all
Defined in:
app/services/comment_handler.rb

Overview

Handler for working with comments

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user = nil) ⇒ CommentHandler

Returns a new instance of CommentHandler.

Parameters:

  • user (User) (defaults to: nil)


8
9
10
11
12
# File 'app/services/comment_handler.rb', line 8

def initialize(user = nil)
  slug = Biovision::Components::CommentsComponent::SLUG
  @user = user
  @handler = Biovision::Components::BaseComponent.handler(slug, user)
end

Instance Attribute Details

#userObject

Returns the value of attribute user.



5
6
7
# File 'app/services/comment_handler.rb', line 5

def user
  @user
end

Instance Method Details

#allow?(*privilege_name) ⇒ Boolean

Parameters:

  • privilege_name (String)

Returns:

  • (Boolean)


46
47
48
# File 'app/services/comment_handler.rb', line 46

def allow?(*privilege_name)
  @handler.allow?(*privilege_name)
end

#allow_reply?(entity) ⇒ Boolean

Parameters:

  • entity (ApplicationRecord)

Returns:

  • (Boolean)


51
52
53
# File 'app/services/comment_handler.rb', line 51

def allow_reply?(entity)
  entity.respond_to?(:commentable_by?) && entity.commentable_by?(@user)
end

#approve?Boolean

Auto-approve comment from current user?

If premoderation flag is not set, every comment is automatically approved. If premoderation flag is set, difference between approved and non-approved comments must be more than threshold; anonymous comments are always non-approved.

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
# File 'app/services/comment_handler.rb', line 34

def approve?
  return true unless @handler.settings['premoderate']
  return false if @user.nil?
  return true if @handler.allow?

  gate = @handler.settings['auto_approve_threshold'].to_i
  positive = Comment.where(user: @user, approved: true).count
  negative = Comment.where(user: @user, approved: false).count
  positive - negative >= gate
end

#list(entity) ⇒ Object

Get list of comments for entity

Depending on privileges, receives list for visitors (only visible and approved) or all comments (including deleted).

Parameters:

  • entity (ApplicationRecord)


20
21
22
23
24
25
26
# File 'app/services/comment_handler.rb', line 20

def list(entity)
  if @handler.allow?
    entity.comments.list_for_administration
  else
    entity.comments.list_for_visitors
  end
end