mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-02-25 11:20:16 -05:00
Merge pull request #4329 from Growstuff/add-youtube-video-to-crop
Add YouTube Video to Crop Page
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
5
db/migrate/20251128193317_add_en_youtube_url_to_crops.rb
Normal file
5
db/migrate/20251128193317_add_en_youtube_url_to_crops.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class AddEnYoutubeUrlToCrops < ActiveRecord::Migration[7.2]
|
||||
def change
|
||||
add_column :crops, :en_youtube_url, :string
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user