diff --git a/backend/Gemfile b/backend/Gemfile index 667a564f..782b1232 100644 --- a/backend/Gemfile +++ b/backend/Gemfile @@ -9,6 +9,7 @@ gem 'bootsnap', require: false gem 'devise' gem 'devise-jwt' gem 'faraday' +gem 'http_accept_language' gem 'jbuilder' gem 'newrelic_rpm' gem 'pg', '~> 1.1' diff --git a/backend/Gemfile.lock b/backend/Gemfile.lock index 6033d17f..266c9177 100644 --- a/backend/Gemfile.lock +++ b/backend/Gemfile.lock @@ -115,6 +115,7 @@ GEM faraday-net_http (3.0.2) globalid (1.1.0) activesupport (>= 5.0) + http_accept_language (2.1.1) i18n (1.14.1) concurrent-ruby (~> 1.0) io-console (0.6.0) @@ -287,6 +288,7 @@ DEPENDENCIES devise-jwt factory_bot_rails faraday + http_accept_language jbuilder newrelic_rpm pg (~> 1.1) diff --git a/backend/app/controllers/surveys_controller.rb b/backend/app/controllers/surveys_controller.rb index 59dbec4e..7e3d6d6a 100644 --- a/backend/app/controllers/surveys_controller.rb +++ b/backend/app/controllers/surveys_controller.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class SurveysController < ApplicationController + AVAILABLE_LANGUAGES = %w(en es) before_action :set_survey, only: %i[show edit update destroy] # GET /surveys or /surveys.json @@ -9,7 +10,9 @@ def index end # GET /surveys/1 or /surveys/1.json - def show; end + def show + @language = http_accept_language.preferred_language_from(AVAILABLE_LANGUAGES) + end # GET /surveys/new def new diff --git a/backend/app/models/localized_survey_question.rb b/backend/app/models/localized_survey_question.rb new file mode 100644 index 00000000..484d6a3f --- /dev/null +++ b/backend/app/models/localized_survey_question.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class LocalizedSurveyQuestion < ApplicationRecord + belongs_to :survey_question +end diff --git a/backend/app/models/survey_question.rb b/backend/app/models/survey_question.rb index 3a9b6bea..3b739aca 100644 --- a/backend/app/models/survey_question.rb +++ b/backend/app/models/survey_question.rb @@ -3,6 +3,20 @@ class SurveyQuestion < ApplicationRecord belongs_to :survey enum response_type: { radio: 0, text: 1 } + has_many :localized_survey_questions + accepts_nested_attributes_for :localized_survey_questions validates :display_order, uniqueness: { scope: :survey_id } + + after_initialize do + @language ||= 'en' + end + + def text + localized_survey_questions.find_by(language_code: @language_code).text + end + + def response_options + localized_survey_questions.find_by(language_code: @language_code).response_options + end end diff --git a/backend/db/migrate/20230920000437_create_localized_survey_questions.rb b/backend/db/migrate/20230920000437_create_localized_survey_questions.rb new file mode 100644 index 00000000..75a2f224 --- /dev/null +++ b/backend/db/migrate/20230920000437_create_localized_survey_questions.rb @@ -0,0 +1,14 @@ +class CreateLocalizedSurveyQuestions < ActiveRecord::Migration[7.0] + def change + create_table :localized_survey_questions do |t| + t.string :language_code + t.text :text + t.string :response_options, array: true + t.references :survey_question + + t.timestamps + end + remove_column :survey_questions, :text, :text + remove_column :survey_questions, :response_options, :string, array: true + end +end diff --git a/backend/db/schema.rb b/backend/db/schema.rb index 08806692..d43e420f 100644 --- a/backend/db/schema.rb +++ b/backend/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_07_12_003916) do +ActiveRecord::Schema[7.0].define(version: 2023_09_20_000437) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -41,8 +41,6 @@ t.datetime "updated_at", null: false t.bigint "assignment_id" t.integer "visit_order" - t.integer "status", default: 0, null: false - t.boolean "user_added", default: false, null: false t.integer "bedroom_score" t.integer "floor_score" t.string "geometry" @@ -56,6 +54,8 @@ t.integer "total_score_x" t.string "yr_built_category" t.integer "yr_built_score" + t.integer "status", default: 0, null: false + t.boolean "user_added", default: false, null: false t.index ["assignment_id", "visit_order"], name: "index_homes_on_assignment_id_and_visit_order", unique: true t.index ["assignment_id"], name: "index_homes_on_assignment_id" end @@ -68,6 +68,16 @@ t.index ["jti"], name: "index_jwt_denylists_on_jti" end + create_table "localized_survey_questions", force: :cascade do |t| + t.string "language_code" + t.text "text" + t.string "response_options", array: true + t.bigint "survey_question_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["survey_question_id"], name: "index_localized_survey_questions_on_survey_question_id" + end + create_table "survey_answers", force: :cascade do |t| t.text "answer" t.bigint "survey_response_id", null: false @@ -79,14 +89,11 @@ end create_table "survey_questions", force: :cascade do |t| - t.text "text" t.integer "response_type" - t.string "response_options", array: true t.bigint "survey_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "display_order", default: 0, null: false - t.index ["response_options"], name: "index_survey_questions_on_response_options", using: :gin t.index ["survey_id", "display_order"], name: "index_survey_questions_on_survey_id_and_display_order", unique: true t.index ["survey_id"], name: "index_survey_questions_on_survey_id" end diff --git a/backend/db/seed_importer.rb b/backend/db/seed_importer.rb index 747cc890..4e760a9a 100644 --- a/backend/db/seed_importer.rb +++ b/backend/db/seed_importer.rb @@ -25,7 +25,17 @@ def import_seed_data(path, clustered_ordered_parcels) chunk.each do |data_hash| data_hash[:response_options] = (data_hash[:response_options].nil? ? [] : data_hash[:response_options].split('/', -1)) - question = SurveyQuestion.new(data_hash) + fixed_data_hash = { + display_order: data_hash[:display_order], + response_type: data_hash[:response_type], + localized_survey_questions: [ + LocalizedSurveyQuestion.new({ + text: data_hash[:text], + response_options: data_hash[:response_options], + language_code: 'en' + })] + } + question = SurveyQuestion.new(fixed_data_hash) question.survey = survey question.save! end