Add targets index to the administration

Co-authored-by: Javi Martín <javim@elretirao.net>
This commit is contained in:
Senén Rodero Rodríguez
2020-11-14 10:19:49 +01:00
parent 00e91c40b9
commit 7fb3f1920e
10 changed files with 119 additions and 1 deletions

View File

@@ -1,5 +1,9 @@
<%= header %>
<%# TODO: replace links with tabs %>
<%= link_to SDG::Goal.model_name.human(count: 2).titleize, sdg_management_goals_path %>
<%= link_to SDG::Target.model_name.human(count: 2).titleize, sdg_management_targets_path %>
<table>
<thead>
<tr>

View File

@@ -2,6 +2,6 @@ class SDGManagement::MenuComponent < ApplicationComponent
private
def sdg?
controller_name == "goals"
controller_name == "goals" || controller_name == "targets"
end
end

View File

@@ -0,0 +1,35 @@
<%= header %>
<%# TODO: replace links with tabs %>
<%= link_to SDG::Goal.model_name.human(count: 2).titleize, sdg_management_goals_path %>
<%= link_to SDG::Target.model_name.human(count: 2).titleize, sdg_management_targets_path %>
<table>
<thead>
<tr>
<td></td>
<th id="target-title" scope="col"><%= attribute_name(:title) %></th>
</tr>
</thead>
<tbody>
<% targets.group_by(&:goal).map do |goal, targets| %>
<tr class="goal-header">
<th id="<%= header_id(goal) %>" colspan="2" scope="colgroup">
<%= goal.code %>. <%= goal.title %>
</th>
</tr>
<% targets.each do |target| %>
<tr id="<%= dom_id(target) %>" class="target">
<th headers="<%= header_id(goal) %>" id="<%= header_id(target) %>">
<%= target.code %>
</th>
<td headers="<%= header_id(goal) %> <%= header_id(target) %> target-title">
<%= target.title %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>

View File

@@ -0,0 +1,23 @@
class SDGManagement::Targets::IndexComponent < ApplicationComponent
include SDGManagement::Header
attr_reader :targets
def initialize(targets)
@targets = targets
end
private
def title
SDG::Target.model_name.human(count: 2).titleize
end
def attribute_name(attribute)
SDG::Target.human_attribute_name(attribute)
end
def header_id(object)
"#{dom_id(object)}_header"
end
end

View File

@@ -0,0 +1,7 @@
class SDGManagement::TargetsController < SDGManagement::BaseController
Target = ::SDG::Target
def index
@targets = Target.all.sort
end
end

View File

@@ -0,0 +1 @@
<%= render SDGManagement::Targets::IndexComponent.new(@targets) %>

View File

@@ -75,6 +75,9 @@ en:
sdg/goal:
one: "goal"
other: "goals"
sdg/target:
one: "target"
other: "targets"
site_customization/page:
one: Custom page
other: Custom pages
@@ -315,6 +318,9 @@ en:
code: "Code"
title: "Title"
description: "Description"
sdg/target:
code: "Code"
title: "Title"
site_customization/page:
content: Content
created_at: Created at

View File

@@ -75,6 +75,9 @@ es:
sdg/goal:
one: "objetivo"
other: "objetivos"
sdg/target:
one: "meta"
other: "metas"
site_customization/page:
one: Página
other: Páginas
@@ -312,6 +315,9 @@ es:
code: "Código"
title: "Título"
description: "Descripción"
sdg/target:
code: "Código"
title: "Título"
signature_sheet:
title: "Título"
signable_type: "Tipo de hoja de firmas"

View File

@@ -2,4 +2,5 @@ namespace :sdg_management do
root to: "goals#index"
resources :goals, only: [:index]
resources :targets, only: [:index]
end

View File

@@ -0,0 +1,35 @@
require "rails_helper"
describe "Targets", :js do
before do
login_as(create(:administrator).user)
Setting["feature.sdg"] = true
end
describe "Index" do
scenario "Visit the index" do
visit sdg_management_goals_path
click_link "Targets"
expect(page).to have_title "SDG content - Targets"
within("table") { expect(page).to have_content "By 2030, eradicate extreme poverty" }
end
scenario "Show targets grouped by goal and sorted asc by code" do
goal_8 = SDG::Goal[8]
goal_8_target_2 = SDG::Target["8.2"]
goal_8_target_10 = SDG::Target["8.10"]
goal_16 = SDG::Goal[16]
goal_16_target_10 = SDG::Target["16.10"]
goal_16_target_A = SDG::Target["16.A"]
visit sdg_management_targets_path
expect(goal_8.title).to appear_before(goal_8_target_2.title)
expect(goal_8_target_2.title).to appear_before(goal_8_target_10.title)
expect(goal_8_target_10.title).to appear_before(goal_16.title)
expect(goal_16.title).to appear_before(goal_16_target_10.title)
expect(goal_16_target_10.title).to appear_before(goal_16_target_A.title)
end
end
end