Class: Hyrax::RedirectsLookup

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/redirects_lookup.rb

Overview

Single point of truth for read-side queries against the hyrax_redirect_paths table. The unique index on from_path makes both lookups (.find_row and .taken?) indexed equality checks on a small derived table — sub-millisecond against any reasonable load.

See documentation/redirects.md.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, except_id: nil) ⇒ RedirectsLookup

Returns a new instance of RedirectsLookup.



47
48
49
50
# File 'app/services/hyrax/redirects_lookup.rb', line 47

def initialize(path, except_id: nil)
  @path = Hyrax::RedirectPathNormalizer.call(path)
  @except_id = except_id
end

Class Method Details

.display_path_for(resource_id) ⇒ String?

Returns the from_path of the row marked as the resource's display URL, or nil when no row is marked.

Parameters:

  • resource_id (String)

Returns:

  • (String, nil)


42
43
44
45
# File 'app/services/hyrax/redirects_lookup.rb', line 42

def self.display_path_for(resource_id)
  return nil if resource_id.blank?
  Hyrax::RedirectPath.where(resource_id: resource_id, is_display_url: true).limit(1).pick(:from_path)
end

.find_row(path) ⇒ Hyrax::RedirectPath?

Returns the Hyrax::RedirectPath row matching the given from_path, or nil when no row matches. Used by request-time resolvers (Hyrax::RedirectsController and the Hyrax::RedirectToDisplayUrl before_action) to decide whether the visited path triggers a redirect or in-process dispatch.

Parameters:

  • path (String)

    the visited path, normalized or not

Returns:



31
32
33
34
35
# File 'app/services/hyrax/redirects_lookup.rb', line 31

def self.find_row(path)
  normalized = Hyrax::RedirectPathNormalizer.call(path)
  return nil if normalized.blank?
  Hyrax::RedirectPath.find_by(from_path: normalized)
end

.taken?(path, except_id: nil) ⇒ Boolean

Returns true if any row exists with the given from_path, optionally excluding rows owned by except_id. Used by Hyrax::RedirectValidator at form-submit time to surface "this alias is already in use" before the DB unique index would reject the insert.

Parameters:

  • path (String)

    the alias to check, normalized or not

  • except_id (String, nil) (defaults to: nil)

    resource_id to exclude from the check

Returns:

  • (Boolean)


19
20
21
# File 'app/services/hyrax/redirects_lookup.rb', line 19

def self.taken?(path, except_id: nil)
  new(path, except_id: except_id).taken?
end

Instance Method Details

#taken?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'app/services/hyrax/redirects_lookup.rb', line 52

def taken?
  return false if @path.blank?
  scope = Hyrax::RedirectPath.where(from_path: @path)
  scope = scope.where.not(resource_id: @except_id) if @except_id.present?
  scope.exists?
end