Sha256: fee213b6f07ec8619a7d7c56c2e1af3bb0771c72e0aa9ef262ad336c36c9b19d

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    # A proposal can include an endorsement per user or group.
    class ProposalEndorsement < ApplicationRecord
      include Decidim::Authorable

      belongs_to :proposal, foreign_key: "decidim_proposal_id", class_name: "Decidim::Proposals::Proposal", counter_cache: true

      validates :proposal, uniqueness: { scope: [:author, :user_group] }
      validate :author_and_proposal_same_organization
      validate :proposal_not_rejected

      scope :for_listing, -> { order(:decidim_user_group_id, :created_at) }

      private

      def organization
        proposal&.component&.organization
      end

      # Private: check if the proposal and the author have the same organization
      def author_and_proposal_same_organization
        return if !proposal || !author

        errors.add(:proposal, :invalid) unless author.organization == proposal.organization
      end

      def proposal_not_rejected
        return unless proposal

        errors.add(:proposal, :invalid) if proposal.rejected?
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
decidim-proposals-0.21.0 app/models/decidim/proposals/proposal_endorsement.rb
decidim-proposals-0.20.1 app/models/decidim/proposals/proposal_endorsement.rb
decidim-proposals-0.20.0 app/models/decidim/proposals/proposal_endorsement.rb
decidim-proposals-0.19.1 app/models/decidim/proposals/proposal_endorsement.rb
decidim-proposals-0.19.0 app/models/decidim/proposals/proposal_endorsement.rb