Add empty SDG goal show page

Note we're using the code instead of the ID to get the goal in the URL.
IMHO this is what most people would expect; visiting a URL with a "7"
takes you to SDG number 7, and not to the one with "7" as a database ID.

In order to avoid tests (either automated tests or manual tests) passing
by coincidence due to the goal ID and the goal code being the same, I'm
shuffling the codes before entering them in the databse.

I've tried using `resolve` in the routes so the code is automatically
taken into account, but it doesn't work since `resolve` cannot be used
inside a namespace, and here we're within the `sdg` namespace.
This commit is contained in:
Javi Martín
2020-12-19 15:24:18 +01:00
parent de4be15a8d
commit 910acff624
10 changed files with 45 additions and 3 deletions

View File

@@ -0,0 +1 @@
<%= link_list(*goals.map { |goal| [goal.code_and_title, sdg_goal_path(goal.code)] }) %>

View File

@@ -1,5 +1,6 @@
class SDG::Goals::IndexComponent < ApplicationComponent class SDG::Goals::IndexComponent < ApplicationComponent
attr_reader :goals attr_reader :goals
delegate :link_list, to: :helpers
def initialize(goals) def initialize(goals)
@goals = goals @goals = goals

View File

@@ -0,0 +1,7 @@
class SDG::Goals::ShowComponent < ApplicationComponent
attr_reader :goal
def initialize(goal)
@goal = goal
end
end

View File

@@ -1,9 +1,12 @@
class SDG::GoalsController < ApplicationController class SDG::GoalsController < ApplicationController
include FeatureFlags include FeatureFlags
feature_flag :sdg feature_flag :sdg
load_and_authorize_resource load_and_authorize_resource find_by: :code, id_param: :code
def index def index
@goals = @goals.order(:code) @goals = @goals.order(:code)
end end
def show
end
end end

View File

@@ -0,0 +1 @@
<%= render SDG::Goals::ShowComponent.new(@goal) %>

View File

@@ -1,3 +1,3 @@
namespace :sdg do namespace :sdg do
resources :goals, only: :index resources :goals, param: :code, only: [:index, :show]
end end

View File

@@ -1,4 +1,4 @@
(1..17).each do |code| (1..17).to_a.shuffle.each do |code|
SDG::Goal.where(code: code).first_or_create! SDG::Goal.where(code: code).first_or_create!
end end

View File

@@ -0,0 +1,19 @@
require "rails_helper"
describe "SDG routes" do
it "maps goals to their code" do
expect(get("/sdg/goals/1")).to route_to(
controller: "sdg/goals",
action: "show",
code: "1"
)
end
it "requires using the code instead of the ID" do
expect(get(sdg_goal_path(SDG::Goal[2].code))).to route_to(
controller: "sdg/goals",
action: "show",
code: "2"
)
end
end

View File

@@ -21,4 +21,14 @@ describe "SDG Goals", :js do
expect(page).to have_current_path sdg_goals_path expect(page).to have_current_path sdg_goals_path
end end
end end
describe "Index" do
scenario "has links to SDGs" do
visit sdg_goals_path
click_link "7. Affordable and Clean Energy"
expect(page).to have_current_path sdg_goal_path(7)
end
end
end end