Sha256: f7c47b644ef78479c7ab7652eda8d38dc7596c16d917c1a41bca3cf42c5519ab

Contents?: true

Size: 1.68 KB

Versions: 13

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Decidim
  module Forms
    module Admin
      #
      # Presenter for questionnaire answer
      #
      class QuestionnaireAnswerPresenter < Rectify::Presenter
        include Decidim::TranslatableAttributes

        attribute :answer, Decidim::Forms::Answer

        def question
          translated_attribute(answer.question.body)
        end

        def body
          return answer.body if answer.body.present?
          return attachments if answer.attachments.any?
          return "-" if answer.choices.empty?

          choices = answer.choices.map do |choice|
            choice.try(:custom_body) || choice.try(:body)
          end

          return choices.first if answer.question.question_type == "single_option"

          content_tag(:ul) do
            safe_join(choices.map { |c| choice(c) })
          end
        end

        def attachments
          content_tag(:ul) do
            safe_join(answer.attachments.map { |a| pretty_attachment(a) })
          end
        end

        private

        def pretty_attachment(attachment)
          # rubocop:disable Style/StringConcatenation
          # Interpolating strings that are `html_safe` is problematic with Rails.
          content_tag :li do
            link_to(translated_attribute(attachment.title), attachment.url) +
              " " +
              content_tag(:small) do
                "#{attachment.file_type} #{number_to_human_size(attachment.file_size)}"
              end
          end
          # rubocop:enable Style/StringConcatenation
        end

        def choice(choice_body)
          content_tag :li do
            choice_body
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-forms-0.25.2 app/presenters/decidim/forms/admin/questionnaire_answer_presenter.rb
decidim-forms-0.25.1 app/presenters/decidim/forms/admin/questionnaire_answer_presenter.rb
decidim-forms-0.25.0 app/presenters/decidim/forms/admin/questionnaire_answer_presenter.rb
decidim-forms-0.25.0.rc4 app/presenters/decidim/forms/admin/questionnaire_answer_presenter.rb
decidim-forms-0.25.0.rc3 app/presenters/decidim/forms/admin/questionnaire_answer_presenter.rb
decidim-forms-0.25.0.rc2 app/presenters/decidim/forms/admin/questionnaire_answer_presenter.rb
decidim-forms-0.25.0.rc1 app/presenters/decidim/forms/admin/questionnaire_answer_presenter.rb
decidim-forms-0.24.3 app/presenters/decidim/forms/admin/questionnaire_answer_presenter.rb
decidim-forms-0.24.2 app/presenters/decidim/forms/admin/questionnaire_answer_presenter.rb
decidim-forms-0.24.1 app/presenters/decidim/forms/admin/questionnaire_answer_presenter.rb
decidim-forms-0.24.0 app/presenters/decidim/forms/admin/questionnaire_answer_presenter.rb
decidim-forms-0.24.0.rc2 app/presenters/decidim/forms/admin/questionnaire_answer_presenter.rb
decidim-forms-0.24.0.rc1 app/presenters/decidim/forms/admin/questionnaire_answer_presenter.rb