Sha256: c159e0601ab2b61b80179af4a8d02363887ca002231e98c832134a6f24d636a7

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Decidim
  module ActionDelegator
    module Consultations
      module QuestionVotesControllerOverride
        extend ActiveSupport::Concern

        included do
          helper ::Decidim::ActionDelegator::DelegationHelper

          def destroy
            enforce_permission_to_unvote

            user = delegation.blank? ? current_user : delegation.granter

            PaperTrail.request(enabled: delegation.present?, whodunnit: current_user.id) do
              Decidim::Consultations::UnvoteQuestion.call(current_question, user) do
                on(:ok) do
                  current_question.reload
                  render :update_vote_button
                end
              end
            end
          end

          private

          def info_for_paper_trail
            if delegation.present?
              { decidim_action_delegator_delegation_id: delegation.id }
            else
              {}
            end
          end

          def delegation
            @delegation ||= Decidim::ActionDelegator::Delegation.find_by(id: params[:decidim_consultations_delegation_id])
          end

          def enforce_permission_to_unvote
            if delegation.blank?
              enforce_permission_to :unvote, :question, question: current_question
            else
              raise Decidim::ActionForbidden unless allowed_to?(
                :unvote_delegation,
                :question,
                { question: current_question, delegation: delegation },
                [Decidim::ActionDelegator::Permissions, Decidim::Admin::Permissions, Decidim::Permissions]
              )
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-action_delegator-0.8.1 app/controllers/concerns/decidim/action_delegator/consultations/question_votes_controller_override.rb
decidim-action_delegator-0.7.2 app/controllers/concerns/decidim/action_delegator/consultations/question_votes_controller_override.rb
decidim-action_delegator-0.7.1 app/controllers/concerns/decidim/action_delegator/consultations/question_votes_controller_override.rb
decidim-action_delegator-0.7.0 app/controllers/concerns/decidim/action_delegator/consultations/question_votes_controller_override.rb