Skip to content

Commit

Permalink
Merge pull request #1137 from sul-dlss/t1135-embed-url
Browse files Browse the repository at this point in the history
Don't include version in sul-embed iframe URL when purl is base url
  • Loading branch information
justinlittman authored Aug 16, 2024
2 parents b7adf0a + 22f8133 commit 405af5d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ def iframe_url(druid, version_id = nil)
def embeddable_url(druid, version_id = nil)
if Settings.embed.url
(Settings.embed.url % { druid: }).tap do |embed_url|
return "#{embed_url}/version/#{version_id}" if version_id.present?
return "#{embed_url}/version/#{version_id}" if version_id.present? && request.path == version_purl_path(druid, version_id)
end
else
version_id.present? ? versioned_purl_url(druid, version_id) : purl_url(druid)
version_id.present? ? version_purl_url(druid, version_id) : purl_url(druid)
end
end

Expand Down
44 changes: 36 additions & 8 deletions spec/views/purl/_embed.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,69 @@
RSpec.describe 'purl/_embed' do
before { assign(:purl, purl) }

let(:purl) { PurlResource.new(id: 'xyz') }
let(:purl) { PurlResource.new(id: 'bf973rp9392') }

it 'displays a purl embed viewer' do
render
expect(rendered).to have_css '.purl-embed-viewer'
embed = Nokogiri::HTML(rendered).css('.purl-embed-viewer')
expect(embed.attr('data-oembed-provider').to_s).to eq 'https://embed.stanford.edu/embed.json?hide_title=true&hide_metadata=true'
expect(embed.attr('href').to_s).to eq('https://purl.stanford.edu/xyz')
expect(embed.attr('href').to_s).to eq('https://purl.stanford.edu/bf973rp9392')
end

it 'displays a non-javascript fallback' do
render
expect(rendered).to have_css 'noscript iframe'
iframe = Nokogiri::HTML(rendered).xpath('//iframe')
expect(iframe.attr('src').to_s).to eq 'https://embed.stanford.edu/iframe/?url=https%3A%2F%2Fpurl.stanford.edu%2Fxyz'
expect(iframe.attr('src').to_s).to eq 'https://embed.stanford.edu/iframe/?url=https%3A%2F%2Fpurl.stanford.edu%2Fbf973rp9392'
end

context 'when a version is specified' do
before { assign(:version, version) }
let(:version) { PurlVersion.new(id: 'bf973rp9392', head: true, state: 'available', version_id: 2) }

let(:version) { PurlVersion.new(id: 'xyz', head: true, state: 'available', version_id: 2) }
before do
assign(:version, version)
allow_any_instance_of(ActionDispatch::Request).to receive(:path).and_return('/bf973rp9392/version/2') # rubocop:disable RSpec/AnyInstance
allow(purl).to receive(:version).with(2).and_return(version)
end

it 'displays a purl embed viewer with the version' do
render
expect(rendered).to have_css '.purl-embed-viewer'
embed = Nokogiri::HTML(rendered).css('.purl-embed-viewer')
expect(embed.attr('data-oembed-provider').to_s).to eq 'https://embed.stanford.edu/embed.json?hide_title=true&hide_metadata=true'
expect(embed.attr('href').to_s).to eq('https://purl.stanford.edu/bf973rp9392/version/2')
end

it 'displays a non-javascript fallback' do
render
expect(rendered).to have_css 'noscript iframe'
iframe = Nokogiri::HTML(rendered).xpath('//iframe')
expect(iframe.attr('src').to_s).to eq 'https://embed.stanford.edu/iframe/?url=https%3A%2F%2Fpurl.stanford.edu%2Fbf973rp9392%2Fversion%2F2'
end
end

context 'when a version is not specified' do
let(:version) { PurlVersion.new(id: 'bf973rp9392', head: true, state: 'available', version_id: 1) }

before do
allow(purl).to receive(:version).and_return(version)
allow_any_instance_of(ActionDispatch::Request).to receive(:path).and_return('/bf973rp9392') # rubocop:disable RSpec/AnyInstance
end

it 'displays a purl embed viewer' do
it 'does not display a purl embed viewer with a version' do
render
expect(rendered).to have_css '.purl-embed-viewer'
embed = Nokogiri::HTML(rendered).css('.purl-embed-viewer')
expect(embed.attr('data-oembed-provider').to_s).to eq 'https://embed.stanford.edu/embed.json?hide_title=true&hide_metadata=true'
expect(embed.attr('href').to_s).to eq('https://purl.stanford.edu/xyz/version/2')
expect(embed.attr('href').to_s).to eq('https://purl.stanford.edu/bf973rp9392')
end

it 'displays a non-javascript fallback' do
render
expect(rendered).to have_css 'noscript iframe'
iframe = Nokogiri::HTML(rendered).xpath('//iframe')
expect(iframe.attr('src').to_s).to eq 'https://embed.stanford.edu/iframe/?url=https%3A%2F%2Fpurl.stanford.edu%2Fxyz%2Fversion%2F2'
expect(iframe.attr('src').to_s).to eq 'https://embed.stanford.edu/iframe/?url=https%3A%2F%2Fpurl.stanford.edu%2Fbf973rp9392'
end
end
end

0 comments on commit 405af5d

Please sign in to comment.