Class: Decidim::TimeTracker::TimeTrackerActivityQuestionnaireAnswersSerializer
- Inherits:
-
Exporters::Serializer
- Object
- Exporters::Serializer
- Decidim::TimeTracker::TimeTrackerActivityQuestionnaireAnswersSerializer
- Includes:
- Decidim::TranslationsHelper
- Defined in:
- lib/decidim/time_tracker/time_tracker_activity_questionnaire_answers_serializer.rb
Overview
This class serializes the answers given by a User for questionnaire so can be exported to CSV, JSON or other formats.
Instance Method Summary collapse
-
#initialize(answers) ⇒ TimeTrackerActivityQuestionnaireAnswersSerializer
constructor
Public: Initializes the serializer with a collection of Answers.
-
#serialize ⇒ Object
Public: Exports a hash with the serialized data for the user answers.
Constructor Details
#initialize(answers) ⇒ TimeTrackerActivityQuestionnaireAnswersSerializer
Public: Initializes the serializer with a collection of Answers.
11 12 13 |
# File 'lib/decidim/time_tracker/time_tracker_activity_questionnaire_answers_serializer.rb', line 11 def initialize(answers) @answers = answers end |
Instance Method Details
#serialize ⇒ Object
Public: Exports a hash with the serialized data for the user answers.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/decidim/time_tracker/time_tracker_activity_questionnaire_answers_serializer.rb', line 16 def serialize @answers.each_with_index.inject({}) do |serialized, (answer, idx)| serialized.update( answer_translated_attribute_name(:id) => answer.id, answer_translated_attribute_name(:created_at) => answer.created_at.to_s(:db), answer_translated_attribute_name(:ip_hash) => answer.ip_hash, answer_translated_attribute_name(:user_status) => answer_translated_attribute_name(answer.decidim_user_id.present? ? "registered" : "unregistered"), answer_translated_attribute_name(:task_id) => task_for(answer).id, answer_translated_attribute_name(:task_name) => task_for(answer).name, answer_translated_attribute_name(:activity_id) => activity_for(answer).id, answer_translated_attribute_name(:activity_description) => activity_for(answer).description, "#{idx + 1}. #{translated_attribute(answer.question.body)}" => normalize_body(answer) ) end end |