Sha256: 3571c963e94c4443a670fadd29cd15369faaa78bd36ef9017208c5c87aa40560

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module Decidim
  module ActionDelegator
    class Delegation < ApplicationRecord
      self.table_name = "decidim_action_delegator_delegations"

      belongs_to :granter, class_name: "Decidim::User"
      belongs_to :grantee, class_name: "Decidim::User"
      belongs_to :setting,
                 foreign_key: "decidim_action_delegator_setting_id",
                 class_name: "Decidim::ActionDelegator::Setting"

      validates :granter, uniqueness: {
        scope: [:setting],
        message: I18n.t("delegations.create.error_granter_unique", scope: "decidim.action_delegator.admin")
      }

      delegate :consultation, to: :setting

      before_destroy { |record| throw(:abort) if record.grantee_voted? }

      def self.granted_to?(user, consultation)
        GranteeDelegations.for(consultation, user).exists?
      end

      def grantee_voted?
        return false unless consultation.questions.any?

        @grantee_voted ||= begin
          granter_votes = Decidim::Consultations::Vote.where(author: granter, question: consultation.questions)
          granter_votes&.detect { |vote| vote.versions.exists?(whodunnit: grantee&.id) } ? true : false
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-action_delegator-0.8.1 app/models/decidim/action_delegator/delegation.rb
decidim-action_delegator-0.7.2 app/models/decidim/action_delegator/delegation.rb
decidim-action_delegator-0.7.1 app/models/decidim/action_delegator/delegation.rb
decidim-action_delegator-0.7.0 app/models/decidim/action_delegator/delegation.rb