Module: Decidim::Comments::CommentsHelper

Defined in:
app/helpers/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, options = {}) ⇒ Object

Creates a Comments component which is rendered using ‘react_ujs` from react-rails gem

resource - A commentable resource options - A hash of options (default: {})

:arguable - A boolean value to indicate if tihs option is available

Returns a div which contain a RectComponent to be rendered by ‘react_ujs`



14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/decidim/comments/comments_helper.rb', line 14

def comments_for(resource, options = {})
  commentable_type = resource.class.name
  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,
                                    options: options.slice(:arguable, :votable),
                                    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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/helpers/decidim/comments/comments_helper.rb', line 29

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]}",
          options: JSON.parse("#{j(props[:options].to_json)}"),
          locale: "#{props[:locale]}"
        }
      );
    })
end