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:
@@ -0,0 +1 @@
|
||||
<%= link_list(*goals.map { |goal| [goal.code_and_title, sdg_goal_path(goal.code)] }) %>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
class SDG::Goals::IndexComponent < ApplicationComponent
|
||||
attr_reader :goals
|
||||
delegate :link_list, to: :helpers
|
||||
|
||||
def initialize(goals)
|
||||
@goals = goals
|
||||
|
||||
0
app/components/sdg/goals/show_component.html.erb
Normal file
0
app/components/sdg/goals/show_component.html.erb
Normal file
7
app/components/sdg/goals/show_component.rb
Normal file
7
app/components/sdg/goals/show_component.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class SDG::Goals::ShowComponent < ApplicationComponent
|
||||
attr_reader :goal
|
||||
|
||||
def initialize(goal)
|
||||
@goal = goal
|
||||
end
|
||||
end
|
||||
@@ -1,9 +1,12 @@
|
||||
class SDG::GoalsController < ApplicationController
|
||||
include FeatureFlags
|
||||
feature_flag :sdg
|
||||
load_and_authorize_resource
|
||||
load_and_authorize_resource find_by: :code, id_param: :code
|
||||
|
||||
def index
|
||||
@goals = @goals.order(:code)
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
end
|
||||
|
||||
1
app/views/sdg/goals/show.html.erb
Normal file
1
app/views/sdg/goals/show.html.erb
Normal file
@@ -0,0 +1 @@
|
||||
<%= render SDG::Goals::ShowComponent.new(@goal) %>
|
||||
@@ -1,3 +1,3 @@
|
||||
namespace :sdg do
|
||||
resources :goals, only: :index
|
||||
resources :goals, param: :code, only: [:index, :show]
|
||||
end
|
||||
|
||||
@@ -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!
|
||||
end
|
||||
|
||||
|
||||
19
spec/routing/sdg_routes_spec.rb
Normal file
19
spec/routing/sdg_routes_spec.rb
Normal 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
|
||||
@@ -21,4 +21,14 @@ describe "SDG Goals", :js do
|
||||
expect(page).to have_current_path sdg_goals_path
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user