Sha256: 18dc6be900310e3a729affa684be136ac6ecce134cdb14af33dba3397bc5ecdc

Contents?: true

Size: 1.9 KB

Versions: 78

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

class CreateDecidimSurveyAnswerChoices < ActiveRecord::Migration[5.1]
  class SurveyAnswer < ApplicationRecord
    self.table_name = :decidim_surveys_survey_answers
  end

  class SurveyAnswerChoice < ApplicationRecord
    self.table_name = :decidim_surveys_survey_answer_choices
  end

  class SurveyQuestion < ApplicationRecord
    self.table_name = :decidim_surveys_survey_questions
  end

  class SurveyAnswerOption < ApplicationRecord
    self.table_name = :decidim_surveys_survey_answer_options
  end

  def up
    create_table :decidim_surveys_survey_answer_choices do |t|
      t.references :decidim_survey_answer, index: { name: "index_decidim_surveys_answer_choices_answer_id" }
      t.references :decidim_survey_answer_option, index: { name: "index_decidim_surveys_answer_choices_answer_option_id" }
      t.jsonb :body
    end

    SurveyAnswer.find_each do |answer|
      question = SurveyQuestion.find_by(id: answer.decidim_survey_question_id)
      choices = SurveyAnswerChoice.where(decidim_survey_answer_id: answer.id)

      choices.each do |answer_choice|
        answer_options = SurveyAnswerOption.where(decidim_survey_question_id: question.id)

        answer_option = answer_options.find do |option|
          option.body.has_value?(answer_choice)
        end

        SurveyAnswerChoice.create!(
          decidim_survey_answer_id: answer.id,
          decidim_survey_answer_option_id: answer_option.id,
          body: answer_choice
        )
      end
    end

    remove_column :decidim_surveys_survey_answers, :choices
  end

  def down
    add_column :decidim_surveys_survey_answers, :choices, :jsonb, default: []

    SurveyAnswerChoice.find_each do |answer_choice|
      answer = SurveyAnswer.find_by(id: answer_choice.decidim_survey_answer_id)

      answer.choices << answer_choice.body

      answer.save!
    end

    drop_table :decidim_surveys_survey_answer_choices
  end
end

Version data entries

78 entries across 78 versions & 1 rubygems

Version Path
decidim-surveys-0.29.1 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.28.4 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.27.9 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.29.0 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.28.3 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.27.8 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.29.0.rc4 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.29.0.rc3 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.29.0.rc2 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.29.0.rc1 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.28.2 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.27.7 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.28.1 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.27.6 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.26.10 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.26.9 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.28.0 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.27.5 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.28.0.rc5 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb
decidim-surveys-0.28.0.rc4 db/migrate/20180405015147_create_decidim_survey_answer_choices.rb