Sha256: bdd5c800b84c1c9d2f3a60704519c4f6e5dbc258bf01305f37a750148896da53

Contents?: true

Size: 1.07 KB

Versions: 24

Compression:

Stored size: 1.07 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

24 entries across 24 versions & 1 rubygems

Version Path
decidim-proposals-0.11.2 app/models/decidim/proposals/proposal_endorsement.rb
decidim-proposals-0.12.0.pre app/models/decidim/proposals/proposal_endorsement.rb
decidim-proposals-0.11.1 app/models/decidim/proposals/proposal_endorsement.rb
decidim-proposals-0.11.0.pre1 app/models/decidim/proposals/proposal_endorsement.rb