Add form to assign targets to a record
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
<%= header %>
|
||||||
|
|
||||||
|
<%= form_for record, url: update_path do |f| %>
|
||||||
|
<%= f.text_field :sdg_target_list %>
|
||||||
|
|
||||||
|
<%= f.submit %>
|
||||||
|
<% end %>
|
||||||
24
app/components/sdg_management/relations/edit_component.rb
Normal file
24
app/components/sdg_management/relations/edit_component.rb
Normal 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
|
||||||
@@ -1,14 +1,25 @@
|
|||||||
class SDGManagement::RelationsController < SDGManagement::BaseController
|
class SDGManagement::RelationsController < SDGManagement::BaseController
|
||||||
|
before_action :load_record, only: [:edit, :update]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@records = relatable_class.accessible_by(current_ability).order(:id).page(params[:page])
|
@records = relatable_class.accessible_by(current_ability).order(:id).page(params[:page])
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit
|
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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def load_record
|
||||||
|
@record = relatable_class.find(params[:id])
|
||||||
|
end
|
||||||
|
|
||||||
def relatable_class
|
def relatable_class
|
||||||
params[:relatable_type].classify.constantize
|
params[:relatable_type].classify.constantize
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -23,4 +23,13 @@ module SDG::Relatable
|
|||||||
def sdg_target_list
|
def sdg_target_list
|
||||||
sdg_targets.sort.map(&:code).join(", ")
|
sdg_targets.sort.map(&:code).join(", ")
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
<h2><%= @record.title %></h2>
|
<%= render SDGManagement::Relations::EditComponent.new(@record) %>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ en:
|
|||||||
attributes:
|
attributes:
|
||||||
geozone_id: "Scope of operation"
|
geozone_id: "Scope of operation"
|
||||||
results_enabled: "Show results"
|
results_enabled: "Show results"
|
||||||
|
sdg_target_list: "Targets"
|
||||||
stats_enabled: "Show stats"
|
stats_enabled: "Show stats"
|
||||||
advanced_stats_enabled: "Show advanced stats"
|
advanced_stats_enabled: "Show advanced stats"
|
||||||
name: Name
|
name: Name
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ es:
|
|||||||
attributes:
|
attributes:
|
||||||
geozone_id: "Ámbito de actuación"
|
geozone_id: "Ámbito de actuación"
|
||||||
results_enabled: "Mostrar resultados"
|
results_enabled: "Mostrar resultados"
|
||||||
|
sdg_target_list: "Metas"
|
||||||
stats_enabled: "Mostrar estadísticas"
|
stats_enabled: "Mostrar estadísticas"
|
||||||
advanced_stats_enabled: "Mostrar estadísticas avanzadas"
|
advanced_stats_enabled: "Mostrar estadísticas avanzadas"
|
||||||
name: Nombre
|
name: Nombre
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ namespace :sdg_management do
|
|||||||
|
|
||||||
get "*relatable_type", to: "relations#index", as: "relations", relatable_type: types_constraint
|
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
|
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|
|
types.each do |type|
|
||||||
get type, to: "relations#index", as: type
|
get type, to: "relations#index", as: type
|
||||||
|
|||||||
@@ -90,4 +90,30 @@ describe SDG::Relatable do
|
|||||||
expect(relatable.reload.related_sdgs).to match_array related_sdgs
|
expect(relatable.reload.related_sdgs).to match_array related_sdgs
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
@@ -83,4 +83,19 @@ describe "SDG Relations", :js do
|
|||||||
expect(page).to have_css "h2", exact_text: "Build a hospital"
|
expect(page).to have_css "h2", exact_text: "Build a hospital"
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user