Sha256: b5c1370f894e44b1399a11fc20c46c9618ee7a453617757f0d8288e83616dab0

Contents?: true

Size: 1.74 KB

Versions: 13

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module Decidim
  module Forms
    # The data store for a Questionnaire in the Decidim::Forms component.
    class Questionnaire < Forms::ApplicationRecord
      include Decidim::Templates::Templatable if defined? Decidim::Templates::Templatable
      include Decidim::Publicable
      include Decidim::TranslatableResource
      include Decidim::Traceable

      translatable_fields :title, :description, :tos
      belongs_to :questionnaire_for, polymorphic: true

      has_many :questions, -> { order(:position) }, class_name: "Question", foreign_key: "decidim_questionnaire_id", dependent: :destroy
      has_many :answers, class_name: "Answer", foreign_key: "decidim_questionnaire_id", dependent: :destroy

      after_initialize :set_default_salt

      attr_accessor :questionnaire_template_id

      # Public: returns whether the questionnaire questions can be modified or not.
      def questions_editable?
        has_component = questionnaire_for.respond_to? :component
        (has_component && !questionnaire_for.component.published?) || answers.empty?
      end

      # Public: returns whether the questionnaire is answered by the user or not.
      def answered_by?(user)
        query = user.is_a?(String) ? { session_token: user } : { user: }
        answers.where(query).any? if questions.present?
      end

      def pristine?
        created_at.to_i == updated_at.to_i && questions.empty?
      end

      def self.log_presenter_class_for(_log)
        Decidim::Forms::AdminLog::QuestionnairePresenter
      end

      private

      # salt is used to generate secure hash in anonymous answers
      def set_default_salt
        return unless defined?(salt)

        self.salt ||= Tokenizer.random_salt
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-forms-0.29.1 app/models/decidim/forms/questionnaire.rb
decidim-forms-0.28.4 app/models/decidim/forms/questionnaire.rb
decidim-forms-0.29.0 app/models/decidim/forms/questionnaire.rb
decidim-forms-0.28.3 app/models/decidim/forms/questionnaire.rb
decidim-forms-0.29.0.rc4 app/models/decidim/forms/questionnaire.rb
decidim-forms-0.29.0.rc3 app/models/decidim/forms/questionnaire.rb
decidim-forms-0.29.0.rc2 app/models/decidim/forms/questionnaire.rb
decidim-forms-0.29.0.rc1 app/models/decidim/forms/questionnaire.rb
decidim-forms-0.28.2 app/models/decidim/forms/questionnaire.rb
decidim-forms-0.28.1 app/models/decidim/forms/questionnaire.rb
decidim-forms-0.28.0 app/models/decidim/forms/questionnaire.rb
decidim-forms-0.28.0.rc5 app/models/decidim/forms/questionnaire.rb
decidim-forms-0.28.0.rc4 app/models/decidim/forms/questionnaire.rb