Add SDG::Relation model

This commit is contained in:
Javi Martín
2020-11-17 21:32:09 +01:00
parent ebcd64ad1a
commit 1740e0ba66
8 changed files with 81 additions and 1 deletions

View File

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

View File

@@ -0,0 +1,7 @@
module SDG::Related
extend ActiveSupport::Concern
included do
has_many :relations, as: :related_sdg, dependent: :destroy
end
end

View File

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

View File

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

View File

@@ -1,5 +1,6 @@
class SDG::Target < ApplicationRecord
include Comparable
include SDG::Related
validates :code, presence: true, uniqueness: true
validates :goal, presence: true

View File

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

View File

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

View File

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