Skip to content

Commit

Permalink
Merge pull request #1133 from sul-dlss/version-row
Browse files Browse the repository at this point in the history
Version links shouldn't have a 'v' in them
  • Loading branch information
justinlittman authored Aug 15, 2024
2 parents 9d2ca6f + bc60b59 commit 3a2f3d6
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 33 deletions.
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-08-09 21:39:57 UTC using RuboCop version 1.65.1.
# on 2024-08-14 21:47:26 UTC using RuboCop version 1.65.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -21,7 +21,7 @@ Metrics/BlockLength:
# Offense count: 3
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 189
Max: 192

# Offense count: 2
# Configuration parameters: AllowedMethods, AllowedPatterns.
Expand Down Expand Up @@ -79,7 +79,7 @@ RSpec/LeadingSubject:
- 'spec/model/content_metadata_spec.rb'
- 'spec/model/iiif_presentation_manifest_spec.rb'

# Offense count: 77
# Offense count: 80
RSpec/MultipleExpectations:
Max: 19

Expand Down
2 changes: 1 addition & 1 deletion app/components/version_actions_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<% if requested_version? %>
You are viewing this version
<% else %>
<a href="<%= url %>">View</a>
<%= link_to 'View', version %>
<% end %>
| <a href="#" data-action="click->clipboard#copy">Copy URL</a>
<% else %>
Expand Down
5 changes: 2 additions & 3 deletions app/components/version_actions_component.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# frozen_string_literal: true

class VersionActionsComponent < ViewComponent::Base
def initialize(version:, requested_version:, url:)
def initialize(version:, requested_version:)
@version = version
@requested_version = requested_version
@url = url
super
end

attr_reader :version, :url
attr_reader :version

delegate :state, to: :version

Expand Down
7 changes: 7 additions & 0 deletions app/components/version_row_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<tr>
<th class="version-identifier">Version <%= version.version_id %></th>
<td class="version-updated-at"><%= updated_at %></td>
<td data-controller="clipboard" data-clipboard-url-value="<%= version_purl_url(id: version.druid, version: version.version_id) %>">
<%= render VersionActionsComponent.new(version:, requested_version:) %>
</td>
</tr>
17 changes: 17 additions & 0 deletions app/components/version_row_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class VersionRowComponent < ViewComponent::Base
with_collection_parameter :version

def initialize(version:, requested_version:)
@version = version
@requested_version = requested_version
super
end

attr_reader :version, :requested_version

def updated_at
l(version.updated_at.to_date, format: :short) if version.updated_at
end
end
14 changes: 3 additions & 11 deletions app/components/versions_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
<%= render SectionComponent.new(label: 'Versions', label_id:) do %>
<%= render TableComponent.new(label_id:) do %>
<% versions.each do |version| %>
<tbody>
<tr>
<th class="version-identifier">Version <%= version.version_id %></th>
<td class="version-updated-at"><%= updated_at(version) %></td>
<td data-controller="clipboard" data-clipboard-url-value="<%= url(version) %>">
<%= render VersionActionsComponent.new(version:, requested_version: @version, url: url(version)) %>
</td>
</tr>
</tbody>
<% end %>
<tbody>
<%= render VersionRowComponent.with_collection(versions, requested_version: @version) %>
</tbody>
<% end %>
<p>Each version has a distinct URL, but you can use this PURL to access the latest version.<br>
<%= link_to embeddable_url, embeddable_url %>
Expand Down
8 changes: 0 additions & 8 deletions app/components/versions_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ def embeddable_url
@embeddable_url ||= helpers.embeddable_url(purl.druid)
end

def updated_at(version)
l(version.updated_at.to_date, format: :short) if version.updated_at
end

def url(version)
versioned_purl_url(id: purl.druid, version: "v#{version.version_id}")
end

def versions
@purl.versions.sort_by(&:version_id).reverse
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/purl_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class ObjectNotReady < StandardError; end

