-
Notifications
You must be signed in to change notification settings - Fork 135
Redirects normalize on write #7435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,38 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Hyrax | ||
| # Normalizes redirect path entries on assignment so every write path — | ||
| # form submissions, console writes, importers, change-set saves — | ||
| # produces canonical data. Read-side consumers can trust the persisted | ||
| # shape and skip their own normalization. | ||
| # | ||
| # Included next to Hyrax::Schema(:redirects) on Hyrax::Work and | ||
| # Hyrax::PcdmCollection. The override sits on Valkyrie's set_value | ||
| # primitive so it fires under both flex modes (the simple schema's | ||
| # generated setter and the m3 singleton-class setter both route through | ||
| # set_value). | ||
| # | ||
| # See documentation/redirects.md. | ||
| module RedirectsNormalization | ||
| extend ActiveSupport::Concern | ||
|
|
||
| def set_value(key, value) | ||
| value = normalize_redirects(value) if key.to_sym == :redirects | ||
| super | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def normalize_redirects(value) | ||
| Array(value).map { |entry| normalize_entry(entry) } | ||
| end | ||
|
|
||
| def normalize_entry(entry) | ||
| return entry unless entry.is_a?(Hash) | ||
| normalized = entry.dup | ||
| path_key = normalized.key?(:path) ? :path : 'path' | ||
| normalized[path_key] = Hyrax::RedirectPathNormalizer.call(normalized[path_key]) | ||
| normalized | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
86 changes: 86 additions & 0 deletions
86
spec/models/concerns/hyrax/redirects_normalization_spec.rb
This file contains hidden or 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,86 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Hyrax::RedirectsNormalization do | ||
| let(:resource_class) do | ||
| Class.new(Valkyrie::Resource) do | ||
| attribute :redirects, Valkyrie::Types::Array.of(Valkyrie::Types::Hash) | ||
| include Hyrax::RedirectsNormalization | ||
| end | ||
| end | ||
|
|
||
| let(:resource) { resource_class.new } | ||
|
|
||
| describe 'normalization on write' do | ||
| it 'strips trailing slashes' do | ||
| resource.redirects = [{ 'path' => '/foo/' }] | ||
| expect(resource.redirects.first['path']).to eq('/foo') | ||
| end | ||
|
|
||
| it 'adds a leading slash' do | ||
| resource.redirects = [{ 'path' => 'handle/123' }] | ||
| expect(resource.redirects.first['path']).to eq('/handle/123') | ||
| end | ||
|
|
||
| it 'extracts path from a full URL with query string' do | ||
| resource.redirects = [{ 'path' => 'https://old.example.edu/handle/123?utm=email' }] | ||
| expect(resource.redirects.first['path']).to eq('/handle/123') | ||
| end | ||
|
|
||
| it 'accepts symbol keys' do | ||
| resource.redirects = [{ path: '/foo/' }] | ||
| expect(resource.redirects.first[:path]).to eq('/foo') | ||
| end | ||
|
|
||
| it 'preserves non-path keys on the entry' do | ||
| resource.redirects = [{ 'path' => '/foo/', 'canonical' => true, 'sequence' => 0 }] | ||
| entry = resource.redirects.first | ||
| expect(entry['path']).to eq('/foo') | ||
| expect(entry['canonical']).to be true | ||
| expect(entry['sequence']).to eq(0) | ||
| end | ||
|
|
||
| it 'normalizes every entry in a multi-entry array' do | ||
| resource.redirects = [{ 'path' => '/foo/' }, { 'path' => 'bar' }] | ||
| expect(resource.redirects.map { |e| e['path'] }).to eq(['/foo', '/bar']) | ||
| end | ||
|
|
||
| it 'is idempotent — re-assigning normalized values is a no-op' do | ||
| resource.redirects = [{ 'path' => '/foo/' }] | ||
| first = resource.redirects | ||
| resource.redirects = first | ||
| expect(resource.redirects).to eq(first) | ||
| end | ||
|
|
||
| it 'leaves non-redirects attributes untouched' do | ||
| other_class = Class.new(Valkyrie::Resource) do | ||
| attribute :title, Valkyrie::Types::String | ||
| attribute :redirects, Valkyrie::Types::Array.of(Valkyrie::Types::Hash) | ||
| include Hyrax::RedirectsNormalization | ||
| end | ||
| r = other_class.new(title: 'A title that ends with /') | ||
| expect(r.title).to eq('A title that ends with /') | ||
| end | ||
| end | ||
|
|
||
| describe 'flex-mode behavior (m3 singleton-class setter)' do | ||
| let(:flex_class) do | ||
| Class.new(Valkyrie::Resource) do | ||
| include Hyrax::Flexibility | ||
| attribute :redirects, Valkyrie::Types::Array.of(Valkyrie::Types::Hash) | ||
| include Hyrax::RedirectsNormalization | ||
| end | ||
| end | ||
|
|
||
| it 'fires under the flex singleton-class setter path' do | ||
| # Mirrors how Hyrax::Flexibility.load attaches the schema to the | ||
| # singleton class before set_value is called. | ||
| resource = flex_class.allocate | ||
| resource.send(:initialize, {}) | ||
| resource.singleton_class.attributes(redirects: Valkyrie::Types::Array.of(Valkyrie::Types::Hash)) | ||
|
|
||
| resource.set_value(:redirects, [{ 'path' => '/foo/' }]) | ||
|
|
||
| expect(resource.redirects.first['path']).to eq('/foo') | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.