Skip to content

Commit

Permalink
Merge pull request #21 from dalvarez2596/lessons60-65
Browse files Browse the repository at this point in the history
Lessons60-65
  • Loading branch information
dalvarez2596 authored Oct 29, 2024
2 parents de087a1 + 0849a06 commit 2db6c8d
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 36 deletions.
25 changes: 11 additions & 14 deletions app/controllers/lessons_controller.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
class LessonsController < ApplicationController
before_action :set_lesson, only: %i[ show edit update destroy ]

# GET /lessons or /lessons.json
def index
@lessons = Lesson.all
end

# GET /lessons/1 or /lessons/1.json
def show
authorize @lesson
end

# GET /lessons/new
def new
@lesson = Lesson.new
@course = Course.friendly.find(params[:course_id])
end

# GET /lessons/1/edit
def edit
authorize @lesson
end

# POST /lessons or /lessons.json
def create
@lesson = Lesson.new(lesson_params)

@course = Course.friendly.find(params[:course_id])
@lesson.course_id = @course.id
respond_to do |format|
if @lesson.save
format.html { redirect_to @lesson, notice: "Lesson was successfully created." }
format.html { redirect_to [ @course, @lesson ], notice: "Lesson was successfully created." }
format.json { render :show, status: :created, location: @lesson }
else
format.html { render :new, status: :unprocessable_entity }
Expand All @@ -34,11 +33,11 @@ def create
end
end

# PATCH/PUT /lessons/1 or /lessons/1.json
def update
authorize @lesson
respond_to do |format|
if @lesson.update(lesson_params)
format.html { redirect_to @lesson, notice: "Lesson was successfully updated." }
format.html { redirect_to [ @course, @lesson ], notice: "Lesson was successfully updated." }
format.json { render :show, status: :ok, location: @lesson }
else
format.html { render :edit, status: :unprocessable_entity }
Expand All @@ -47,23 +46,21 @@ def update
end
end

# DELETE /lessons/1 or /lessons/1.json
def destroy
authorize @lesson
@lesson.destroy!

