Extract method to get SDG code with title

This commit is contained in:
Javi Martín
2021-08-25 21:18:34 +02:00
parent ead5aa5c31
commit 7f9a05d052
8 changed files with 26 additions and 14 deletions

View File

@@ -18,7 +18,7 @@ class SDG::RelatedListSelectorComponent < ApplicationComponent
def suggestion_tag_for(goal_or_target)
{
tag: "#{goal_or_target.code}. #{goal_or_target.title.gsub(",", "")}",
tag: goal_or_target.code_and_title.gsub(",", ""),
display_text: text_for(goal_or_target),
title: goal_or_target.title,
value: goal_or_target.code

View File

@@ -30,14 +30,10 @@ class SDGManagement::LocalTargets::FormComponent < ApplicationComponent
def target_options
grouped_targets = SDG::Goal.order(:code).map do |goal|
[
code_and_title(goal),
goal.targets.sort.map { |target| [code_and_title(target), target.id] }
goal.code_and_title,
goal.targets.sort.map { |target| [target.code_and_title, target.id] }
]
end
grouped_options_for_select(grouped_targets, local_target.target_id)
end
def code_and_title(resource)
"#{resource.code} #{resource.title}"
end
end

View File

@@ -18,7 +18,7 @@
<% local_targets.group_by(&:target).map do |target, local_targets| %>
<tr class="target-header">
<th id="<%= header_id(target) %>" colspan="3" scope="colgroup">
<%= target.code %> <%= target.title %>
<%= target.code_and_title %>
</th>
</tr>

View File

@@ -41,4 +41,8 @@ module SDG::Related
end
end
end
def code_and_title
"#{code}. #{title}"
end
end

View File

@@ -21,8 +21,4 @@ class SDG::Goal < ApplicationRecord
def self.[](code)
find_by!(code: code)
end
def code_and_title
"#{code}. #{title}"
end
end

View File

@@ -47,6 +47,14 @@ describe SDG::LocalTarget do
expect(build(:sdg_local_target, target: nil)).not_to be_valid
end
describe "#code_and_title" do
it "returns the code and the title" do
target = build(:sdg_local_target, code: "4.4.1", title: "Build a university")
expect(target.code_and_title).to eq "4.4.1. Build a university"
end
end
describe "#set_related_goal" do
it "before validation set related goal" do
local_target = build(:sdg_local_target, code: "1.1.1", target: SDG::Target["1.1"], goal: nil)

View File

@@ -42,6 +42,14 @@ describe SDG::Target do
end
end
describe "#code_and_title" do
it "returns the code and the title" do
target = SDG::Target["8.A"]
expect(target.code_and_title).to start_with "8.A. Increase Aid for Trade support for developing"
end
end
describe "#<=>" do
let(:goal) { build(:sdg_goal, code: 10) }
let(:target) { build(:sdg_target, code: "10.19", goal: goal) }

View File

@@ -40,7 +40,7 @@ describe "Local Targets" do
visit new_sdg_management_local_target_path
target = SDG::Target["1.1"]
select "#{target.code} #{target.title}", from: "Target"
select "1.1. #{target.title}", from: "Target"
fill_in "Code", with: "1.1.1"
fill_in "Title", with: "Local target title"
fill_in "Description", with: "Local target description"
@@ -54,7 +54,7 @@ describe "Local Targets" do
visit new_sdg_management_local_target_path
target = SDG::Target["2.3"]
code_and_title = "#{target.code} #{target.title}"
code_and_title = "2.3. #{target.title}"
select code_and_title, from: "Target"
click_button "Create local target"