Module: Decidim::Comments::CommentsHelper

Defined in:
lib/decidim/comments/comments_helper.rb

Overview

A helper to expose the comments component for a commentable

Instance Method Summary collapse

Instance Method Details

#comments_for(resource) ⇒ Object

Render commentable comments inside the ‘expanded` template content.

resource - A commentable resource



10
11
12
13
14
15
# File 'lib/decidim/comments/comments_helper.rb', line 10

def comments_for(resource)
  return unless resource.commentable?
  content_for :expanded do
    inline_comments_for(resource)
  end
end

#inline_comments_for(resource) ⇒ Object

Creates a Comments component which is rendered using ‘ReactDOM`

resource - A commentable resource

Returns a div which contain a RectComponent



22
23
24
25
26
27
28
29
30
# File 'lib/decidim/comments/comments_helper.rb', line 22

def inline_comments_for(resource)
  return unless resource.commentable?
  commentable_type = resource.commentable_type
  commentable_id = resource.id.to_s
  node_id = "comments-for-#{commentable_type.demodulize}-#{commentable_id}"
  react_comments_component(node_id, commentableType: commentable_type,
                                    commentableId: commentable_id,
                                    locale: I18n.locale)
end

#react_comments_component(node_id, props) ⇒ Object

Private: Render Comments component using inline javascript

node_id - The id of the DOMElement to render the React component props - A hash corresponding to Comments component props



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/decidim/comments/comments_helper.rb', line 36

def react_comments_component(node_id, props)
  ("div", "", id: node_id) +
    javascript_include_tag("decidim/comments/comments") +
    javascript_tag(%{
      window.DecidimComments.renderCommentsComponent(
        '#{node_id}',
        {
          commentableType: "#{props[:commentableType]}",
          commentableId: "#{props[:commentableId]}",
          locale: "#{props[:locale]}"
        }
      );
    })
end