Sha256: e83ad59c7c48c4f309993f482f27d0416a7473a0bb11618a74e94b4e67140b46

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true
module Decidim
  module Comments
    # A helper to expose the comments component for a commentable
    module CommentsHelper
      # 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`
      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

      # 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
      def react_comments_component(node_id, props)
        content_tag("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
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
decidim-0.0.4 decidim-comments/lib/decidim/comments/comments_helper.rb
decidim-comments-0.0.3 app/helpers/decidim/comments/comments_helper.rb
decidim-0.0.3 decidim-comments/app/helpers/decidim/comments/comments_helper.rb