Class: Decidim::Comments::CommentsController

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

Overview

Controller that manages the comments for a commentable object.

Instance Method Summary collapse

Methods inherited from ApplicationController

#permission_class_chain

Instance Method Details

#createObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/decidim/comments/comments_controller.rb', line 86

def create
  enforce_permission_to(:create, :comment, commentable:)

  form = Decidim::Comments::CommentForm.from_params(
    params.merge(commentable:)
  ).with_context(
    current_organization:,
    current_component:,
    current_user:
  )
  Decidim::Comments::CreateComment.call(form) 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

#current_componentObject



113
114
115
116
117
# File 'app/controllers/decidim/comments/comments_controller.rb', line 113

def current_component
  return commentable.component if commentable.respond_to?(:component)
  return commentable.participatory_space if commentable.respond_to?(:participatory_space)
  return commentable if Decidim.participatory_space_manifests.find { |manifest| manifest.model_class_name == commentable.class.name }
end

#destroyObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/decidim/comments/comments_controller.rb', line 119

def destroy
  set_comment
  @commentable = @comment.commentable

  enforce_permission_to(:destroy, :comment, comment:)

  Decidim::Comments::DeleteComment.call(comment, current_user) do
    on(:ok) do
      @comments_count = @comment.root_commentable.comments_count
      respond_to do |format|
        format.js { render :delete }
      end
    end

    on(:invalid) do
      respond_to do |format|
        format.js { render :deletion_error }
      end
    end
  end
end

#indexObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/decidim/comments/comments_controller.rb', line 18

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

  if commentable.is_a?(Decidim::Comments::Comment)
    @comments = commentable.descendants.includes(:author, :up_votes, :down_votes).to_a
    @has_more_comments = false
  else
    @sorted_comments_query = SortedComments.new(
      commentable,
      order_by: order,
      offset: comments_offset,
      alignment:
    )
    @comments = @sorted_comments_query.query
    @has_more_comments = @sorted_comments_query.has_more?
  end
  @comments = @comments.reject do |comment|
    next if comment.depth < 1
    next if !comment.deleted? && !comment.hidden?

    comment.commentable.replies.blank?
  end
  @comments_count = commentable.comments_count

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

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

#updateObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/decidim/comments/comments_controller.rb', line 58

def update
  set_comment
  set_commentable
  enforce_permission_to(:update, :comment, comment:)

  form = Decidim::Comments::CommentForm.from_params(
    params.merge(commentable: comment.commentable)
  ).with_context(
    current_user:,
    current_organization:,
    current_component:
  )

  Decidim::Comments::UpdateComment.call(comment, form) do
    on(:ok) do
      respond_to do |format|
        format.js { render :update }
      end
    end

    on(:invalid) do
      respond_to do |format|
        format.js { render :update_error }
      end
    end
  end
end