Merge pull request #4329 from Growstuff/add-youtube-video-to-crop

Add YouTube Video to Crop Page
This commit is contained in:
Daniel O'Connor
2025-11-29 14:46:22 +10:30
committed by GitHub
6 changed files with 35 additions and 1 deletions

View File

@@ -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,

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,5 @@
class AddEnYoutubeUrlToCrops < ActiveRecord::Migration[7.2]
def change
add_column :crops, :en_youtube_url, :string
end
end