Skip to content

Commit 89db33d

Browse files
committed
Lesson-121/Added validation and functionality to attach videos to lessons
1 parent 890425a commit 89db33d

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

app/controllers/lessons_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ def set_lesson
7474
@lesson = Lesson.friendly.find(params[:id])
7575
end
7676
def lesson_params
77-
params.require(:lesson).permit(:title, :content, :row_order_position)
77+
params.require(:lesson).permit(:title, :content, :row_order_position, :video, :video_thumbnail)
7878
end
7979
end

app/models/lesson.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ class Lesson < ApplicationRecord
66
has_many :user_lessons, dependent: :destroy
77
# Course.find_each{ |course| Course.reset_counters(course.id, :lessons)}
88
validates :title, :content, :course, presence: true
9-
validates :title, uniqueness:true, length: { maximum: 70 }
9+
validates :title, length: { maximum: 70 }
10+
validates_uniqueness_of :title, scope: :course_id
1011

1112
has_rich_text :content
13+
has_one_attached :video
14+
has_one_attached :video_thumbnail
15+
validates :video,
16+
content_type: [ "video/mp4", "video/quicktime", "video/mpeg" ],
17+
size: { less_than: 50.megabytes, message: "size should be under 50 megabytes" }
18+
19+
validates :video_thumbnail,
20+
content_type: [ "image/png", "image/jpg", "image/jpeg" ],
21+
size: { less_than: 500.kilobytes, message: "size should be under 500 kilobytes" }
1222

1323
extend FriendlyId
1424
friendly_id :title, use: :slugged

app/views/lessons/_form.html.haml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present?
55

66
.form-inputs
7-
= f.input :title
7+
= f.input :title, input_html: { autocomplete: 'off' }
88
= f.label :content
99
= f.rich_text_area :content
1010
%small= f.error :content, class: "text-danger"
1111
%p
12+
= f.input :video
13+
= f.input :video_thumbnail
1214
-# como estaba
1315
-# = f.association :course
1416
-# Esto de abajo asigno el input del html por defecto si ya es un lesson o por params. as: :hidden hide the input

app/views/lessons/show.html.haml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
.row
1919
.col-lg-8
2020
= render 'lessons/lesson', lesson: @lesson
21+
= @lesson.video.attached?
22+
= image_tag @lesson.video_thumbnail
23+
%video(controls="true" poster="#{url_for @lesson.video_thumbnail}" width="100%" preload="metadata")
24+
%source{src: rails_blob_url(@lesson.video), type: "#{@lesson.video.content_type}"}
2125
%p
2226
.col-lg-4
2327
%ul.list-group

0 commit comments

Comments
 (0)