Skip to content

Commit

Permalink
Merge pull request #20 from dalvarez2596/lesson-59
Browse files Browse the repository at this point in the history
Added lessons model
  • Loading branch information
dalvarez2596 authored Oct 28, 2024
2 parents f76d65a + 11b19a5 commit de087a1
Show file tree
Hide file tree
Showing 21 changed files with 297 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/controllers/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def index

# GET /courses/1 or /courses/1.json
def show
@lessons = @course.lessons
end

# GET /courses/new
Expand Down
70 changes: 70 additions & 0 deletions app/controllers/lessons_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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
end

# GET /lessons/new
def new
@lesson = Lesson.new
end

# GET /lessons/1/edit
def edit
end

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

respond_to do |format|
if @lesson.save
format.html { redirect_to @lesson, notice: "Lesson was successfully created." }
format.json { render :show, status: :created, location: @lesson }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @lesson.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /lessons/1 or /lessons/1.json
def update
respond_to do |format|
if @lesson.update(lesson_params)
format.html { redirect_to @lesson, notice: "Lesson was successfully updated." }
format.json { render :show, status: :ok, location: @lesson }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @lesson.errors, status: :unprocessable_entity }
end
end
end

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

respond_to do |format|
format.html { redirect_to lessons_path, 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
@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
end
2 changes: 2 additions & 0 deletions app/helpers/lessons_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module LessonsHelper
end
2 changes: 2 additions & 0 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Course < ApplicationRecord
validates :description, presence: true, length: { minimum: 5 }

belongs_to :user
has_many :lessons, dependent: :destroy

def to_s
title
end
Expand Down
7 changes: 7 additions & 0 deletions app/models/lesson.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Lesson < ApplicationRecord
belongs_to :course
validates :title, :content, :course, presence: true

extend FriendlyId
friendly_id :title, use: :slugged
end
3 changes: 3 additions & 0 deletions app/views/courses/_course.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
.card-footer
.text-center
= number_to_currency(course.price)
.card-footer
.fa.fa-book
= course.lessons.count
.card-footer
.row
.col-md-6
Expand Down
7 changes: 6 additions & 1 deletion app/views/courses/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
\/
= link_to @course.title, course_path(@course)
%p
= render 'courses/course', course: @course
.row
.col-lg-6
= render 'courses/course', course: @course
.col-lg-6
- @lessons.each do |lesson|
= render 'lessons/lesson', lesson: lesson
12 changes: 12 additions & 0 deletions app/views/lessons/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-# frozen_string_literal: true
= simple_form_for(@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

.form-actions
= f.button :submit
10 changes: 10 additions & 0 deletions app/views/lessons/_lesson.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.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'

7 changes: 7 additions & 0 deletions app/views/lessons/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%h1 Editing lesson

= render 'form'

= link_to 'Show', @lesson
\|
= link_to 'Back', lessons_path
25 changes: 25 additions & 0 deletions app/views/lessons/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
%h1 Listing lessons

%table
%thead
%tr
%th Title
%th Content
%th Course
%th
%th
%th

%tbody
- @lessons.each do |lesson|
%tr
%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?' }

%br

= link_to 'New Lesson', new_lesson_path
5 changes: 5 additions & 0 deletions app/views/lessons/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%h1 New lesson

= render 'form'

= link_to 'Back', lessons_path
5 changes: 5 additions & 0 deletions app/views/lessons/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%p#notice= notice

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

= link_to 'Back to course', course_path(@lesson.course)
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :lessons
devise_for :users
resources :courses
resources :users, only: [ :index, :edit, :show, :update ]
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20241026192838_create_lessons.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateLessons < ActiveRecord::Migration[7.2]
def change
create_table :lessons do |t|
t.string :title
t.text :content
t.references :course, null: false, foreign_key: true

t.timestamps
end
end
end
6 changes: 6 additions & 0 deletions db/migrate/20241028032304_add_slug_to_lessons.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddSlugToLessons < ActiveRecord::Migration[7.2]
def change
add_column :lessons, :slug, :string
add_index :lessons, :slug, unique: true
end
end
14 changes: 13 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions test/controllers/lessons_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "test_helper"

class LessonsControllerTest < ActionDispatch::IntegrationTest
setup do
@lesson = lessons(:one)
end

test "should get index" do
get lessons_url
assert_response :success
end

test "should get new" do
get new_lesson_url
assert_response :success
end

test "should create lesson" do
assert_difference("Lesson.count") do
post lessons_url, params: { lesson: { content: @lesson.content, course_id: @lesson.course_id, title: @lesson.title } }
end

assert_redirected_to lesson_url(Lesson.last)
end

test "should show lesson" do
get lesson_url(@lesson)
assert_response :success
end

test "should get edit" do
get edit_lesson_url(@lesson)
assert_response :success
end

test "should update lesson" do
patch lesson_url(@lesson), params: { lesson: { content: @lesson.content, course_id: @lesson.course_id, title: @lesson.title } }
assert_redirected_to lesson_url(@lesson)
end

test "should destroy lesson" do
assert_difference("Lesson.count", -1) do
delete lesson_url(@lesson)
end

assert_redirected_to lessons_url
end
end
11 changes: 11 additions & 0 deletions test/fixtures/lessons.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
title: MyString
content: MyText
course: one

two:
title: MyString
content: MyText
course: two
7 changes: 7 additions & 0 deletions test/models/lesson_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class LessonTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
45 changes: 45 additions & 0 deletions test/system/lessons_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require "application_system_test_case"

class LessonsTest < ApplicationSystemTestCase
setup do
@lesson = lessons(:one)
end

test "visiting the index" do
visit lessons_url
assert_selector "h1", text: "Lessons"
end

test "should create lesson" do
visit lessons_url
click_on "New lesson"

fill_in "Content", with: @lesson.content
fill_in "Course", with: @lesson.course_id
fill_in "Title", with: @lesson.title
click_on "Create Lesson"

assert_text "Lesson was successfully created"
click_on "Back"
end

test "should update Lesson" do
visit lesson_url(@lesson)
click_on "Edit this lesson", match: :first

fill_in "Content", with: @lesson.content
fill_in "Course", with: @lesson.course_id
fill_in "Title", with: @lesson.title
click_on "Update Lesson"

assert_text "Lesson was successfully updated"
click_on "Back"
end

test "should destroy Lesson" do
visit lesson_url(@lesson)
click_on "Destroy this lesson", match: :first

assert_text "Lesson was successfully destroyed"
end
end

0 comments on commit de087a1

Please sign in to comment.