Skip to content

Commit

Permalink
Add page with exercise approaches (#6871)
Browse files Browse the repository at this point in the history
* Add page with exercise approaches

* Change links

* Style generic_exercises/approaches

---------

Co-authored-by: dem4ron <demaaron88@gmail.com>
  • Loading branch information
ErikSchierboom and dem4ron authored Apr 16, 2024
1 parent 1f7b549 commit 0c6a7a4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
18 changes: 13 additions & 5 deletions app/controllers/generic_exercises_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
class GenericExercisesController < ApplicationController
before_action :use_exercise!, only: %i[show start edit complete tooltip no_test_runner]

skip_before_action :authenticate_user!, only: %i[index show tooltip]
skip_before_action :verify_authenticity_token, only: :start
before_action :use_exercise!, only: %i[show approaches]
skip_before_action :authenticate_user!, only: %i[show approaches]

def show
@ps_data = @exercise.generic_exercise
@track_variants = Exercise.available.where(
slug: params[:id],
track_id: Track.active.select(:id)
Expand Down Expand Up @@ -34,13 +31,24 @@ def show
end
end

def approaches
@approaches = Exercise::Approach.includes(:exercise, :track).joins(:exercise).
where(exercise: { slug: params[:id] }).
group_by(&:slug).
to_h.
transform_values { |approaches| approaches.sort_by { |a| a.track.title } }.
sort
end

private
def use_exercise!
begin
@exercise = Track.find_by(slug: 'elixir').exercises.find(params[:id])
rescue ActiveRecord::RecordNotFound
@exercise = Exercise.find(params[:id])
end

@ps_data = @exercise.generic_exercise
rescue ActiveRecord::RecordNotFound
render_404
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/challenges/implementation_status.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
- @featured_exercises.each.with_index do |featured_exercises, idx|
%th.bg-backgroundColorD.align-top{ class: 'h-[100px]' }
.text-textColor7.mb-4 W#{idx + 1}
= link_to featured_exercises[:slug], generic_exercise_path(featured_exercises[:slug])
= link_to featured_exercises[:slug], approaches_generic_exercise_path(featured_exercises[:slug])
%tbody
- @tracks.each do |track|
%tr
Expand Down
29 changes: 29 additions & 0 deletions app/views/generic_exercises/approaches.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
- content_for :meta_title, "Explore different ways to solve #{@ps_data.title} on Exercism"
- content_for :meta_description, "Explore the different ways to solve #{@ps_data.title}."
- content_for :canonical_url, approaches_generic_exercise_url(@ps_data.slug)
- require_stylesheet "track"

#page-generic-exercise
.header.mb-8.py-8
.lg-container.flex.items-center.relative
= exercise_icon @exercise, css_class: "self-start md:self-center w-[68px] h-[68px] md:w-[118px] md:h-[118px] mr-12 md:mr-24"
.flex-grow
%h1.text-h1.md:mb-8
#{@exercise.title} approaches

.p
= link_to "← Back to the exercise", generic_exercise_path(@exercise.slug), class: "text-prominentLinkColor"

.lg-container
.flex.flex-col.md:flex-row.gap-48
.lhs.flex-grow
%section.approaches
- @approaches.each do |(slug, track_approaches)|
.border-1.px-8.py-4.rounded-8.mb-8.border-borderColor5
%h4.text-h4.mb-8.font-mono= slug
%ul.mb-8.flex.gap-4.flex-wrap
- track_approaches.each do |approach|
%li
= link_to track_exercise_approach_path(approach.track, approach.exercise, approach), class: 'flex items-center border-1 border-prominentLinkColor rounded-16 w-fit py-4 px-8' do
= image_tag approach.track.icon_url, height: '24px', width: '24px', class: 'mr-4'
.text-h6= approach.track.title
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,11 @@
post :join
end
end
resources :exercises, only: %i[show], controller: "generic_exercises", as: :generic_exercises
resources :exercises, only: %i[show], controller: "generic_exercises", as: :generic_exercises do
member do
get :approaches
end
end

resource :user_onboarding, only: %i[show create], controller: "user_onboarding"
resource :journey, only: [:show], controller: "journey" do
Expand Down

0 comments on commit 0c6a7a4

Please sign in to comment.