Allow add local targets to RelatedListSelectorComponent

This commit is contained in:
taitus
2021-01-23 14:01:20 +01:00
parent 0a3de68206
commit 41ead2b37c
11 changed files with 139 additions and 38 deletions

View File

@@ -15,7 +15,8 @@ class SDG::RelatedListSelectorComponent < ApplicationComponent
def goals_and_targets
goals.map do |goal|
[goal, *goal.targets.sort]
global_and_local_targets = goal.targets + goal.local_targets
[goal, global_and_local_targets.sort]
end.flatten
end

View File

@@ -74,19 +74,20 @@ module SDG::Relatable
end
def sdg_related_list
sdg_goals.order(:code).map do |goal|
[goal, sdg_global_targets.where(goal: goal).sort]
end.flatten.map(&:code).join(", ")
related_sdgs.sort.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

View File

@@ -1,4 +1,5 @@
class SDG::Goal < ApplicationRecord
include Comparable
include SDG::Related
validates :code, presence: true, uniqueness: true, inclusion: { in: 1..17 }
@@ -18,6 +19,14 @@ class SDG::Goal < ApplicationRecord
I18n.t("sdg.goals.goal_#{code}.description")
end
def <=>(goal_or_target)
if goal_or_target.class == self.class
code <=> goal_or_target.code
elsif goal_or_target.respond_to?(:goal)
[self, -1] <=> [goal_or_target.goal, 1]
end
end
def self.[](code)
find_by!(code: code)
end

View File

@@ -23,11 +23,11 @@ class SDG::LocalTarget < ApplicationRecord
find_by!(code: code)
end
def <=>(any_target)
if any_target.class == self.class
[target, numeric_subcode] <=> [any_target.target, any_target.numeric_subcode]
elsif any_target.class == target.class
-1 * (any_target <=> self)
def <=>(goal_or_target)
if goal_or_target.class == self.class
[target, numeric_subcode] <=> [goal_or_target.target, goal_or_target.numeric_subcode]
elsif [target.class, goal.class].include?(goal_or_target.class)
-1 * (goal_or_target <=> self)
end
end

View File

@@ -12,11 +12,13 @@ class SDG::Target < ApplicationRecord
I18n.t("sdg.goals.goal_#{goal.code}.targets.target_#{code_key}.title")
end
def <=>(any_target)
if any_target.class == self.class
[goal.code, numeric_subcode] <=> [any_target.goal.code, any_target.numeric_subcode]
elsif any_target.class.name == "SDG::LocalTarget"
[self, -1] <=> [any_target.target, 1]
def <=>(goal_or_target)
if goal_or_target.class == self.class
[goal.code, numeric_subcode] <=> [goal_or_target.goal.code, goal_or_target.numeric_subcode]
elsif goal_or_target.class == goal.class
-1 * (goal_or_target <=> self)
elsif goal_or_target.class.name == "SDG::LocalTarget"
[self, -1] <=> [goal_or_target.target, 1]
end
end

View File

@@ -36,10 +36,12 @@ describe SDG::RelatedListSelectorComponent, type: :component do
describe "#goals_and_targets" do
it "return all goals and target with order" do
create(:sdg_local_target, code: "1.1.1")
goals_and_targets = component.goals_and_targets
expect(goals_and_targets.first).to eq SDG::Goal[1]
expect(goals_and_targets.second).to eq SDG::Target[1.1]
expect(goals_and_targets.third).to eq SDG::LocalTarget["1.1.1"]
expect(goals_and_targets.last).to eq SDG::Target[17.19]
end
end
@@ -56,7 +58,7 @@ describe SDG::RelatedListSelectorComponent, type: :component do
})
end
it "return suggestion tag for target" do
it "returns suggestion tag for global target" do
suggestion = component.suggestion_tag_for(SDG::Target[1.1])
expect(suggestion).to eq({
@@ -66,5 +68,17 @@ describe SDG::RelatedListSelectorComponent, type: :component do
value: "1.1"
})
end
it "returns suggestion tag for local target" do
create(:sdg_local_target, code: "1.1.1", title: "By 2030, eradicate extreme custom text")
suggestion = component.suggestion_tag_for(SDG::LocalTarget["1.1.1"])
expect(suggestion).to eq({
tag: "1.1.1. By 2030 eradicate extreme custom text",
display_text: "1.1.1",
title: "By 2030, eradicate extreme custom text",
value: "1.1.1"
})
end
end
end

View File

@@ -21,6 +21,34 @@ describe SDG::Goal do
end
end
describe "#<=>" do
let(:goal) { SDG::Goal[10] }
it "can be compared against goals" do
lesser_goal = SDG::Goal[9]
greater_goal = SDG::Goal[11]
expect(goal).to be > lesser_goal
expect(goal).to be < greater_goal
end
it "can be compared against global targets" do
lesser_target = build(:sdg_target, code: "9.A", goal: SDG::Goal[9])
greater_target = build(:sdg_target, code: "10.1", goal: SDG::Goal[10])
expect(goal).to be > lesser_target
expect(goal).to be < greater_target
end
it "can be compared against local targets" do
lesser_local_target = build(:sdg_local_target, code: "9.B.12")
greater_local_target = build(:sdg_local_target, code: "10.1.4")
expect(goal).to be > lesser_local_target
expect(goal).to be < greater_local_target
end
end
describe ".[]" do
it "finds existing goals by code" do
expect(SDG::Goal[1].code).to be 1

View File

@@ -90,6 +90,14 @@ describe SDG::LocalTarget do
expect(local_target).to be > lesser_target
expect(local_target).to be < greater_target
end
it "can be compared against goals" do
lesser_goal = build(:sdg_goal, code: "10")
greater_goal = build(:sdg_goal, code: "11")
expect(local_target).to be > lesser_goal
expect(local_target).to be < greater_goal
end
end
describe ".[]" do

View File

@@ -102,9 +102,10 @@ describe SDG::Relatable do
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]]
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
@@ -133,6 +134,13 @@ describe SDG::Relatable do
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 [local_target.goal]
expect(relatable.reload.sdg_local_targets).to match_array [local_target]
end
it "assigns multiple targets" do
relatable.sdg_related_list = "1.1,2.3"
@@ -146,6 +154,13 @@ describe SDG::Relatable do
expect(relatable.reload.sdg_goals).to match_array [SDG::Goal[1], SDG::Goal[2], SDG::Goal[3]]
end
it "assigns multiple local targets" do
relatable.sdg_related_list = "#{local_target.code}, #{another_local_target.code}"
expect(relatable.reload.sdg_goals).to match_array [local_target.goal, another_local_target.goal]
expect(relatable.reload.sdg_local_targets).to match_array [local_target, another_local_target]
end
it "ignores trailing spaces and spaces between commas" do
relatable.sdg_related_list = " 1.1, 2.3 "
@@ -153,11 +168,12 @@ describe SDG::Relatable do
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], another_local_target.goal, 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 [another_local_target]
end
it "touches the associated record" do

View File

@@ -87,6 +87,14 @@ describe SDG::Target do
expect(target).to be < local_target
end
end
it "can be compared against goals" do
lesser_goal = build(:sdg_goal, code: "9")
greater_goal = build(:sdg_goal, code: "11")
expect(target).to be > lesser_goal
expect(target).to be < greater_goal
end
end
describe ".[]" do

View File

