Skip to content

Commit

Permalink
Add relation between Goal and LocalTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
taitus committed Jan 25, 2021
1 parent 5101e37 commit f048df8
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/models/sdg/goal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class SDG::Goal < ApplicationRecord
validates :code, presence: true, uniqueness: true, inclusion: { in: 1..17 }

has_many :targets, dependent: :destroy
has_many :local_targets, dependent: :destroy

def title
I18n.t("sdg.goals.goal_#{code}.title")
Expand Down
10 changes: 8 additions & 2 deletions app/models/sdg/local_target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ class SDG::LocalTarget < ApplicationRecord
include Comparable
include SDG::Related

delegate :goal, to: :target

translates :title, touch: true
translates :description, touch: true
include Globalizable
Expand All @@ -14,8 +12,12 @@ class SDG::LocalTarget < ApplicationRecord
validates :code, presence: true, uniqueness: true,
format: ->(local_target) { /\A#{local_target.target&.code}\.\d+/ }
validates :target, presence: true
validates :goal, presence: true

belongs_to :target
belongs_to :goal

before_validation :set_related_goal

def self.[](code)
find_by!(code: code)
Expand All @@ -40,4 +42,8 @@ def numeric_subcode
def subcode
code.split(".").last
end

def set_related_goal
self.goal ||= target&.goal
end
end
3 changes: 2 additions & 1 deletion db/dev_seeds/sdg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
local_target = SDG::LocalTarget.create!(code: "#{target.code}.#{n + 1}",
title: title,
description: description,
target: target)
target: target,
goal: target.goal)
random_locales.map do |locale|
Globalize.with_locale(locale) do
local_target.title = "Title for locale #{locale}"
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20210123100638_add_goals_to_local_targets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddGoalsToLocalTargets < ActiveRecord::Migration[5.2]
def change
add_reference :sdg_local_targets, :goal
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_01_07_125458) do
ActiveRecord::Schema.define(version: 2021_01_23_100638) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
Expand Down Expand Up @@ -1322,7 +1322,9 @@
t.string "code"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "goal_id"
t.index ["code"], name: "index_sdg_local_targets_on_code", unique: true
t.index ["goal_id"], name: "index_sdg_local_targets_on_goal_id"
t.index ["target_id"], name: "index_sdg_local_targets_on_target_id"
end

Expand Down
1 change: 1 addition & 0 deletions spec/factories/sdg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
sequence(:description) { |n| "Help for Local Target #{n}" }

target { SDG::Target[code.rpartition(".").first] }
goal { SDG::Goal[code.split(".")[0]] }
end

factory :sdg_phase, class: "SDG::Phase" do
Expand Down
11 changes: 10 additions & 1 deletion spec/models/sdg/local_target_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
end

it "is not valid without a code" do
expect(build(:sdg_local_target, code: nil, target: SDG::Target[1.1])).not_to be_valid
expect(build(:sdg_local_target, code: nil, target: SDG::Target[1.1], goal: SDG::Goal[1])).not_to be_valid
end

it "is not valid when code does not include associated target code" do
Expand Down Expand Up @@ -47,6 +47,15 @@
expect(build(:sdg_local_target, target: nil)).not_to be_valid
end

describe "#set_related_goal" do
it "before validation set related goal" do
local_target = build(:sdg_local_target, code: "1.1.1", goal: nil)

expect(local_target).to be_valid
expect(local_target.goal).to eq(SDG::Goal[1])
end
end

describe "#goal" do
it "returns the target goal" do
local_target = create(:sdg_local_target, code: "1.1.1")
Expand Down

0 comments on commit f048df8

Please sign in to comment.