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.
This commit is contained in:
Javi Martín
2020-11-11 11:39:10 +01:00
parent e19bc594d8
commit 428644cd3e
9 changed files with 83 additions and 1 deletions

5
app/models/sdg.rb Normal file
View File

@@ -0,0 +1,5 @@
module SDG
def self.table_name_prefix
"sdg_"
end
end

11
app/models/sdg/goal.rb Normal file
View File

@@ -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

View File

@@ -17,5 +17,6 @@
ActiveSupport::Inflector.inflections(:en) do |inflect| ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.plural(/^(\d+)$/i, '\1') inflect.plural(/^(\d+)$/i, '\1')
inflect.acronym "SDG"
inflect.irregular "organización", "organizaciones" inflect.irregular "organización", "organizaciones"
end end

View File

@@ -0,0 +1,6 @@
en:
sdg:
goals:
goal_1:
title: "No Poverty"
description: "End poverty in all its forms, everywhere."

View File

@@ -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."

View File

@@ -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

View File

@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm" 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" t.index ["process_type", "process_id"], name: "index_reports_on_process_type_and_process_id"
end 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| create_table "settings", id: :serial, force: :cascade do |t|
t.string "key" t.string "key"
t.string "value" t.string "value"

5
spec/factories/sdg.rb Normal file
View File

@@ -0,0 +1,5 @@
FactoryBot.define do
factory :sdg_goal, class: "SDG::Goal" do
sequence(:code) { |n| n }
end
end

View File

@@ -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