Sha256: b2aec0db2947845aaf863eb1a18a26afb06b39ea670f191308400e8133571805

Contents?: true

Size: 1.47 KB

Versions: 75

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

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

    included do
      after_commit :store_reference, on: [:create, :update]

      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. Works for both component resources and
      # participatory spaces.
      #
      # Returns a String.
      def calculate_reference
        Decidim.reference_generator.call(self, respond_to?(:component) ? component : nil)
      end

      # Internal: Sets the unique reference to the model. Note that if the resource
      # implements `Decidim::Traceable` then any normal update (or `update`)
      # will create a new version through an ActiveRecord update callback, but here
      # we can't track the author of the version, so we use the `update_column` method
      # which does not trigger callbacks.
      #
      # Returns nothing.
      def store_reference
        self[:reference] ||= calculate_reference
        return unless changed?

        # rubocop:disable Rails/SkipsModelValidations
        update_column(:reference, self[:reference])
        # rubocop:enable Rails/SkipsModelValidations
      end
    end
  end
end

Version data entries

75 entries across 75 versions & 1 rubygems

Version Path
decidim-core-0.27.9 lib/decidim/has_reference.rb
decidim-core-0.27.8 lib/decidim/has_reference.rb
decidim-core-0.27.7 lib/decidim/has_reference.rb
decidim-core-0.27.6 lib/decidim/has_reference.rb
decidim-core-0.26.10 lib/decidim/has_reference.rb
decidim-core-0.26.9 lib/decidim/has_reference.rb
decidim-core-0.27.5 lib/decidim/has_reference.rb
decidim-core-0.26.8 lib/decidim/has_reference.rb
decidim-core-0.27.4 lib/decidim/has_reference.rb
decidim-core-0.27.3 lib/decidim/has_reference.rb
decidim-core-0.26.7 lib/decidim/has_reference.rb
decidim-core-0.26.5 lib/decidim/has_reference.rb
decidim-core-0.27.2 lib/decidim/has_reference.rb
decidim-core-0.27.1 lib/decidim/has_reference.rb
decidim-core-0.26.4 lib/decidim/has_reference.rb
decidim-core-0.27.0 lib/decidim/has_reference.rb
decidim-core-0.26.3 lib/decidim/has_reference.rb
decidim-core-0.27.0.rc2 lib/decidim/has_reference.rb
decidim-core-0.27.0.rc1 lib/decidim/has_reference.rb
decidim-core-0.26.2 lib/decidim/has_reference.rb