diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 8a76566d0..5b8156269 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -188,7 +188,7 @@ class CropsController < ApplicationController def crop_params params.require(:crop).permit( - :name, :en_wikipedia_url, + :name, :en_wikipedia_url, :en_youtube_url, :parent_id, :perennial, :request_notes, :reason_for_rejection, :rejection_notes, diff --git a/app/helpers/crops_helper.rb b/app/helpers/crops_helper.rb index a7f19a60d..19f2a9e48 100644 --- a/app/helpers/crops_helper.rb +++ b/app/helpers/crops_helper.rb @@ -17,4 +17,12 @@ module CropsHelper def crop_ebay_seeds_url(crop) "https://www.ebay.com/sch/i.html?_nkw=#{CGI.escape crop.name}" end + + def youtube_video_id(url) + return unless url + + regex = %r{(?:youtube(?:-nocookie)?\.com/(?:[^/\n\s]+/\S+/|(?:v|e(?:mbed)?)/|\S*?[?&]v=)|youtu\.be/)([a-zA-Z0-9_-]{11})} + match = url.match(regex) + match[1] if match + end end diff --git a/app/models/crop.rb b/app/models/crop.rb index a647c766f..bf1a7bbe7 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -55,6 +55,12 @@ class Crop < ApplicationRecord message: 'is not a valid English Wikipedia URL' }, if: :approved? + validates :en_youtube_url, + format: { + with: %r{\A(?:https?://)?(?:www\.)?(?:youtube(?:-nocookie)?\.com/(?:(?:v|e(?:mbed)?)/|\S*?[?&]v=)|youtu\.be/)[a-zA-Z0-9_-]{11}(?:[?&]\S*)?\z}, + message: 'is not a valid YouTube URL' + }, + allow_blank: true validates :name, uniqueness: { scope: :approval_status }, if: :pending? def to_s diff --git a/app/views/crops/_form.html.haml b/app/views/crops/_form.html.haml index 8797bfe81..85712c9f5 100644 --- a/app/views/crops/_form.html.haml +++ b/app/views/crops/_form.html.haml @@ -54,6 +54,9 @@ = f.url_field :en_wikipedia_url, id: "en_wikipedia_url", label: 'Wikipedia URL' %span.help-block Link to the crop's page on the English language Wikipedia (required). + = f.url_field :en_youtube_url, label: 'YouTube URL' + %span.help-block + Link to a YouTube video about the crop in English. -# Only crop wranglers see the crop hierarchy (for now) - if can? :wrangle, @crop diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml index 5ab1525b8..a2f0fe88a 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -30,6 +30,11 @@ - @crop.all_companions.each do |companion| = render 'crops/tiny', crop: companion + - if @crop.en_youtube_url.present? + %section.youtube + .embed-responsive.embed-responsive-16by9 + %iframe.embed-responsive-item{ src: "https://www.youtube.com/embed/#{youtube_video_id(@crop.en_youtube_url)}", allowfullscreen: true } + %section.photos = cute_icon = render 'crops/photos', crop: @crop @@ -157,3 +162,10 @@ = icon 'fas', 'external-link-alt' Wikihow instructions + %li.list-group-item + = link_to "https://www.youtube.com/results?search_query=#{CGI.escape "growing #{@crop.name}"}", + target: "_blank", + class: 'card-link', + rel: "noopener noreferrer" do + = icon 'fab', 'youtube' + YouTube diff --git a/db/migrate/20251128193317_add_en_youtube_url_to_crops.rb b/db/migrate/20251128193317_add_en_youtube_url_to_crops.rb new file mode 100644 index 000000000..bd0cb8e40 --- /dev/null +++ b/db/migrate/20251128193317_add_en_youtube_url_to_crops.rb @@ -0,0 +1,5 @@ +class AddEnYoutubeUrlToCrops < ActiveRecord::Migration[7.2] + def change + add_column :crops, :en_youtube_url, :string + end +end