Sha256: cb44f5a994e240406d8ef6fc7471884224ab6b29b58639b029ca08ef86b01823

Contents?: true

Size: 929 Bytes

Versions: 32

Compression:

Stored size: 929 Bytes

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  # A concern with the features needed when you want a model to have a
  # reference.
  module HasReference
    extend ActiveSupport::Concern

    included do
      after_commit :store_reference

      validates :reference, presence: true, on: :update

      def reference
        self[:reference] || calculate_reference
      end

      private

      # Public: Calculates a unique reference for the model using the function
      # provided by configuration
      #
      # Returns a String.
      def calculate_reference
        return unless feature
        Decidim.resource_reference_generator.call(self, feature)
      end

      # Internal: Sets the unique reference to the model.
      #
      # Returns nothing.
      def store_reference
        self[:reference] ||= calculate_reference
        save if changed?
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 2 rubygems

Version Path
decidim-core-0.7.4 lib/decidim/has_reference.rb
decidim-core-0.7.3 lib/decidim/has_reference.rb
decidim-core-0.7.2 lib/decidim/has_reference.rb
decidim-core-0.7.1 lib/decidim/has_reference.rb
decidim-core-0.7.0 lib/decidim/has_reference.rb
decidim-core-0.6.8 lib/decidim/has_reference.rb
decidim-0.6.8 decidim-core/lib/decidim/has_reference.rb
decidim-core-0.6.7 lib/decidim/has_reference.rb
decidim-0.6.7 decidim-core/lib/decidim/has_reference.rb
decidim-core-0.6.6 lib/decidim/has_reference.rb
decidim-0.6.6 decidim-core/lib/decidim/has_reference.rb
decidim-core-0.6.5 lib/decidim/has_reference.rb
decidim-0.6.5 decidim-core/lib/decidim/has_reference.rb
decidim-core-0.6.4 lib/decidim/has_reference.rb
decidim-0.6.4 decidim-core/lib/decidim/has_reference.rb
decidim-core-0.6.3 lib/decidim/has_reference.rb
decidim-0.6.3 decidim-core/lib/decidim/has_reference.rb
decidim-core-0.6.2 lib/decidim/has_reference.rb
decidim-0.6.2 decidim-core/lib/decidim/has_reference.rb
decidim-core-0.6.1 lib/decidim/has_reference.rb