diff --git a/app/components/sdg/goals/index_component.html.erb b/app/components/sdg/goals/index_component.html.erb index e69de29bb..d475bf90b 100644 --- a/app/components/sdg/goals/index_component.html.erb +++ b/app/components/sdg/goals/index_component.html.erb @@ -0,0 +1 @@ +<%= link_list(*goals.map { |goal| [goal.code_and_title, sdg_goal_path(goal.code)] }) %> diff --git a/app/components/sdg/goals/index_component.rb b/app/components/sdg/goals/index_component.rb index 3d89921ca..e89079166 100644 --- a/app/components/sdg/goals/index_component.rb +++ b/app/components/sdg/goals/index_component.rb @@ -1,5 +1,6 @@ class SDG::Goals::IndexComponent < ApplicationComponent attr_reader :goals + delegate :link_list, to: :helpers def initialize(goals) @goals = goals diff --git a/app/components/sdg/goals/show_component.html.erb b/app/components/sdg/goals/show_component.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/app/components/sdg/goals/show_component.rb b/app/components/sdg/goals/show_component.rb new file mode 100644 index 000000000..49a311b41 --- /dev/null +++ b/app/components/sdg/goals/show_component.rb @@ -0,0 +1,7 @@ +class SDG::Goals::ShowComponent < ApplicationComponent + attr_reader :goal + + def initialize(goal) + @goal = goal + end +end diff --git a/app/controllers/sdg/goals_controller.rb b/app/controllers/sdg/goals_controller.rb index ea5498c2b..595add701 100644 --- a/app/controllers/sdg/goals_controller.rb +++ b/app/controllers/sdg/goals_controller.rb @@ -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 diff --git a/app/views/sdg/goals/show.html.erb b/app/views/sdg/goals/show.html.erb new file mode 100644 index 000000000..2eb018511 --- /dev/null +++ b/app/views/sdg/goals/show.html.erb @@ -0,0 +1 @@ +<%= render SDG::Goals::ShowComponent.new(@goal) %> diff --git a/config/routes/sdg.rb b/config/routes/sdg.rb index 9c065b9a6..52525c685 100644 --- a/config/routes/sdg.rb +++ b/config/routes/sdg.rb @@ -1,3 +1,3 @@ namespace :sdg do - resources :goals, only: :index + resources :goals, param: :code, only: [:index, :show] end diff --git a/db/sdg.rb b/db/sdg.rb index 9dde1885b..f81741e77 100644 --- a/db/sdg.rb +++ b/db/sdg.rb @@ -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 diff --git a/spec/routing/sdg_routes_spec.rb b/spec/routing/sdg_routes_spec.rb new file mode 100644 index 000000000..d3dbcb398 --- /dev/null +++ b/spec/routing/sdg_routes_spec.rb @@ -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 diff --git a/spec/system/sdg/goals_spec.rb b/spec/system/sdg/goals_spec.rb index 313765c5c..00ab66a4d 100644 --- a/spec/system/sdg/goals_spec.rb +++ b/spec/system/sdg/goals_spec.rb @@ -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