Add SDG phase model

The purpose of this model will be to have different sections in the SDG
index.
This commit is contained in:
Javi Martín
2021-01-07 19:23:55 +01:00
parent ee29ca43a5
commit 13f95e9419
9 changed files with 83 additions and 1 deletions

View File

@@ -29,6 +29,7 @@ module Abilities
can [:search, :comments, :read, :create, :new_comment], Legislation::Annotation
can :read, ::SDG::Goal
can :read, ::SDG::Phase
end
end
end

8
app/models/sdg/phase.rb Normal file
View File

@@ -0,0 +1,8 @@
class SDG::Phase < ApplicationRecord
enum kind: %w[sensitization planning monitoring]
validates :kind, presence: true, uniqueness: true
def self.[](kind)
find_by!(kind: kind)
end
end

View File

@@ -0,0 +1,9 @@
class CreateSDGPhases < ActiveRecord::Migration[5.2]
def change
create_table :sdg_phases do |t|
t.integer :kind, null: false
t.index :kind, 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: 2021_01_06_132909) do
ActiveRecord::Schema.define(version: 2021_01_07_125458) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
@@ -1333,6 +1333,13 @@ ActiveRecord::Schema.define(version: 2021_01_06_132909) do
t.index ["user_id"], name: "index_sdg_managers_on_user_id", unique: true
end
create_table "sdg_phases", force: :cascade do |t|
t.integer "kind", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["kind"], name: "index_sdg_phases_on_kind", unique: true
end
create_table "sdg_relations", force: :cascade do |t|
t.string "related_sdg_type"
t.bigint "related_sdg_id"

View File

@@ -23,3 +23,5 @@ end
].each do |code|
SDG::Target.where(code: code, goal: SDG::Goal.find_by!(code: code.split(".").first)).first_or_create!
end
SDG::Phase.kinds.values.each { |kind| SDG::Phase.where(kind: kind).first_or_create! }

View File

@@ -14,4 +14,8 @@ FactoryBot.define do
target { SDG::Target[code.rpartition(".").first] }
end
factory :sdg_phase, class: "SDG::Phase" do
kind { :sensitization }
end
end

View File

@@ -9,27 +9,33 @@ describe "rake db:load_sdg" do
it "populates empty databases and assigns targets correctly" do
SDG::Goal.destroy_all
SDG::Phase.destroy_all
run_rake_task
expect(SDG::Goal.count).to eq 17
expect(SDG::Target.count).to eq 169
expect(SDG::Target["17.1"].goal.code).to eq 17
expect(SDG::Phase.count).to eq 3
end
it "does not create additional records on populated databases" do
expect(SDG::Goal.count).to eq 17
expect(SDG::Target.count).to eq 169
expect(SDG::Phase.count).to eq 3
goal_id = SDG::Goal.last.id
target_id = SDG::Target.last.id
phase_id = SDG::Phase.last.id
run_rake_task
expect(SDG::Goal.count).to eq 17
expect(SDG::Target.count).to eq 169
expect(SDG::Phase.count).to eq 3
expect(SDG::Goal.last.id).to eq goal_id
expect(SDG::Target.last.id).to eq target_id
expect(SDG::Phase.last.id).to eq phase_id
end
end

View File

@@ -55,6 +55,7 @@ describe Abilities::Everyone do
it { should be_able_to(:read, SDG::Goal) }
it { should_not be_able_to(:read, SDG::Target) }
it { should be_able_to(:read, SDG::Phase) }
it { should_not be_able_to(:read, SDG::Manager) }
it { should_not be_able_to(:create, SDG::Manager) }

View File

@@ -0,0 +1,44 @@
require "rails_helper"
describe SDG::Phase do
let(:phase) { build(:sdg_phase) }
before { SDG::Phase["sensitization"].destroy }
it "is valid with a valid kind" do
phase.kind = "sensitization"
expect(phase).to be_valid
end
it "is not valid without a kind" do
phase.kind = nil
expect(phase).not_to be_valid
end
it "is not valid with a duplicate kind" do
phase.kind = "planning"
expect(phase).not_to be_valid
end
it "is not valid with a custom kind" do
expect { phase.kind = "improvement" }.to raise_exception(ArgumentError)
end
describe ".[]" do
it "finds existing phases by kind" do
expect(SDG::Phase["monitoring"].kind).to eq "monitoring"
end
it "raises an exception on empty databases" do
SDG::Phase["monitoring"].destroy!
expect { SDG::Phase["monitoring"] }.to raise_exception ActiveRecord::RecordNotFound
end
it "raises an exception for non-existing kinds" do
expect { SDG::Phase["improvement"] }.to raise_exception ActiveRecord::StatementInvalid
end
end
end