MODS_NS = 'http://www.loc.gov/mods/v3'.freeze

def persisted?
true
end

def head?
head
end
Expand Down
11 changes: 10 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
get ':id/file/:file' => 'purl#file', as: :purl_file
get ':id/metrics' => 'purl#metrics', as: :purl_metrics


resources :purl, only: [:show], path: '/' do
member do
get 'version/:version', to: 'purl#show', as: :versioned
get 'version/:version', to: 'purl#show', as: :version
get 'embed', to: redirect("/iframe/?url=#{Settings.embed.url % { druid: '%{id}' }}")
# These routes should only be used until our viewers support v3 manifests.
# We should aim to only serve a IIIF resource from a single URL.
Expand Down Expand Up @@ -81,3 +82,11 @@

options '/:id/*whatever', to: 'iiif#options'
end

Rails.application.routes.named_routes.path_helpers_module.module_eval do
# @param [PurlVersion] a PurlVersion object
# @return path for the show route
def purl_version_path(purl_version, options = {})
version_purl_path(id: purl_version.druid, version: purl_version.version_id)
end
end
50 changes: 50 additions & 0 deletions spec/components/version_row_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe VersionRowComponent, type: :component do
let(:purl) { PurlResource.new(id: 'wp335yr5649') }

before { render_inline(instance) }

context 'when rendering not the requested version' do
let(:instance) { described_class.new(version: purl.version(:head), requested_version: purl.version(2)) }

it 'renders the version row' do
expect(page).to have_css 'th', text: 'Version 4'
expect(page).to have_css 'td', text: 'Jul 24, 2024'
expect(page).to have_link 'View', href: '/wp335yr5649/version/4'
expect(page).to have_css '[data-clipboard-url-value="http://test.host/wp335yr5649/version/4"]', text: 'Copy URL'
end
end

context 'when rendering the requested version' do
let(:instance) { described_class.new(version: purl.version(2), requested_version: purl.version(2)) }

it 'renders the version row' do
expect(page).to have_css 'th', text: 'Version 2'
expect(page).to have_css 'td', text: 'Jul 22, 2024'
expect(page).to have_css 'td', text: 'You are viewing this version'
expect(page).to have_css '[data-clipboard-url-value="http://test.host/wp335yr5649/version/2"]', text: 'Copy URL'
end
end

context 'when rendering a withdrawn version' do
let(:instance) { described_class.new(version: purl.version(3), requested_version: purl.version(2)) }

it 'renders the version row' do
expect(page).to have_css 'th', text: 'Version 3'
expect(page).to have_css 'td', text: 'Jul 23, 2024'
expect(page).to have_css 'td', text: 'Withdrawn'
end
end

context 'when rendering a permanently withdrawn version' do
let(:instance) { described_class.new(version: purl.version(1), requested_version: purl.version(2)) }

it 'renders the version row' do
expect(page).to have_css 'th', text: 'Version 1'
expect(page).to have_css 'td', text: 'Permanently withdrawn'
end
end
end
7 changes: 1 addition & 6 deletions spec/components/versions_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
expect(page).to have_css '.section-header', text: 'Versions'
expect(page).to have_css 'p', text: 'Each version has a distinct URL, but you can use this PURL to access the latest version.'
expect(page).to have_link 'https://purl.stanford.edu/wp335yr5649'
end

it 'displays versions as expected' do
expect(page.text).to match(/Version 4\s+Jul 24, 2024\s+You are viewing this version | Copy URL/)
expect(page.text).to match(/Version 3\s+Jul 23, 2024\s+Withdrawn/)
expect(page.text).to match(/Version 2\s+Jul 22, 2024\s+View | Copy URL/)
expect(page.text).to match(/Version 1\s+Permanently withdrawn/)
expect(page).to have_css 'table tr', count: 4
end
end

0 comments on commit 3a2f3d6

Please sign in to comment.