Add SDG::Relation model
This commit is contained in:
7
app/models/concerns/sdg/relatable.rb
Normal file
7
app/models/concerns/sdg/relatable.rb
Normal 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
|
||||||
7
app/models/concerns/sdg/related.rb
Normal file
7
app/models/concerns/sdg/related.rb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
module SDG::Related
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
included do
|
||||||
|
has_many :relations, as: :related_sdg, dependent: :destroy
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
class SDG::Goal < ApplicationRecord
|
class SDG::Goal < ApplicationRecord
|
||||||
|
include SDG::Related
|
||||||
|
|
||||||
validates :code, presence: true, uniqueness: true, inclusion: { in: 1..17 }
|
validates :code, presence: true, uniqueness: true, inclusion: { in: 1..17 }
|
||||||
|
|
||||||
has_many :targets, dependent: :destroy
|
has_many :targets, dependent: :destroy
|
||||||
|
|||||||
6
app/models/sdg/relation.rb
Normal file
6
app/models/sdg/relation.rb
Normal 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
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
class SDG::Target < ApplicationRecord
|
class SDG::Target < ApplicationRecord
|
||||||
include Comparable
|
include Comparable
|
||||||
|
include SDG::Related
|
||||||
|
|
||||||
validates :code, presence: true, uniqueness: true
|
validates :code, presence: true, uniqueness: true
|
||||||
validates :goal, presence: true
|
validates :goal, presence: true
|
||||||
|
|||||||
12
db/migrate/20201117200945_create_sdg_relations.rb
Normal file
12
db/migrate/20201117200945_create_sdg_relations.rb
Normal 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
|
||||||
14
db/schema.rb
14
db/schema.rb
@@ -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_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
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "pg_trgm"
|
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
|
t.index ["code"], name: "index_sdg_goals_on_code", unique: true
|
||||||
end
|
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|
|
create_table "sdg_targets", force: :cascade do |t|
|
||||||
t.bigint "goal_id"
|
t.bigint "goal_id"
|
||||||
t.string "code", null: false
|
t.string "code", null: false
|
||||||
|
|||||||
33
spec/models/sdg/relation_spec.rb
Normal file
33
spec/models/sdg/relation_spec.rb
Normal 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
|
||||||
Reference in New Issue
Block a user