Sha256: 91b5aa8eeb31184223436ae3615b244721cd7c0ee9739a8bc165fe96bb65a81a

Contents?: true

Size: 1.97 KB

Versions: 6

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module Decidim
  module EnhancedTextwork
    # Exposes the paragraph vote resource so users can vote paragraphs.
    class ParagraphVotesController < Decidim::EnhancedTextwork::ApplicationController
      include ParagraphVotesHelper
      include Rectify::ControllerHelpers

      helper_method :paragraph

      before_action :authenticate_user!

      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

      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

      private

      def paragraph
        @paragraph ||= Paragraph.where(component: current_component).find(params[:paragraph_id])
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-enhanced_textwork-1.0.5 app/controllers/decidim/enhanced_textwork/paragraph_votes_controller.rb
decidim-enhanced_textwork-1.0.4 app/controllers/decidim/enhanced_textwork/paragraph_votes_controller.rb
decidim-enhanced_textwork-1.0.3 app/controllers/decidim/enhanced_textwork/paragraph_votes_controller.rb
decidim-enhanced_textwork-1.0.2 app/controllers/decidim/enhanced_textwork/paragraph_votes_controller.rb
decidim-enhanced_textwork-1.0.1 app/controllers/decidim/enhanced_textwork/paragraph_votes_controller.rb
decidim-enhanced_textwork-1.0.0 app/controllers/decidim/enhanced_textwork/paragraph_votes_controller.rb