Add form to assign targets to a record

This commit is contained in:
Javi Martín
2020-11-25 12:02:03 +01:00
parent 11c3b3db13
commit f76279a4dd
10 changed files with 97 additions and 2 deletions

View File

@@ -0,0 +1,7 @@
<%= header %>
<%= form_for record, url: update_path do |f| %>
<%= f.text_field :sdg_target_list %>
<%= f.submit %>
<% end %>

View File

@@ -0,0 +1,24 @@
class SDGManagement::Relations::EditComponent < ApplicationComponent
include Header
attr_reader :record
def initialize(record)
@record = record
end
private
def title
@record.title
end
def update_path
{
controller: "sdg_management/relations",
action: :update,
relatable_type: record.class.name.tableize,
id: record
}
end
end

View File

@@ -1,14 +1,25 @@
class SDGManagement::RelationsController < SDGManagement::BaseController
before_action :load_record, only: [:edit, :update]
def index
@records = relatable_class.accessible_by(current_ability).order(:id).page(params[:page])
end
def edit
@record = relatable_class.find(params[:id])
end
def update
@record.sdg_target_list = params[@record.class.table_name.singularize][:sdg_target_list]
redirect_to action: :index
end
private
def load_record
@record = relatable_class.find(params[:id])
end
def relatable_class
params[:relatable_type].classify.constantize
end

View File

@@ -23,4 +23,13 @@ module SDG::Relatable
def sdg_target_list
sdg_targets.sort.map(&:code).join(", ")
end
def sdg_target_list=(codes)
targets = codes.tr(" ", "").split(",").map { |code| SDG::Target[code] }
transaction do
self.sdg_targets = targets
self.sdg_goals = targets.map(&:goal).uniq
end
end
end

View File

@@ -1 +1 @@
<h2><%= @record.title %></h2>
<%= render SDGManagement::Relations::EditComponent.new(@record) %>

View File

@@ -2,6 +2,7 @@ en:
attributes:
geozone_id: "Scope of operation"
results_enabled: "Show results"
sdg_target_list: "Targets"
stats_enabled: "Show stats"
advanced_stats_enabled: "Show advanced stats"
name: Name

View File

@@ -2,6 +2,7 @@ es:
attributes:
geozone_id: "Ámbito de actuación"
results_enabled: "Mostrar resultados"
sdg_target_list: "Metas"
stats_enabled: "Mostrar estadísticas"
advanced_stats_enabled: "Mostrar estadísticas avanzadas"
name: Nombre

View File

@@ -10,6 +10,7 @@ namespace :sdg_management do
get "*relatable_type", to: "relations#index", as: "relations", relatable_type: types_constraint
get "*relatable_type/:id/edit", to: "relations#edit", as: "edit_relation", relatable_type: types_constraint
patch "*relatable_type/:id", to: "relations#update", as: "relation", relatable_type: types_constraint
types.each do |type|
get type, to: "relations#index", as: type

View File

@@ -90,4 +90,30 @@ describe SDG::Relatable do
expect(relatable.reload.related_sdgs).to match_array related_sdgs
end
end
describe "#sdg_target_list=" do
it "assigns a single target" do
relatable.sdg_target_list = "1.1"
expect(relatable.reload.sdg_targets).to match_array [SDG::Target["1.1"]]
end
it "assigns multiple targets" do
relatable.sdg_target_list = "1.1,2.3"
expect(relatable.reload.sdg_targets).to match_array [SDG::Target["1.1"], SDG::Target["2.3"]]
end
it "ignores trailing spaces and spaces between commas" do
relatable.sdg_target_list = " 1.1, 2.3 "
expect(relatable.reload.sdg_targets).to match_array [SDG::Target["1.1"], SDG::Target["2.3"]]
end
it "assigns goals" do
relatable.sdg_target_list = "1.1,1.2,2.3"
expect(relatable.reload.sdg_goals).to match_array [SDG::Goal[1], SDG::Goal[2]]
end
end
end

View File

@@ -83,4 +83,19 @@ describe "SDG Relations", :js do
expect(page).to have_css "h2", exact_text: "Build a hospital"
end
end
describe "Edit" do
scenario "allows changing the targets" do
process = create(:legislation_process, title: "SDG process")
process.sdg_targets = [SDG::Target["3.3"]]
visit sdg_management_edit_legislation_process_path(process)
fill_in "Targets", with: "1.2, 2.1"
click_button "Update Process"
within("tr", text: "SDG process") do
expect(page).to have_css "td", exact_text: "1.2, 2.1"
end
end
end
end