Class: Hyrax::Redirect

Inherits:
Object
  • Object
show all
Defined in:
app/models/hyrax/redirect.rb

Overview

Wraps a single redirect entry for use in form views, providing .path and .is_display_url readers over the underlying persisted hash.

Redirects are persisted as plain hashes on the parent work or collection. Form-render code calls Hyrax::Redirect.wrap(entry) to get a value object the view can call methods on. Other code (the validator, indexer, sync step) reads the persisted hash directly.

Examples:

Hyrax::Redirect.new(path: '/handle/12345/678', is_display_url: false)
Hyrax::Redirect.wrap('path' => '/foo', 'is_display_url' => true)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, is_display_url: false) ⇒ Redirect

Accept nil values so the view can build an empty trailing row.



21
22
23
24
# File 'app/models/hyrax/redirect.rb', line 21

def initialize(path: nil, is_display_url: false)
  @path = path
  @is_display_url = is_display_url
end

Instance Attribute Details

#is_display_urlObject (readonly)

Returns the value of attribute is_display_url.



17
18
19
# File 'app/models/hyrax/redirect.rb', line 17

def is_display_url
  @is_display_url
end

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'app/models/hyrax/redirect.rb', line 17

def path
  @path
end

Class Method Details

.wrap(input) ⇒ Hyrax::Redirect?

Build a presenter from a hash. If passed a presenter, returns it unchanged. Returns nil for nil input. Accepts string or symbol keys.

Parameters:

Returns:

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
# File 'app/models/hyrax/redirect.rb', line 32

def self.wrap(input)
  return nil if input.nil?
  return input if input.is_a?(Hyrax::Redirect)
  raise ArgumentError, "cannot wrap #{input.class} as Hyrax::Redirect" unless input.respond_to?(:to_h)

  h = input.to_h.transform_keys(&:to_s)
  new(path: h['path'], is_display_url: h.fetch('is_display_url', false))
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Equality on attribute values, so Array#uniq works as expected.



53
54
55
56
57
# File 'app/models/hyrax/redirect.rb', line 53

def ==(other)
  other.is_a?(Hyrax::Redirect) &&
    other.path == path &&
    other.is_display_url == is_display_url
end

#as_jsonObject



47
48
49
# File 'app/models/hyrax/redirect.rb', line 47

def as_json(*)
  to_h
end

#hashObject



60
61
62
# File 'app/models/hyrax/redirect.rb', line 60

def hash
  [path, is_display_url].hash
end

#to_hHash{String => Object}

Returns string-keyed hash matching the persisted shape.

Returns:

  • (Hash{String => Object})

    string-keyed hash matching the persisted shape.



43
44
45
# File 'app/models/hyrax/redirect.rb', line 43

def to_h
  { 'path' => path, 'is_display_url' => is_display_url }
end