Sha256: 2961d50f3f9b36189a063401b9f7267b37902de34f1285ffd0cdea0f206c773b

Contents?: true

Size: 851 Bytes

Versions: 6

Compression:

Stored size: 851 Bytes

Contents

# frozen_string_literal: true
module Decidim
  # A ResourceLink allows linking resources (other entities in Decidim) between
  # them without them having to know each other.
  #
  # In order to group the relations between resources you need to specify a name.
  #
  # Some examples could be: proposals that have been created in a meeting or
  # projects that are a result of merging diffrent proposals.
  class ResourceLink < ApplicationRecord
    belongs_to :from, polymorphic: true
    belongs_to :to, polymorphic: true

    validates :from, :to, :name, presence: true
    validates :name, uniqueness: { scope: [:from, :to] }

    validate :same_organization

    private

    def same_organization
      return if from.try(:organization) == to.try(:organization)

      errors.add(:from, :invalid)
      errors.add(:to, :invalid)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.1.0 app/models/decidim/resource_link.rb
decidim-core-0.0.8.1 app/models/decidim/resource_link.rb
decidim-core-0.0.7 app/models/decidim/resource_link.rb
decidim-core-0.0.6 app/models/decidim/resource_link.rb
decidim-core-0.0.5 app/models/decidim/resource_link.rb
decidim-core-0.0.3 app/models/decidim/resource_link.rb