Skip to content

Commit

Permalink
Extract an embed component for rendering the oembed markup.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Sep 24, 2024
1 parent d9c65d5 commit 0efed76
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 29 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ jobs:
rails_version: ['7.1.4', '7.2.1']
blacklight_version: ['~> 7.0']
additional_name: ['']
legacy_config: ['']
include:
- ruby: '3.3'
rails_version: '7.2.1'
blacklight_version: '~> 8.0'
additional_name: ''
- ruby: '3.3'
rails_version: '7.2.1'
blacklight_version: '~> 8.0'
additional_name: 'with legacy partial config'
legacy_config: 'true'
env:
RAILS_VERSION: ${{ matrix.rails_version }}
BLACKLIGHT_VERSION: ${{ matrix.blacklight_version }}
CI_TEST_LEGACY_CONFIGURATION: ${{ matrix.legacy_config }}
steps:
- uses: actions/checkout@v2
- name: Set up Ruby ${{ matrix.ruby }}
Expand Down
2 changes: 1 addition & 1 deletion .solr_wrapper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# port: 8983
version: 9.6.1
collection:
dir: lib/generators/blacklight/templates/solr/conf
dir: .internal_test_app/solr/conf
name: blacklight-core
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= tag.div class: class_names(classes) do %>
<%= embed %>
<% end %>
60 changes: 60 additions & 0 deletions app/components/blacklight/oembed/document_oembed_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module Blacklight
module Oembed
class DocumentOembedComponent < Blacklight::Component
attr_reader :document, :presenter, :classes

def initialize(document:, presenter:, classes: ['oembed-widget'], **kwargs)
super

@document = document
@presenter = presenter
@classes = classes
end

def embed
return if embed_url.blank?

@embed ||= if Blacklight::Oembed::Engine.config.render_helper != :render_oembed_tag_async && Blacklight::Oembed::Engine.config.render_helper != :render_oembed_tag_embed
legacy_helper_method_embed_markup
elsif view_config.render_oembed_using_async_javascript || Blacklight::Oembed::Engine.config.render_helper == :render_oembed_tag_async
async_embed_markup
else
inline_embed_markup
end
end

def render?
embed.present?
end

private

def view_config
presenter.view_config
end

def embed_url
document.first(view_config.oembed_field)
end

def async_embed_url(**kwargs)
helpers.blacklight_oembed_engine.embed_url(**kwargs)
end

def async_embed_markup
content_tag :div, '', data: { async_embed_url: async_embed_url(url: embed_url) }
end

def inline_embed_markup
OEmbed::Providers.get(embed_url).html.html_safe
rescue OEmbed::NotFound
link_to t(:'blacklight_oembed.catalog.view'), embed_url
end

def legacy_helper_method_embed_markup
Blacklight::Oembed.deprecator.warn('Subclass Blacklight::Oembed::DocumentOembedComponent instead of using the blacklight-oembed render_helper config')
helpers.call(Blacklight::Oembed::Engine.config.render_helper, url)
end
end
end
end
27 changes: 15 additions & 12 deletions app/helpers/blacklight/oembed/oembed_helper.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
module Blacklight::Oembed
module OembedHelper

def render_oembed_solr_document_tag document
# @deprecated
def render_oembed_solr_document_tag(document)
url = document.first(blacklight_config.show.oembed_field)
return if url.blank?

render_oembed_tag url
end

def render_oembed_tag url
# @deprecated
def render_oembed_tag(url)
send Blacklight::Oembed::Engine.config.render_helper, url
end
Blacklight::Oembed.deprecator.deprecate_methods Blacklight::Oembed::OembedHelper,
:render_oembed_solr_document_tag, :render_oembed_tag,
'Use Blacklight::Oembed::DocumentOembedComponent instead'

private
def render_oembed_tag_embed url
begin
OEmbed::Providers.get(url).html.html_safe
rescue OEmbed::NotFound
link_to t(:'blacklight_oembed.catalog.view'), url
end

