Sha256: 1a26dfb11c8c9472a724be0f9894fff602337892106a45c1c46edd30b0d05268
Contents?: true
Size: 1.46 KB
Versions: 1
Compression:
Stored size: 1.46 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
decidim-core-0.1.0 | lib/decidim/has_reference.rb |