respond_to do |format|
format.html { redirect_to lessons_path, status: :see_other, notice: "Lesson was successfully destroyed." }
format.html { redirect_to @course, status: :see_other, notice: "Lesson was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_lesson
@course = Course.friendly.find(params[:course_id])
@lesson = Lesson.friendly.find(params[:id])
end

# Only allow a list of trusted parameters through.
def lesson_params
params.require(:lesson).permit(:title, :content, :course_id)
end
Expand Down
10 changes: 10 additions & 0 deletions app/models/lesson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ class Lesson < ApplicationRecord
belongs_to :course
validates :title, :content, :course, presence: true

has_rich_text :content

extend FriendlyId
friendly_id :title, use: :slugged

include PublicActivity::Model
# tracked
tracked owner: Proc.new { |controller, model| controller.current_user }

def to_s
title
end
end
36 changes: 36 additions & 0 deletions app/policies/lesson_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class LessonPolicy < ApplicationPolicy
# NOTE: Up to Pundit v2.3.1, the inheritance was declared as
# `Scope < Scope` rather than `Scope < ApplicationPolicy::Scope`.
# In most cases the behavior will be identical, but if updating existing
# code, beware of possible changes to the ancestors:
# https://gist.github.com/Burgestrand/4b4bc22f31c8a95c425fc0e30d7ef1f5

class Scope < ApplicationPolicy::Scope
# NOTE: Be explicit about which records you allow access to!
# def resolve
# scope.all
# end
end

def show?
@user.has_role?(:admin) || @record.course.user == @user.id
end

def edit?
@record.course.user_id == @user.id
end

def update?
@record.course.user_id == @user.id
end

def new?
end

def create?
end

def destroy?
@record.course.user_id == @user.id
end
end
2 changes: 1 addition & 1 deletion app/views/courses/_course.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
.col-md-6
.text-end
.fa.fa-chalkboard-teacher
= course.user.username
= link_to course.user.username, user_path(course.user)
- if current_user && policy(course).edit?
.card-footer
= link_to 'Edit', edit_course_path(course), class: 'btn btn-sm btn-warning'
Expand Down
1 change: 1 addition & 0 deletions app/views/courses/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
.col-lg-6
= render 'courses/course', course: @course
.col-lg-6
= link_to 'Add lesson', new_course_lesson_path(@course, @lesson)
- @lessons.each do |lesson|
= render 'lessons/lesson', lesson: lesson
10 changes: 7 additions & 3 deletions app/views/lessons/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
-# frozen_string_literal: true
= simple_form_for(@lesson) do |f|
= simple_form_for([@course, @lesson]) do |f|
= f.error_notification
= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present?

.form-inputs
= f.input :title
= f.input :content
= f.association :course
= f.label :content
= f.rich_text_area :content
-# como estaba
-# = f.association :course
-# Esto de abajo asigno el input del html por defecto si ya es un lesson o por params. as: :hidden hide the input
-# = f.input :course_id, input_html: {value: @course.id || params[:course_id]}, as: :hidden
.form-actions
= f.button :submit
17 changes: 10 additions & 7 deletions app/views/lessons/_lesson.html.haml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
.card
.card-header
= link_to lesson.title, lesson
.card-body
= lesson.content
= lesson.course
.card-footer
= link_to 'Edit', edit_lesson_path(lesson), class: 'btn btn-sm btn-warning'
= link_to 'Destroy', lesson, data: { turbo_method: :delete, confirm: 'Are you sure' }, class: 'btn btn-sm btn-danger'
= link_to lesson.title, [@course, lesson]
- if policy(lesson).show?
.card-body
= lesson.content
= lesson.course
- if policy(lesson).edit?
.card-footer
= link_to 'Edit', edit_course_lesson_path(@course, lesson), class: 'btn btn-sm btn-warning'
= link_to 'Destroy', [@course, lesson], data: { turbo_method: :delete, confirm: 'Are you sure' }, class: 'btn btn-sm btn-danger'
%p

4 changes: 2 additions & 2 deletions app/views/lessons/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

= render 'form'

= link_to 'Show', @lesson
= link_to 'Show', [@course, @lesson]
\|
= link_to 'Back', lessons_path
= link_to 'Back to course', course_path(@course)
8 changes: 4 additions & 4 deletions app/views/lessons/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
%td= lesson.title
%td= lesson.content
%td= lesson.course
%td= link_to 'Show', lesson
%td= link_to 'Edit', edit_lesson_path(lesson)
%td= link_to 'Destroy', lesson, method: :delete, data: { confirm: 'Are you sure?' }
%td= link_to 'Show', course_lesson_path(@course, lesson)
%td= link_to 'Edit', edit_course_lesson_path(@course, lesson)
%td= link_to 'Destroy', [@course, lesson], method: :delete, data: { confirm: 'Are you sure?', turbo_frame: "_top" }

%br

= link_to 'New Lesson', new_lesson_path
= link_to 'New Lesson', new_course_lesson_path
2 changes: 1 addition & 1 deletion app/views/lessons/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

= render 'form'

= link_to 'Back', lessons_path
= link_to 'Back', course_path(@course)
8 changes: 6 additions & 2 deletions app/views/lessons/show.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
%p#notice= notice
= link_to 'Courses', courses_path
\/
= link_to @lesson.course.title, course_path(@course)
\/
= link_to @lesson.title, course_lesson_path(@course, @lesson)

= render 'lessons/lesson', lesson: @lesson

= link_to 'Back to course', course_path(@lesson.course)
= link_to 'Back to course', course_path(@course)
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Rails.application.routes.draw do
resources :lessons
devise_for :users
resources :courses
resources :courses do
resources :lessons
end
resources :users, only: [ :index, :edit, :show, :update ]
get "home/index"
get "home/activity"
Expand Down

0 comments on commit 2db6c8d

Please sign in to comment.