Class: Decidim::Comments::CommentsController

Inherits:
ApplicationController show all
Includes:
ResourceHelper
Defined in:
decidim-comments/app/controllers/decidim/comments/comments_controller.rb

Overview

Controller that manages the comments for a commentable object.

Instance Method Summary collapse

Methods included from ResourceHelper

#linked_classes_filter_values_for, #linked_classes_for, #linked_resources_for, #resource_locator, #resource_title

Methods inherited from ApplicationController

#permission_class_chain

Methods included from UserBlockedChecker

#check_user_block_status, #check_user_not_blocked

Methods included from NeedsSnippets

#snippets

Methods included from HttpCachingDisabler

#disable_http_caching

Methods included from HasStoredPath

#skip_store_location?, #store_current_location

Methods included from RegistersPermissions

register_permissions

Methods included from NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#createObject


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'decidim-comments/app/controllers/decidim/comments/comments_controller.rb', line 40

def create
  enforce_permission_to :create, :comment, commentable: commentable

  form = Decidim::Comments::CommentForm.from_params(
    params.merge(commentable: commentable)
  ).with_context(
    current_organization: current_organization,
    current_component: commentable.try(:component) || commentable.participatory_space
  )
  Decidim::Comments::CreateComment.call(form, current_user) do
    on(:ok) do |comment|
      handle_success(comment)
      respond_to do |format|
        format.js { render :create }
      end
    end

    on(:invalid) do
      @error = t("create.error", scope: "decidim.comments.comments")
      respond_to do |format|
        format.js { render :error }
      end
    end
  end
end

#indexObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'decidim-comments/app/controllers/decidim/comments/comments_controller.rb', line 16

def index
  enforce_permission_to :read, :comment, commentable: commentable

  @comments = SortedComments.for(
    commentable,
    order_by: order,
    after: params.fetch(:after, 0).to_i
  )
  @comments_count = commentable.comments_count

  respond_to do |format|
    format.js do
      if reload?
        render :reload
      else
        render :index
      end
    end

    # This makes sure bots are not causing unnecessary log entries.
    format.html { redirect_to commentable_path }
  end
end