Skip to content

Commit

Permalink
WIP commit for run CI: Need refactor and add specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
taitus committed Jan 23, 2021
1 parent 24a35be commit 27efd28
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 16 deletions.
5 changes: 4 additions & 1 deletion app/components/sdg/related_list_selector_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def sdg_related_suggestions

def goals_and_targets
goals.map do |goal|
[goal, *goal.targets.sort]
global_targets = goal.targets
local_targets = SDG::LocalTarget.where(target: [global_targets])
targets = global_targets + local_targets
[goal, targets.sort]
end.flatten
end

Expand Down
5 changes: 5 additions & 0 deletions app/controllers/sdg_management/local_targets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def new
end

def create
add_related_goal
if @local_target.save
redirect_to sdg_management_local_targets_path, notice: t("sdg_management.local_targets.create.notice")
else
Expand Down Expand Up @@ -40,4 +41,8 @@ def local_target_params
translations_attributes = translation_params(::SDG::LocalTarget)
params.require(:sdg_local_target).permit(:code, :target_id, translations_attributes)
end

def add_related_goal
@local_target.goal = @local_target.target.goal
end
end
14 changes: 10 additions & 4 deletions app/models/concerns/sdg/relatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,24 @@ def sdg_target_list

def sdg_related_list
sdg_goals.order(:code).map do |goal|
[goal, sdg_global_targets.where(goal: goal).sort]
global_targets = sdg_global_targets.where(goal: goal)
local_targets = sdg_local_targets.where(goal: goal)
targets_related_with_goal = global_targets + local_targets
[goal, targets_related_with_goal.sort]
end.flatten.map(&:code).join(", ")
end

def sdg_related_list=(codes)
target_codes, goal_codes = codes.tr(" ", "").split(",").partition { |code| code.include?(".") }
targets = target_codes.map { |code| SDG::Target[code] }
local_targets_codes, global_targets_codes = target_codes.partition { |code| code.split(".")[2] }
global_targets = global_targets_codes.map { |code| SDG::Target[code] }
local_targets = local_targets_codes.map { |code| SDG::LocalTarget[code] }
goals = goal_codes.map { |code| SDG::Goal[code] }

transaction do
self.sdg_global_targets = targets
self.sdg_goals = (targets.map(&:goal) + goals).uniq
self.sdg_local_targets = local_targets
self.sdg_global_targets = global_targets
self.sdg_goals = (global_targets.map(&:goal) + local_targets.map(&:goal) + goals).uniq
end
end
end
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, through: :targets

def title
I18n.t("sdg.goals.goal_#{code}.title")
Expand Down
4 changes: 2 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,10 @@ 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

def self.[](code)
find_by!(code: code)
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
6 changes: 5 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,10 @@
expect(build(:sdg_local_target, target: nil)).not_to be_valid
end

it "is not valid without a goal" do
expect(build(:sdg_local_target, goal: nil)).not_to be_valid
end

describe "#goal" do
it "returns the target goal" do
local_target = create(:sdg_local_target, code: "1.1.1")
Expand Down
31 changes: 24 additions & 7 deletions spec/models/sdg/relatable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@

describe "#sdg_related_list" do
it "orders related list by code" do
relatable.sdg_goals = [SDG::Goal[1], SDG::Goal[3], SDG::Goal[2]]
relatable.sdg_targets = [SDG::Target[2.2], SDG::Target[1.2], SDG::Target[2.1]]
relatable.sdg_goals = [SDG::Goal[1], SDG::Goal[2], SDG::Goal[3]]
local_targets = %w[2.2.2 2.2.1 3.1.1].map { |code| create(:sdg_local_target, code: code) }
relatable.sdg_targets = [SDG::Target[2.2], SDG::Target[1.2], SDG::Target[2.1]] + local_targets

expect(relatable.sdg_related_list).to eq "1, 1.2, 2, 2.1, 2.2, 3"
expect(relatable.sdg_related_list).to eq "1, 1.2, 2, 2.1, 2.2, 2.2.1, 2.2.2, 3, 3.1.1"
end
end

Expand Down Expand Up @@ -133,6 +134,13 @@
expect(relatable.reload.sdg_targets).to match_array [SDG::Target[1.1]]
end

it "assigns a single local target" do
relatable.sdg_related_list = local_target.code

expect(relatable.reload.sdg_goals).to match_array [SDG::Goal[1]]
expect(relatable.reload.sdg_local_targets).to match_array [SDG::LocalTarget["1.2.1"]]
end

it "assigns multiple targets" do
relatable.sdg_related_list = "1.1,2.3"

Expand All @@ -146,18 +154,27 @@
expect(relatable.reload.sdg_goals).to match_array [SDG::Goal[1], SDG::Goal[2], SDG::Goal[3]]
end

it "assigns a multiple local targets" do
relatable.sdg_related_list = "#{local_target.code}, #{another_local_target.code}"

expect(relatable.reload.sdg_goals).to match_array [SDG::Goal[1], SDG::Goal[2]]
expect(relatable.reload.sdg_local_targets).to match_array [SDG::LocalTarget["1.2.1"], SDG::LocalTarget["2.3.1"]]
end


it "ignores trailing spaces and spaces between commas" do
relatable.sdg_related_list = " 1.1, 2.3 "

expect(relatable.reload.sdg_goals).to match_array [SDG::Goal[1], SDG::Goal[2]]
expect(relatable.reload.sdg_targets).to match_array [SDG::Target[1.1], SDG::Target[2.3]]
end

it "assigns goals and targets" do
relatable.sdg_related_list = "1.1,3,4,4.1"
it "assigns goals, targets and local_targets" do
relatable.sdg_related_list = "1.1,3,4,4.1,#{another_local_target.code}"

expect(relatable.reload.sdg_goals).to match_array [SDG::Goal[1], SDG::Goal[3], SDG::Goal[4]]
expect(relatable.reload.sdg_targets).to match_array [SDG::Target[1.1], SDG::Target[4.1]]
expect(relatable.reload.sdg_goals).to match_array [SDG::Goal[1], SDG::Goal[2], SDG::Goal[3], SDG::Goal[4]]
expect(relatable.reload.sdg_global_targets).to match_array [SDG::Target[1.1], SDG::Target[4.1]]
expect(relatable.reload.sdg_local_targets).to match_array [SDG::LocalTarget["2.3.1"]]
end
end

Expand Down

0 comments on commit 27efd28

Please sign in to comment.