Class: Hyrax::Redirect
- Inherits:
-
Object
- Object
- Hyrax::Redirect
- 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.
Instance Attribute Summary collapse
-
#is_display_url ⇒ Object
readonly
Returns the value of attribute is_display_url.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.wrap(input) ⇒ Hyrax::Redirect?
Build a presenter from a hash.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Equality on attribute values, so
Array#uniqworks as expected. - #as_json ⇒ Object
- #hash ⇒ Object
-
#initialize(path: nil, is_display_url: false) ⇒ Redirect
constructor
Accept nil values so the view can build an empty trailing row.
-
#to_h ⇒ Hash{String => Object}
String-keyed hash matching the persisted shape.
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_url ⇒ Object (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 |
#path ⇒ Object (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.
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_json ⇒ Object
47 48 49 |
# File 'app/models/hyrax/redirect.rb', line 47 def as_json(*) to_h end |
#hash ⇒ Object
60 61 62 |
# File 'app/models/hyrax/redirect.rb', line 60 def hash [path, is_display_url].hash end |
#to_h ⇒ Hash{String => Object}
Returns 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 |