-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from dalvarez2596/lesson-59
Added lessons model
- Loading branch information
Showing
21 changed files
with
297 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module LessonsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
%h1 New lesson | ||
|
||
= render 'form' | ||
|
||
= link_to 'Back', lessons_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |