Sha256: eb9ef93bc433a3e294e6a79056b5081cd1e1dad5e08f6c4a3e17ef731a4526bb

Contents?: true

Size: 881 Bytes

Versions: 59

Compression:

Stored size: 881 Bytes

Contents

# frozen_string_literal: true

module Decidim
  # A resource can have an endorsement for each user or group the author/endorser belongs to.
  class Endorsement < ApplicationRecord
    include Decidim::Authorable

    belongs_to :resource,
               polymorphic: true,
               counter_cache: true

    validates :resource_id, uniqueness: { scope: [:resource_type, :author, :user_group] }
    validate :author_and_resource_same_organization

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

    private

    def organization
      resource&.component&.organization
    end

    # Private: check if the resource and the author have the same organization
    def author_and_resource_same_organization
      return if !resource || !author

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

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/models/decidim/endorsement.rb
decidim-core-0.28.4 app/models/decidim/endorsement.rb
decidim-core-0.27.9 app/models/decidim/endorsement.rb
decidim-core-0.29.0 app/models/decidim/endorsement.rb
decidim-core-0.28.3 app/models/decidim/endorsement.rb
decidim-core-0.27.8 app/models/decidim/endorsement.rb
decidim-core-0.29.0.rc4 app/models/decidim/endorsement.rb
decidim-core-0.29.0.rc3 app/models/decidim/endorsement.rb
decidim-core-0.29.0.rc2 app/models/decidim/endorsement.rb
decidim-core-0.29.0.rc1 app/models/decidim/endorsement.rb
decidim-core-0.28.2 app/models/decidim/endorsement.rb
decidim-core-0.27.7 app/models/decidim/endorsement.rb
decidim-core-0.28.1 app/models/decidim/endorsement.rb
decidim-core-0.27.6 app/models/decidim/endorsement.rb
decidim-core-0.26.10 app/models/decidim/endorsement.rb
decidim-core-0.26.9 app/models/decidim/endorsement.rb
decidim-core-0.28.0 app/models/decidim/endorsement.rb
decidim-core-0.27.5 app/models/decidim/endorsement.rb
decidim-core-0.28.0.rc5 app/models/decidim/endorsement.rb
decidim-core-0.28.0.rc4 app/models/decidim/endorsement.rb