Sha256: c8634ec8202810b2dc5b57ad775a44d2dc35846774b7256aac0af397d5ac8736

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Decidim
  module DecidimAwesome
    # This class adds some attributes to the proposal model in a different table
    # in particular, it adds a private_body field that should be encrypted
    # private_body is not translatable, nor is intended to be as it won't be shown to the public
    class ProposalExtraField < ApplicationRecord
      include Decidim::RecordEncryptor

      self.table_name = "decidim_awesome_proposal_extra_fields"

      belongs_to :proposal, foreign_key: "decidim_proposal_id", foreign_type: "decidim_proposal_type", polymorphic: true

      encrypt_attribute :private_body, type: :string

      after_initialize :store_private_body
      before_save :update_private_body_updated_at

      # validate not more than one extra field can be associated to a proposal
      # validates :proposal, uniqueness: true
      validate :no_more_than_one_extra_field

      private

      def store_private_body
        @initial_private_body = private_body
      end

      # using private_body_changed? does not sufice as the encrypted value is always updated on saving
      def update_private_body_updated_at
        if private_body != @initial_private_body
          self.private_body_updated_at = Time.current
          @initial_private_body = private_body
        end
      end

      def no_more_than_one_extra_field
        return unless ProposalExtraField.where(proposal:).where.not(id:).exists?

        errors.add(:proposal, :invalid)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
decidim-decidim_awesome-0.11.2 app/models/decidim/decidim_awesome/proposal_extra_field.rb
decidim-decidim_awesome-0.11.1 app/models/decidim/decidim_awesome/proposal_extra_field.rb