@@ -204,14 +204,16 @@ describe "SDG Relations", :js do
end
describe "Edit" do
scenario "allows adding the goals and targets and marks the resource as reviewed" do
scenario "allows adding the goals, global targets and local targets and marks the resource as reviewed" do
process = create(:legislation_process, title: "SDG process")
process.sdg_goals = [SDG::Goal[3]]
process.sdg_targets = [SDG::Target[3.3]]
create(:sdg_local_target, code: "1.1.1")
create(:sdg_local_target, code: "3.3.3")
process.sdg_goals = [SDG::Goal[3], SDG::Goal[4]]
process.sdg_targets = [SDG::Target[3.3], SDG::LocalTarget["3.3.3"]]
visit sdg_management_edit_legislation_process_path(process)
find(:css, ".sdg-related-list-selector-input").set("1.2, 2,")
find(:css, ".sdg-related-list-selector-input").set("1.2, 2, 1.1.1, ")
click_button "Update Process"
@@ -220,20 +222,22 @@ describe "SDG Relations", :js do
click_link "Marked as reviewed"
within("tr", text: "SDG process") do
expect(page).to have_css "td", exact_text: "1.2, 3.3"
expect(page).to have_css "td", exact_text: "1, 2, 3"
expect(page).to have_css "td", exact_text: "1.1.1, 1.2, 3.3, 3.3.3"
expect(page).to have_css "td", exact_text: "1, 2, 3, 4"
end
end
scenario "allows removing the goals and targets" do
scenario "allows removing the goals, global target and local_targets" do
process = create(:legislation_process, title: "SDG process")
process.sdg_goals = [SDG::Goal[2], SDG::Goal[3]]
process.sdg_targets = [SDG::Target[2.1], SDG::Target[3.3]]
create(:sdg_local_target, code: "1.1.1")
process.sdg_goals = [SDG::Goal[1], SDG::Goal[2], SDG::Goal[3]]
process.sdg_targets = [SDG::Target[2.1], SDG::Target[3.3], SDG::LocalTarget["1.1.1"]]
visit sdg_management_edit_legislation_process_path(process)
remove_sdg_goal_or_target_tag(2)
remove_sdg_goal_or_target_tag(3.3)
remove_sdg_goal_or_target_tag("1.1.1")
click_button "Update Process"
@@ -242,14 +246,13 @@ describe "SDG Relations", :js do
click_link "Marked as reviewed"
within("tr", text: "SDG process") do
expect(page).to have_css "td", exact_text: "2, 3"
expect(page).to have_css "td", exact_text: "1, 2, 3"
expect(page).to have_css "td", exact_text: "2.1"
end
end
scenario "does not show the review notice when resource was already reviewed" do
debate = create(:sdg_review, relatable: create(:debate, title: "SDG debate")).relatable
debate.sdg_targets = [SDG::Target[3.3]]
visit sdg_management_edit_debate_path(debate, filter: "sdg_reviewed")
find(:css, ".sdg-related-list-selector-input").set("1.2, 2.1,")
@@ -265,8 +268,9 @@ describe "SDG Relations", :js do
end
end
scenario "allows adding the goals and targets with autocomplete" do
scenario "allows adding the goals, global targets and local targets with autocomplete" do
process = create(:legislation_process, title: "SDG process")
create(:sdg_local_target, code: "1.1.1")
visit sdg_management_edit_legislation_process_path(process)
fill_in "Sustainable Development Goals and Targets", with: "3"
@@ -279,12 +283,17 @@ describe "SDG Relations", :js do
within(".amsify-suggestags-input-area") { expect(page).to have_content "1.1" }
fill_in "Sustainable Development Goals and Targets", with: "1.1.1"
within(".amsify-list") { find(:css, "[data-val='1.1.1']").click }
within(".amsify-suggestags-input-area") { expect(page).to have_content "1.1.1" }
click_button "Update Process"
click_link "Marked as reviewed"
within("tr", text: "SDG process") do
expect(page).to have_css "td", exact_text: "1, 3"
expect(page).to have_css "td", exact_text: "1.1"
expect(page).to have_css "td", exact_text: "1.1, 1.1.1"
end
end
@@ -338,8 +347,9 @@ describe "SDG Relations", :js do
scenario "when remove a last tag related to a Goal, the icon will not be checked" do
process = create(:legislation_process, title: "SDG process")
create(:sdg_local_target, code: "1.1.1")
process.sdg_goals = [SDG::Goal[1]]
process.sdg_targets = [SDG::Target[1.1]]
process.sdg_targets = [SDG::Target[1.1], SDG::LocalTarget["1.1.1"]]
visit sdg_management_edit_legislation_process_path(process)
remove_sdg_goal_or_target_tag(1)
@@ -348,6 +358,10 @@ describe "SDG Relations", :js do
remove_sdg_goal_or_target_tag(1.1)
expect(find("li[data-code='1']")["aria-checked"]).to eq "true"
remove_sdg_goal_or_target_tag("1.1.1")
expect(find("li[data-code='1']")["aria-checked"]).to eq "false"
end
end