Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Open source video embeds (#12418)

Łukasz Adamczyk0aa46dc76d57
diff --git a/admin/app/controllers/spree/admin/action_text/video_embeds_controller.rb b/admin/app/controllers/spree/admin/action_text/video_embeds_controller.rb
new file mode 100644
index 0000000000..3187d4bae8
--- /dev/null
+++ b/admin/app/controllers/spree/admin/action_text/video_embeds_controller.rb
@@ -0,0 +1,44 @@
+module Spree
+ module Admin
+ module ActionText
+ class VideoEmbedsController < BaseController
+ rescue_from ::OEmbed::Error, with: :video_embed_not_found
+
+ def create
+ oembed_response = oembed_cached_response(params[:url])
+
+ video_embed = ::ActionText::VideoEmbed.new(url: params[:url])
+ video_embed.raw_html = oembed_response.html
+ video_embed.thumbnail_url = oembed_response.thumbnail_url
+
+ if video_embed.save
+ content = render_to_string(
+ partial: 'action_text/video_embeds/thumbnail',
+ locals: { video_embed: video_embed },
+ formats: [:html]
+ )
+
+ render json: { sgid: video_embed.attachable_sgid, content: content }, status: :created
+ else
+ render json: { error: video_embed.errors.full_messages.to_sentence }, status: :unprocessable_entity
+ end
+ end
+
+ def destroy
+ ::ActionText::VideoEmbed.from_attachable_sgid(params[:id]).destroy
+ end
+
+ private
+
+ def video_embed_not_found
+ render json: { error: I18n.t('action_text.video_embed.not_found') }, status: :not_found
+ end
+
+ def oembed_cached_response(url)
+ raw_response = Rails.cache.fetch(['oembed', url]) { ::OEmbed::Providers.get(url).fields.to_json }
+ OEmbed::Response.create_for(raw_response, OEmbed::Providers::Youtube, url, 'json')
+ end
+ end
+ end
+ end
+end
diff --git a/admin/config/initializers/oembed.rb b/admin/config/initializers/oembed.rb
new file mode 100644
index 0000000000..a5f6241418
--- /dev/null
+++ b/admin/config/initializers/oembed.rb
@@ -0,0 +1 @@
+OEmbed::Providers.register_all
diff --git a/admin/config/routes.rb b/admin/config/routes.rb
index 51d587f9aa..a913fc141b 100644
--- a/admin/config/routes.rb
+++ b/admin/config/routes.rb
@@ -211,6 +211,11 @@ Spree::Core::Engine.add_routes do
# account management
resources :roles, except: :show
+ # Action Text
+ namespace :action_text do
+ resources :video_embeds, only: [:create, :destroy]
+ end
+
# developer tools
resources :oauth_applications
resources :webhooks_subscribers
diff --git a/admin/lib/spree/admin.rb b/admin/lib/spree/admin.rb
index fd2824d7fa..4bf78912b7 100644
--- a/admin/lib/spree/admin.rb
+++ b/admin/lib/spree/admin.rb
@@ -20,6 +20,7 @@ require 'payment_icons'
require 'dartsass-rails'
require 'responders'
require 'tinymce-rails'
+require 'oembed'
require 'spree/admin/action_callbacks'
require 'spree/admin/callbacks'
diff --git a/admin/spec/controllers/spree/admin/action_text/video_embeds_controller_spec.rb b/admin/spec/controllers/spree/admin/action_text/video_embeds_controller_spec.rb
new file mode 100644
index 0000000000..bde7c3db53
--- /dev/null
+++ b/admin/spec/controllers/spree/admin/action_text/video_embeds_controller_spec.rb
@@ -0,0 +1,84 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::ActionText::VideoEmbedsController do
+ stub_authorization!
+
+ describe '#create' do
+ subject { post :create, params: { url: url } }
+
+ let(:url) { 'https://www.youtube.com/watch?v=2iGBuLQ3S0c' }
+ let(:video_embed) { ActionText::VideoEmbed.last }
+
+ context 'when the URL is valid' do
+ let(:oembed_response) do
+ instance_double(
+ OEmbed::Response::Video,
+ fields: {
+ 'title' => 'RailsConf 2020 CE - Advanced ActionText: Attaching any Model in rich text by Chris Oliver',
+ 'author_name' => 'Confreaks',
+ 'author_url' => 'https://www.youtube.com/@Confreaks',
+ 'type' => 'video',
+ 'height' => 113,
+ 'width' => 200,
+ 'version' => '1.0',
+ 'provider_name' => 'YouTube',
+ 'provider_url' => 'https://www.youtube.com/',
+ 'thumbnail_height' => 360,
+ 'thumbnail_width' => 480,
+ 'thumbnail_url' => 'https://i.ytimg.com/vi/2iGBuLQ3S0c/hqdefault.jpg',
+ 'html' => "<iframe width=\"200\" height=\"113\" src=\"https://www.youtube.com/embed/2iGBuLQ3S0c?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen title=\"RailsConf 2020 CE - Advanced ActionText: Attaching any Model in rich text by Chris Oliver\"></iframe>"
+ }
+ )
+ end
+
+ before do
+ allow(OEmbed::Providers).to receive(:get).with(url).and_return(oembed_response)
+ end
+
+ it 'creates the video embed attachment' do
+ expect { subject }.to change(ActionText::VideoEmbed, :count).by(1)
+
+ expect(response).to have_http_status(:created)
+
+ expect(video_embed).to be_present
+ expect(video_embed.url).to eq(url)
+ expect(video_embed.thumbnail_url).to include('2iGBuLQ3S0c')
+
+ expect(video_embed.raw_html).to start_with('<iframe')
+ expect(video_embed.raw_html).to include('src="https://www.youtube.com/embed/2iGBuLQ3S0c?feature=oembed"')
+
+ expect(JSON.parse(response.body)['sgid']).to eq(video_embed.attachable_sgid)
+ end
+ end
+
+ context 'when the URL is invalid' do
+ before do
+ allow(OEmbed::Providers).to receive(:get).with(url).and_raise(OEmbed::Error)
+ end
+
+ it 'responds with a not found message' do
+ expect { subject }.not_to change(ActionText::VideoEmbed, :count)
+
+ expect(response).to have_http_status(:not_found)
+ expect(JSON.parse(response.body)['error']).to eq(I18n.t('action_text.video_embed.not_found'))
+ end
+ end
+ end
+
+ describe '#destroy' do
+ subject { delete :destroy, params: { id: video_embed.attachable_sgid } }
+
+ let!(:video_embed) do
+ ActionText::VideoEmbed.create!(
+ url: 'https://www.youtube.com/watch?v=2iGBuLQ3S0c',
+ raw_html: "<iframe width=\"200\" height=\"113\" src=\"https://www.youtube.com/embed/2iGBuLQ3S0c?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen title=\"RailsConf 2020 CE - Advanced ActionText: Attaching any Model in rich text by Chris Oliver\"></iframe>",
+ thumbnail_url: 'https://i.ytimg.com/vi/2iGBuLQ3S0c/hqdefault.jpg'
+ )
+ end
+
+ it 'removes the video embed attachment' do
+ expect { subject }.to change(ActionText::VideoEmbed, :count).from(1).to(0)
+ expect { video_embed.reload }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+end
diff --git a/admin/spree_admin.gemspec b/admin/spree_admin.gemspec
index 860f3c6857..7a7d997d06 100644
--- a/admin/spree_admin.gemspec
+++ b/admin/spree_admin.gemspec
@@ -47,4 +47,5 @@ Gem::Specification.new do |s|
s.add_dependency 'responders'
s.add_dependency 'sprockets', '>= 4.0'
s.add_dependency 'tinymce-rails', '~> 7.0'
+ s.add_dependency 'ruby-oembed', '~> 0.18'
end
diff --git a/core/app/models/action_text/video_embed.rb b/core/app/models/action_text/video_embed.rb
new file mode 100644
index 0000000000..641ef3439a
--- /dev/null
+++ b/core/app/models/action_text/video_embed.rb
@@ -0,0 +1,13 @@
+module ActionText
+ class VideoEmbed < ApplicationRecord
+ include ActionText::Attachable
+
+ validates :url, :raw_html, :thumbnail_url, presence: true
+
+ # `to_partial_path` for the storefront render is by default "action_text/video_embeds/video_embed"
+
+ def to_trix_content_attachment_partial_path
+ 'action_text/video_embeds/thumbnail'
+ end
+ end
+end
diff --git a/core/app/views/action_text/video_embeds/_thumbnail.html.erb b/core/app/views/action_text/video_embeds/_thumbnail.html.erb
new file mode 100644
index 0000000000..af06a1f00f
--- /dev/null
+++ b/core/app/views/action_text/video_embeds/_thumbnail.html.erb
@@ -0,0 +1 @@
+<%= image_tag video_embed.thumbnail_url %>
diff --git a/core/app/views/action_text/video_embeds/_video_embed.html.erb b/core/app/views/action_text/video_embeds/_video_embed.html.erb
new file mode 100644
index 0000000000..c701e2f6d0
--- /dev/null
+++ b/core/app/views/action_text/video_embeds/_video_embed.html.erb
@@ -0,0 +1,3 @@
+<div class="video-embed-container mt-4 mb-4">
+ <%= video_embed.raw_html.html_safe %>
+</div>
diff --git a/core/db/migrate/20250217171018_create_action_text_video_embeds.rb b/core/db/migrate/20250217171018_create_action_text_video_embeds.rb
new file mode 100644
index 0000000000..7a6ffb19c3
--- /dev/null
+++ b/core/db/migrate/20250217171018_create_action_text_video_embeds.rb
@@ -0,0 +1,11 @@
+class CreateActionTextVideoEmbeds < ActiveRecord::Migration[6.1]
+ def change
+ create_table :action_text_video_embeds, if_not_exists: true do |t|
+ t.string :url, null: false
+ t.string :thumbnail_url, null: false
+ t.text :raw_html, null: false
+
+ t.timestamps
+ end
+ end
+end
diff --git a/core/spec/jobs/spree/addresses/geocode_address_job_spec.rb b/core/spec/jobs/spree/addresses/geocode_address_job_spec.rb
index ded44f214f..0c37601367 100644
--- a/core/spec/jobs/spree/addresses/geocode_address_job_spec.rb
+++ b/core/spec/jobs/spree/addresses/geocode_address_job_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'
-RSpec.describe Spree::Addresses::GeocodeAddressJob, :vcr do
+RSpec.describe Spree::Addresses::GeocodeAddressJob do
subject(:geocode_address) { described_class.new.perform(address.id) }
let(:address) do