Skip to content

Commit

Permalink
Merge pull request #23 from dalvarez2596/lessons60-65
Browse files Browse the repository at this point in the history
Added policy to create
  • Loading branch information
dalvarez2596 authored Oct 29, 2024
2 parents 2db6c8d + 2cfacb6 commit 4203a1a
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 48 deletions.
3 changes: 2 additions & 1 deletion app/controllers/lessons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def create
@lesson = Lesson.new(lesson_params)
@course = Course.friendly.find(params[:course_id])
@lesson.course_id = @course.id
authorize @lesson
respond_to do |format|
if @lesson.save
format.html { redirect_to [ @course, @lesson ], notice: "Lesson was successfully created." }
Expand Down Expand Up @@ -62,6 +63,6 @@ def set_lesson
@lesson = Lesson.friendly.find(params[:id])
end
def lesson_params
params.require(:lesson).permit(:title, :content, :course_id)
params.require(:lesson).permit(:title, :content)
end
end
4 changes: 3 additions & 1 deletion app/policies/lesson_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Scope < ApplicationPolicy::Scope
end

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

def edit?
Expand All @@ -25,9 +25,11 @@ def update?
end

def new?
# @record.course.user_id == @user.id
end

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

def destroy?
Expand Down
8 changes: 4 additions & 4 deletions app/views/courses/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
%h1 Editing course
.card-body
= render 'form'

= link_to 'Show', @course
\|
= link_to 'Back', courses_path
.card-footer
= link_to 'Show', @course
\|
= link_to 'Back', courses_path
4 changes: 2 additions & 2 deletions app/views/courses/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
%h1 New course
.card-body
= render 'form'

= link_to 'Back', courses_path
.card-footer
= link_to 'Back', courses_path
6 changes: 4 additions & 2 deletions app/views/courses/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
.col-lg-6
= render 'courses/course', course: @course
.col-lg-6
= link_to 'Add lesson', new_course_lesson_path(@course, @lesson)
-if policy(@course).edit?
= link_to "Add lesson", new_course_lesson_path(@course, @lesson), class: "d-flex justify-content-center btn btn-primary"
%p
- @lessons.each do |lesson|
= render 'lessons/lesson', lesson: lesson
= render "lessons/lesson", lesson: lesson
3 changes: 2 additions & 1 deletion app/views/lessons/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
= f.input :title
= f.label :content
= f.rich_text_area :content
%small= f.error :content, class: "text-danger"
-# 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
= f.button :submit, {class: "btn btn-success mb-2 mt-2"}
16 changes: 9 additions & 7 deletions app/views/lessons/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
%h1 Editing lesson

= render 'form'

= link_to 'Show', [@course, @lesson]
\|
= link_to 'Back to course', course_path(@course)
.card
.card-header
%h1 Editing lesson
.card-body
= render 'form'
.card-footer
= link_to 'Show', [@course, @lesson]
\|
= link_to 'Back to course', course_path(@course)
25 changes: 0 additions & 25 deletions app/views/lessons/index.html.haml

This file was deleted.

12 changes: 7 additions & 5 deletions app/views/lessons/new.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
%h1 New lesson

= render 'form'

= link_to 'Back', course_path(@course)
.card
.card-header
%h1 New lesson
.card-body
= render 'form'
.card-footer
= link_to 'Back to course', course_path(@course)

0 comments on commit 4203a1a

Please sign in to comment.