Sha256: 5a71de70c8cd6b7d00f2140a1a17e1592fe379f52663eadccf42c204c9534204

Contents?: true

Size: 871 Bytes

Versions: 48

Compression:

Stored size: 871 Bytes

Contents

# frozen_string_literal: true

module Decidim
  module Surveys
    # This class holds a Form to update survey unswers from Decidim's public page
    class SurveyAnswerForm < Decidim::Form
      attribute :question_id, String
      attribute :body, String

      validates :body, presence: true, if: -> { question.mandatory? }
      validate :body_not_blank, if: -> { question.mandatory? }

      def question
        @question ||= survey.questions.find(question_id)
      end

      # Public: Map the correct fields.
      #
      # Returns nothing.
      def map_model(model)
        self.question_id = model.id
      end

      private

      def survey
        @survey ||= Survey.where(feature: current_feature).first
      end

      def body_not_blank
        return if body.nil?
        errors.add("body", :blank) if body.all?(&:blank?)
      end
    end
  end
end

Version data entries

48 entries across 48 versions & 2 rubygems

Version Path
decidim-surveys-0.8.4 app/forms/decidim/surveys/survey_answer_form.rb
decidim-surveys-0.8.3 app/forms/decidim/surveys/survey_answer_form.rb
decidim-surveys-0.8.2 app/forms/decidim/surveys/survey_answer_form.rb
decidim-surveys-0.8.1 app/forms/decidim/surveys/survey_answer_form.rb
decidim-surveys-0.8.0 app/forms/decidim/surveys/survey_answer_form.rb
decidim-surveys-0.7.4 app/forms/decidim/surveys/survey_answer_form.rb
decidim-surveys-0.7.3 app/forms/decidim/surveys/survey_answer_form.rb
decidim-surveys-0.7.2 app/forms/decidim/surveys/survey_answer_form.rb
decidim-surveys-0.7.1 app/forms/decidim/surveys/survey_answer_form.rb
decidim-surveys-0.7.0 app/forms/decidim/surveys/survey_answer_form.rb
decidim-surveys-0.6.8 app/forms/decidim/surveys/survey_answer_form.rb
decidim-0.6.8 decidim-surveys/app/forms/decidim/surveys/survey_answer_form.rb
decidim-surveys-0.6.7 app/forms/decidim/surveys/survey_answer_form.rb
decidim-0.6.7 decidim-surveys/app/forms/decidim/surveys/survey_answer_form.rb
decidim-surveys-0.6.6 app/forms/decidim/surveys/survey_answer_form.rb
decidim-0.6.6 decidim-surveys/app/forms/decidim/surveys/survey_answer_form.rb
decidim-surveys-0.6.5 app/forms/decidim/surveys/survey_answer_form.rb
decidim-0.6.5 decidim-surveys/app/forms/decidim/surveys/survey_answer_form.rb
decidim-surveys-0.6.4 app/forms/decidim/surveys/survey_answer_form.rb
decidim-0.6.4 decidim-surveys/app/forms/decidim/surveys/survey_answer_form.rb