def render_oembed_tag_embed(url)
OEmbed::Providers.get(url).html.html_safe
rescue OEmbed::NotFound
link_to t(:'blacklight_oembed.catalog.view'), url
end

def render_oembed_tag_async url
content_tag :div, "", data: { embed_url: blacklight_oembed_engine.embed_url(url: url) }
def render_oembed_tag_async(url)
content_tag :div, '', data: { embed_url: blacklight_oembed_engine.embed_url(url: url) }
end
end
end
end
6 changes: 1 addition & 5 deletions app/views/catalog/_oembed_default.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
<% if rendered = render_oembed_solr_document_tag(document) %>
<div class="oembed-widget">
<%= rendered %>
</div>
<% end %>
<%= render Blacklight::Oembed::DocumentOembedComponent.new(document: document, presenter: document_presenter(document)) %>
2 changes: 1 addition & 1 deletion blacklight-oembed.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "rails"
spec.add_dependency 'blacklight', '>= 7.0', '< 9'
spec.add_dependency 'blacklight', '>= 7.25', '< 9'
spec.add_dependency "ruby-oembed"

spec.add_development_dependency "bundler", ">= 1.5"
Expand Down
6 changes: 5 additions & 1 deletion lib/blacklight/oembed.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
require "blacklight/oembed/version"
require 'blacklight/oembed/version'
require 'oembed'
module Blacklight
module Oembed
def self.deprecator
@deprecator ||= ActiveSupport::Deprecation.new('2.0.0', 'blacklight-oembed')
end

require 'blacklight/oembed/engine'
end
end
6 changes: 3 additions & 3 deletions lib/blacklight/oembed/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
module Blacklight
module Oembed
class Engine < Rails::Engine
# @deprecated
config.render_helper = :render_oembed_tag_async

Blacklight::Oembed::Engine.config.render_helper = :render_oembed_tag_async
##
# Allows an adopter to pass additional parameters through to an OEmbed
# service. Examples of this could include `:canvas_index`, or `:max_width`
Blacklight::Oembed::Engine.config.additional_params = []

config.additional_params = []
end
end
end
17 changes: 13 additions & 4 deletions lib/generators/blacklight_oembed/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ def inject_oembed_configuration
end

def configuration
inject_into_file 'app/controllers/catalog_controller.rb', after: "configure_blacklight do |config|" do
<<-EOF
inject_into_file 'app/controllers/catalog_controller.rb', after: 'configure_blacklight do |config|' do
if ENV['CI_TEST_LEGACY_CONFIGURATION'].present? || Gem::Version.new(Blacklight::VERSION) < Gem::Version.new('8.0')
<<-EOF
config.show.oembed_field = :oembed_url_ssm
config.show.partials.insert(1, :oembed)
EOF
else
<<-EOF
config.show.oembed_field = :oembed_url_ssm
config.show.partials.insert(1, :oembed)
EOF
config.show.render_oembed_using_async_javascript = true
config.show.embed_component = Blacklight::Oembed::DocumentOembedComponent
EOF
end
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/controllers/embed_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
render_views

before do
Blacklight::Oembed::Engine.config.render_helper = :render_oembed_tag_embed
Blacklight::Oembed::Engine.config.additional_params = [:canvas_index, :suggested_search]
Blacklight::Oembed::Engine.config.additional_params = %i[canvas_index suggested_search]
end

let :oembed_obj do
Expand Down
17 changes: 17 additions & 0 deletions spec/features/embed_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

RSpec.describe 'Embed', type: :feature do
before do
CatalogController.blacklight_config.show.oembed_field = :id
end

it 'embeds the widget on the show page' do
visit '/catalog/2007020969'

expect(page).to have_css '.oembed-widget'

embed_div = find('.oembed-widget div')

expect(embed_div['data-async-embed-url']).to include '/oembed/embed?url=2007020969'
end
end

0 comments on commit 0efed76

Please sign in to comment.