Sha256: 2eb251a3dcf91202cde3590a89ade075133479845638fea152555a44ebb2ff5d

Contents?: true

Size: 1.82 KB

Versions: 80

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module Decidim
  module Comments
    # A helper to expose the comments component for a commentable
    module CommentsHelper
      # Render commentable comments inside the `expanded` template content.
      #
      # resource - A commentable resource
      def comments_for(resource)
        return unless resource.commentable?
        content_for :expanded do
          inline_comments_for(resource)
        end
      end

      # Creates a Comments component which is rendered using `ReactDOM`
      #
      # resource - A commentable resource
      #
      # Returns a div which contain a RectComponent
      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

      # 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]}",
                locale: "#{props[:locale]}"
              }
            );
          })
      end
    end
  end
end

Version data entries

80 entries across 80 versions & 2 rubygems

Version Path
decidim-comments-0.18.1 lib/decidim/comments/comments_helper.rb
decidim-comments-0.17.2 lib/decidim/comments/comments_helper.rb
decidim-comments-0.18.0 lib/decidim/comments/comments_helper.rb
decidim-comments-0.17.1 lib/decidim/comments/comments_helper.rb
decidim-comments-0.16.1 lib/decidim/comments/comments_helper.rb
decidim-comments-0.17.0 lib/decidim/comments/comments_helper.rb
decidim-comments-0.16.0 lib/decidim/comments/comments_helper.rb
decidim-comments-0.15.2 lib/decidim/comments/comments_helper.rb
decidim-comments-0.15.1 lib/decidim/comments/comments_helper.rb
decidim-comments-0.15.0 lib/decidim/comments/comments_helper.rb
decidim-comments-0.14.4 lib/decidim/comments/comments_helper.rb
decidim-comments-0.14.3 lib/decidim/comments/comments_helper.rb
decidim-comments-0.14.2 lib/decidim/comments/comments_helper.rb
decidim-comments-0.14.1 lib/decidim/comments/comments_helper.rb
decidim-comments-0.13.1 lib/decidim/comments/comments_helper.rb
decidim-comments-0.12.2 lib/decidim/comments/comments_helper.rb
decidim-comments-0.13.0 lib/decidim/comments/comments_helper.rb
decidim-comments-0.12.1 lib/decidim/comments/comments_helper.rb
decidim-comments-0.13.0.pre1 lib/decidim/comments/comments_helper.rb
decidim-comments-0.12.0 lib/decidim/comments/comments_helper.rb