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:
5
app/models/sdg.rb
Normal file
5
app/models/sdg.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
module SDG
|
||||
def self.table_name_prefix
|
||||
"sdg_"
|
||||
end
|
||||
end
|
||||
11
app/models/sdg/goal.rb
Normal file
11
app/models/sdg/goal.rb
Normal 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
|
||||
@@ -17,5 +17,6 @@
|
||||
|
||||
ActiveSupport::Inflector.inflections(:en) do |inflect|
|
||||
inflect.plural(/^(\d+)$/i, '\1')
|
||||
inflect.acronym "SDG"
|
||||
inflect.irregular "organización", "organizaciones"
|
||||
end
|
||||
|
||||
6
config/locales/en/sdg.yml
Normal file
6
config/locales/en/sdg.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
en:
|
||||
sdg:
|
||||
goals:
|
||||
goal_1:
|
||||
title: "No Poverty"
|
||||
description: "End poverty in all its forms, everywhere."
|
||||
6
config/locales/es/sdg.yml
Normal file
6
config/locales/es/sdg.yml
Normal 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."
|
||||
10
db/migrate/20201111095452_create_sdg_goals.rb
Normal file
10
db/migrate/20201111095452_create_sdg_goals.rb
Normal 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
|
||||
@@ -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"
|
||||
|
||||
5
spec/factories/sdg.rb
Normal file
5
spec/factories/sdg.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
FactoryBot.define do
|
||||
factory :sdg_goal, class: "SDG::Goal" do
|
||||
sequence(:code) { |n| n }
|
||||
end
|
||||
end
|
||||
31
spec/models/sdg/goal_spec.rb
Normal file
31
spec/models/sdg/goal_spec.rb
Normal 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
|
||||
Reference in New Issue
Block a user