-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from projectblacklight/component
Extract an embed component for rendering the oembed markup.
- Loading branch information
Showing
12 changed files
with
127 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
app/components/blacklight/oembed/document_oembed_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
60
app/components/blacklight/oembed/document_oembed_component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |