Class: Scrivito::Link
- Inherits:
-
Object
- Object
- Scrivito::Link
- Extended by:
- ActiveModel::Naming
- Defined in:
- app/cms/scrivito/link.rb
Overview
This class represents individual external or internal links Scrivito is able to identify and to process. To format a link for rendering on an HTML page, use the scrivito_path
or scrivito_url
methods.
Class Method Summary collapse
-
.parse(url, host, port) ⇒ Object
Parses a URL and returns a Link object.
Instance Method Summary collapse
-
#alt_description ⇒ Object
The alt description of a
Link
, used by ScrivitoHelper#scrivito_image_tag. -
#display_title ⇒ Object
Returns the title of this Link if it is set.
-
#external? ⇒ Boolean
Returns true if this Link points to an external location.
-
#file_extension ⇒ Object
Returns the file extension (e.g. zip, pdf) of the (internal or external) destination of this link.
-
#fragment ⇒ Object
Returns the anchor of the link as in “index.html#anchor”.
-
#fragment=(value) ⇒ Object
Set the anchor of the link as in “index.html#anchor”.
-
#initialize(link_data) ⇒ Link
constructor
Create a new link object.
-
#internal? ⇒ Boolean
Returns true if this Link points to a CMS object.
-
#obj ⇒ Object
Returns the Obj this link references.
-
#obj=(value) ⇒ Object
Set the Obj this link should reference.
-
#query ⇒ Object
Returns the query string of the link as in “index.html?query_string”.
-
#query=(value) ⇒ Object
Set the query string of the link as in “index.html?query_string”.
-
#target ⇒ Object
Returns the link target.
-
#target=(value) ⇒ Object
Set the link target.
-
#title ⇒ Object
Returns the title of the link.
-
#title=(value) ⇒ Object
Set the link title.
-
#url ⇒ Object
The external URL of the link.
-
#url=(value) ⇒ Object
Set the external URL of the link.
Constructor Details
#initialize(link_data) ⇒ Link
Create a new link object.
33 34 35 |
# File 'app/cms/scrivito/link.rb', line 33 def initialize(link_data) @link_data = link_data.with_indifferent_access end |
Class Method Details
.parse(url, host, port) ⇒ Object
Parses a URL and returns a Scrivito::Link object. Determines internal URLs based on the given hostname
and port
.
18 19 20 |
# File 'app/cms/scrivito/link.rb', line 18 def self.parse(url, host, port) LinkParser.new(host, port).parse(url) end |
Instance Method Details
#alt_description ⇒ Object
The alt description of a Link
, used by ScrivitoHelper#scrivito_image_tag.
By default, this method returns the title
of this Link
. If title
is nil
and this Link
references an Obj, the alt_description
of that Obj is used.
153 154 155 156 |
# File 'app/cms/scrivito/link.rb', line 153 def alt_description return title if title obj.alt_description if internal? end |
#display_title ⇒ Object
Returns the title of this Link if it is set. If not, the display_title
of the destination object is returned for internal links, or the url
for external links.
139 140 141 142 143 144 |
# File 'app/cms/scrivito/link.rb', line 139 def display_title dt = title dt = obj.display_title if dt.blank? && !external? dt = url if dt.blank? dt end |
#external? ⇒ Boolean
Returns true if this Link points to an external location.
166 167 168 |
# File 'app/cms/scrivito/link.rb', line 166 def external? !internal? end |
#file_extension ⇒ Object
Returns the file extension (e.g. zip, pdf) of the (internal or external) destination of this link. Returns an empty string if the file extension cannot be determined.
127 128 129 130 131 132 133 134 |
# File 'app/cms/scrivito/link.rb', line 127 def file_extension if internal? obj ? obj.file_extension : "" else path = URI.parse(url).path rescue nil path.blank? ? "" : File.extname(path)[1..-1] || "" end end |
#fragment ⇒ Object
Returns the anchor of the link as in “index.html#anchor”. See RFC3986 for details (www.ietf.org/rfc/rfc3986.txt).
96 97 98 |
# File 'app/cms/scrivito/link.rb', line 96 def fragment @link_data[:fragment] end |
#fragment=(value) ⇒ Object
Set the anchor of the link as in “index.html#anchor”. See RFC3986 for details (www.ietf.org/rfc/rfc3986.txt).
104 105 106 |
# File 'app/cms/scrivito/link.rb', line 104 def fragment=(value) @link_data[:fragment] = value end |
#internal? ⇒ Boolean
Returns true if this Link points to a CMS object.
160 161 162 |
# File 'app/cms/scrivito/link.rb', line 160 def internal? url.nil? end |
#obj ⇒ Object
Returns the Obj this link references. Returns nil
if the link is external.
54 55 56 |
# File 'app/cms/scrivito/link.rb', line 54 def obj @link_data[:obj] end |
#obj=(value) ⇒ Object
Set the Obj this link should reference.
61 62 63 |
# File 'app/cms/scrivito/link.rb', line 61 def obj=(value) @link_data[:obj] = value end |
#query ⇒ Object
Returns the query string of the link as in “index.html?query_string”. See RFC3986 for details (www.ietf.org/rfc/rfc3986.txt).
81 82 83 |
# File 'app/cms/scrivito/link.rb', line 81 def query @link_data[:query] end |
#query=(value) ⇒ Object
Set the query string of the link as in “index.html?query_string”. See RFC3986 for details (www.ietf.org/rfc/rfc3986.txt).
89 90 91 |
# File 'app/cms/scrivito/link.rb', line 89 def query=(value) @link_data[:query] = value end |
#target ⇒ Object
Returns the link target. “target” refers to the equally-named HTML attribute, not the link destination.
111 112 113 |
# File 'app/cms/scrivito/link.rb', line 111 def target @link_data[:target] end |
#target=(value) ⇒ Object
Set the link target. “target” refers to the equally-named HTML attribute, not the link destination.
119 120 121 |
# File 'app/cms/scrivito/link.rb', line 119 def target=(value) @link_data[:target] = value end |
#title ⇒ Object
Returns the title of the link.
67 68 69 |
# File 'app/cms/scrivito/link.rb', line 67 def title @link_data[:title] end |
#title=(value) ⇒ Object
Set the link title.
74 75 76 |
# File 'app/cms/scrivito/link.rb', line 74 def title=(value) @link_data[:title] = value end |
#url ⇒ Object
The external URL of the link. Returns nil
if the link is internal. Warning: Do not output the URL directly unless you know what you are doing. Normally, you want to use the scrivito_path
or scrivito_url
methods to format a link.
41 42 43 |
# File 'app/cms/scrivito/link.rb', line 41 def url @link_data[:url] end |
#url=(value) ⇒ Object
Set the external URL of the link. This causes the link to become an external link.
48 49 50 |
# File 'app/cms/scrivito/link.rb', line 48 def url=(value) @link_data[:url] = value end |