Sha256: 40760ff0cf2cf3ea5376fbb51081ab07ed68701b79179084442611b443f75c2e

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

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 in
      # the following format:
      #
      # "BCN-DPP-2017-02-6589" which in this example translates to:
      #
      # BCN: A setting configured at the organization to be prepended to each reference.
      # PROP: Unique name identifier for a resource: Decidim::Proposals::Proposal (MEET for meetings or PROJ for projects).
      # 2017-02: Year-Month of the resource creation date
      # 6589: ID of the resource
      #
      # Returns a String.
      def calculate_reference
        return unless feature

        ref = feature.participatory_process.organization.reference_prefix
        class_identifier = self.class.name.demodulize[0..3].upcase
        year_month = (created_at || Time.current).strftime("%Y-%m")

        [ref, class_identifier, year_month, id].join("-")
      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

4 entries across 4 versions & 1 rubygems

Version Path
decidim-core-0.3.2 lib/decidim/has_reference.rb
decidim-core-0.3.1 lib/decidim/has_reference.rb
decidim-core-0.3.0 lib/decidim/has_reference.rb
decidim-core-0.2.0 lib/decidim/has_reference.rb