From 428644cd3e445eb57908053b476c627ed8a846b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 11 Nov 2020 11:39:10 +0100 Subject: [PATCH] Add SDG goal model Since data for this model (title and description) is not generated in CONSUL but by the United Nations, we aren't storing it in the database but in our YAML translation files. The reasoning is as follows. Suppose that, a few months after CONSUL gets SDG support, a new language is added to CONSUL. With YAML files, getting the texts in the new language would mean updating CONSUL to include the new language. But if we store these texts in the database, it means we have to update the databases of all existing CONSUL installations, either each installation by themselves (duplicating efforts) or running a rake task (which we would have to write each time). So we believe using translations works better in this case. We're still storing records in the database with the code, so they can be easily referenced via `has_many` or `has_many :through` associations. --- app/models/sdg.rb | 5 +++ app/models/sdg/goal.rb | 11 +++++++ config/initializers/inflections.rb | 1 + config/locales/en/sdg.yml | 6 ++++ config/locales/es/sdg.yml | 6 ++++ db/migrate/20201111095452_create_sdg_goals.rb | 10 ++++++ db/schema.rb | 9 +++++- spec/factories/sdg.rb | 5 +++ spec/models/sdg/goal_spec.rb | 31 +++++++++++++++++++ 9 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 app/models/sdg.rb create mode 100644 app/models/sdg/goal.rb create mode 100644 config/locales/en/sdg.yml create mode 100644 config/locales/es/sdg.yml create mode 100644 db/migrate/20201111095452_create_sdg_goals.rb create mode 100644 spec/factories/sdg.rb create mode 100644 spec/models/sdg/goal_spec.rb diff --git a/app/models/sdg.rb b/app/models/sdg.rb new file mode 100644 index 000000000..67c0b0fcd --- /dev/null +++ b/app/models/sdg.rb @@ -0,0 +1,5 @@ +module SDG + def self.table_name_prefix + "sdg_" + end +end diff --git a/app/models/sdg/goal.rb b/app/models/sdg/goal.rb new file mode 100644 index 000000000..7ff570cc8 --- /dev/null +++ b/app/models/sdg/goal.rb @@ -0,0 +1,11 @@ +class SDG::Goal < ApplicationRecord + validates :code, presence: true, uniqueness: true + + def title + I18n.t("sdg.goals.goal_#{code}.title") + end + + def description + I18n.t("sdg.goals.goal_#{code}.description") + end +end diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index f17a84b39..e95cdbca7 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -17,5 +17,6 @@ ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.plural(/^(\d+)$/i, '\1') + inflect.acronym "SDG" inflect.irregular "organización", "organizaciones" end diff --git a/config/locales/en/sdg.yml b/config/locales/en/sdg.yml new file mode 100644 index 000000000..44eb51fee --- /dev/null +++ b/config/locales/en/sdg.yml @@ -0,0 +1,6 @@ +en: + sdg: + goals: + goal_1: + title: "No Poverty" + description: "End poverty in all its forms, everywhere." diff --git a/config/locales/es/sdg.yml b/config/locales/es/sdg.yml new file mode 100644 index 000000000..4f13d07ab --- /dev/null +++ b/config/locales/es/sdg.yml @@ -0,0 +1,6 @@ +es: + sdg: + goals: + goal_1: + title: "Fin de la pobreza" + description: "Poner fin a la pobreza en todas sus formas en todo el mundo." diff --git a/db/migrate/20201111095452_create_sdg_goals.rb b/db/migrate/20201111095452_create_sdg_goals.rb new file mode 100644 index 000000000..8079de4cb --- /dev/null +++ b/db/migrate/20201111095452_create_sdg_goals.rb @@ -0,0 +1,10 @@ +class CreateSDGGoals < ActiveRecord::Migration[5.2] + def change + create_table :sdg_goals do |t| + t.integer :code, null: false + t.timestamps + + t.index :code, unique: true + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 31ebc6540..6d7a9a4a1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_09_08_084257) do +ActiveRecord::Schema.define(version: 2020_11_11_095452) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" @@ -1297,6 +1297,13 @@ ActiveRecord::Schema.define(version: 2020_09_08_084257) do t.index ["process_type", "process_id"], name: "index_reports_on_process_type_and_process_id" end + create_table "sdg_goals", force: :cascade do |t| + t.integer "code", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["code"], name: "index_sdg_goals_on_code", unique: true + end + create_table "settings", id: :serial, force: :cascade do |t| t.string "key" t.string "value" diff --git a/spec/factories/sdg.rb b/spec/factories/sdg.rb new file mode 100644 index 000000000..75d911a62 --- /dev/null +++ b/spec/factories/sdg.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :sdg_goal, class: "SDG::Goal" do + sequence(:code) { |n| n } + end +end diff --git a/spec/models/sdg/goal_spec.rb b/spec/models/sdg/goal_spec.rb new file mode 100644 index 000000000..e9693e5a4 --- /dev/null +++ b/spec/models/sdg/goal_spec.rb @@ -0,0 +1,31 @@ +require "rails_helper" + +describe SDG::Goal do + it "is valid" do + expect(build(:sdg_goal)).to be_valid + end + + it "is not valid without a code" do + expect(build(:sdg_goal, code: nil)).not_to be_valid + end + + it "translates title" do + goal = SDG::Goal.where(code: "1").first_or_create! + + expect(goal.title).to eq "No Poverty" + + I18n.with_locale(:es) do + expect(goal.title).to eq "Fin de la pobreza" + end + end + + it "translates description" do + goal = SDG::Goal.where(code: "1").first_or_create! + + expect(goal.description).to eq "End poverty in all its forms, everywhere." + + I18n.with_locale(:es) do + expect(goal.description).to eq "Poner fin a la pobreza en todas sus formas en todo el mundo." + end + end +end