Class: Decidim::EnhancedTextwork::ParagraphVotesController

Inherits:
ApplicationController
  • Object
show all
Includes:
ParagraphVotesHelper, Rectify::ControllerHelpers
Defined in:
app/controllers/decidim/enhanced_textwork/paragraph_votes_controller.rb

Overview

Exposes the paragraph vote resource so users can vote paragraphs.

Instance Method Summary collapse

Methods included from ParagraphVotesHelper

#can_accumulate_supports_beyond_threshold?, #current_user_can_vote?, #remaining_votes_count_for, #threshold_per_paragraph, #threshold_per_paragraph_enabled?, #vote_button_classes, #vote_limit, #vote_limit_enabled?, #votes_blocked?, #votes_count_classes, #votes_enabled?

Methods inherited from ApplicationController

#paragraph_limit, #paragraph_limit_reached?, #paragraphs

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/decidim/enhanced_textwork/paragraph_votes_controller.rb', line 14

def create
  enforce_permission_to :vote, :paragraph, paragraph: paragraph
  @from_paragraphs_list = params[:from_paragraphs_list] == "true"
  @chevron_button = params[:chevron_button] == "true"

  VoteParagraph.call(paragraph, current_user) do
    on(:ok) do
      paragraph.reload

      paragraphs = ParagraphVote.where(
        author: current_user,
        paragraph: Paragraph.where(component: current_component)
      ).map(&:paragraph)

      expose(paragraphs: paragraphs)
      render :update_buttons_and_counters
    end

    on(:invalid) do
      render json: { error: I18n.t("paragraph_votes.create.error", scope: "decidim.enhanced_textwork") }, status: :unprocessable_entity
    end
  end
end

#destroyObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/decidim/enhanced_textwork/paragraph_votes_controller.rb', line 38

def destroy
  enforce_permission_to :unvote, :paragraph, paragraph: paragraph
  @from_paragraphs_list = params[:from_paragraphs_list] == "true"
  @chevron_button = params[:chevron_button] == "true"

  UnvoteParagraph.call(paragraph, current_user) do
    on(:ok) do
      paragraph.reload

      paragraphs = ParagraphVote.where(
        author: current_user,
        paragraph: Paragraph.where(component: current_component)
      ).map(&:paragraph)

      expose(paragraphs: paragraphs + [paragraph])
      render :update_buttons_and_counters
    end
  end
end