diff --git a/app/models/concerns/sdg/relatable.rb b/app/models/concerns/sdg/relatable.rb new file mode 100644 index 000000000..e886616fc --- /dev/null +++ b/app/models/concerns/sdg/relatable.rb @@ -0,0 +1,7 @@ +module SDG::Relatable + extend ActiveSupport::Concern + + included do + has_many :sdg_relations, as: :relatable, dependent: :destroy, class_name: "SDG::Relation" + end +end diff --git a/app/models/concerns/sdg/related.rb b/app/models/concerns/sdg/related.rb new file mode 100644 index 000000000..ac96ff325 --- /dev/null +++ b/app/models/concerns/sdg/related.rb @@ -0,0 +1,7 @@ +module SDG::Related + extend ActiveSupport::Concern + + included do + has_many :relations, as: :related_sdg, dependent: :destroy + end +end diff --git a/app/models/sdg/goal.rb b/app/models/sdg/goal.rb index 940f7a7d1..bd26438c8 100644 --- a/app/models/sdg/goal.rb +++ b/app/models/sdg/goal.rb @@ -1,4 +1,6 @@ class SDG::Goal < ApplicationRecord + include SDG::Related + validates :code, presence: true, uniqueness: true, inclusion: { in: 1..17 } has_many :targets, dependent: :destroy diff --git a/app/models/sdg/relation.rb b/app/models/sdg/relation.rb new file mode 100644 index 000000000..0148bea46 --- /dev/null +++ b/app/models/sdg/relation.rb @@ -0,0 +1,6 @@ +class SDG::Relation < ApplicationRecord + validates :related_sdg_id, uniqueness: { scope: [:related_sdg_type, :relatable_id, :relatable_type] } + + belongs_to :relatable, polymorphic: true, optional: false + belongs_to :related_sdg, polymorphic: true, optional: false +end diff --git a/app/models/sdg/target.rb b/app/models/sdg/target.rb index 72568490d..ad165e280 100644 --- a/app/models/sdg/target.rb +++ b/app/models/sdg/target.rb @@ -1,5 +1,6 @@ class SDG::Target < ApplicationRecord include Comparable + include SDG::Related validates :code, presence: true, uniqueness: true validates :goal, presence: true diff --git a/db/migrate/20201117200945_create_sdg_relations.rb b/db/migrate/20201117200945_create_sdg_relations.rb new file mode 100644 index 000000000..55415c78c --- /dev/null +++ b/db/migrate/20201117200945_create_sdg_relations.rb @@ -0,0 +1,12 @@ +class CreateSDGRelations < ActiveRecord::Migration[5.2] + def change + create_table :sdg_relations do |t| + t.references :related_sdg, polymorphic: true + t.references :relatable, polymorphic: true + + t.index [:related_sdg_id, :related_sdg_type, :relatable_id, :relatable_type], name: "sdg_relations_unique", unique: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5cb3ed956..5b1944278 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_11_12_155047) do +ActiveRecord::Schema.define(version: 2020_11_17_200945) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" @@ -1304,6 +1304,18 @@ ActiveRecord::Schema.define(version: 2020_11_12_155047) do t.index ["code"], name: "index_sdg_goals_on_code", unique: true end + create_table "sdg_relations", force: :cascade do |t| + t.string "related_sdg_type" + t.bigint "related_sdg_id" + t.string "relatable_type" + t.bigint "relatable_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["relatable_type", "relatable_id"], name: "index_sdg_relations_on_relatable_type_and_relatable_id" + t.index ["related_sdg_id", "related_sdg_type", "relatable_id", "relatable_type"], name: "sdg_relations_unique", unique: true + t.index ["related_sdg_type", "related_sdg_id"], name: "index_sdg_relations_on_related_sdg_type_and_related_sdg_id" + end + create_table "sdg_targets", force: :cascade do |t| t.bigint "goal_id" t.string "code", null: false diff --git a/spec/models/sdg/relation_spec.rb b/spec/models/sdg/relation_spec.rb new file mode 100644 index 000000000..d2da87fdd --- /dev/null +++ b/spec/models/sdg/relation_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +describe SDG::Relation do + describe "Validations" do + it "is valid with a related SDG and a relatable model" do + relation = SDG::Relation.new(related_sdg: SDG::Goal[1], relatable: create(:proposal)) + + expect(relation).to be_valid + end + + it "is not valid without a related SDG" do + relation = SDG::Relation.new(relatable: create(:proposal)) + + expect(relation).not_to be_valid + end + + it "is not valid without a relatable model" do + relation = SDG::Relation.new(related_sdg: SDG::Goal[1]) + + expect(relation).not_to be_valid + end + + it "is not valid when a relation already exists" do + proposal = create(:proposal) + goal = SDG::Goal[1] + + SDG::Relation.create!(related_sdg: goal, relatable: proposal) + relation = SDG::Relation.new(related_sdg: goal, relatable: proposal) + + expect(relation).not_to be_valid + end + end +end