Class: Scrivito::Link

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(link_data) ⇒ Link

Create a new link object.

Examples:

Set the ‘current_campaign’ link of an object:

obj.update(current_campaign: Scrivito::Link.new(obj: Obj.find('55dd1ce50ecc41c8')))

Parameters:

  • link_data (Hash)

Options Hash (link_data):

  • url (String)
  • obj (Obj)
  • title (String)
  • query (String)
  • target (String)
  • fragment (String)


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.

Parameters:

  • url (String)
  • host (String)

    the hostname of internal urls

  • port (String)

    the port of internal urls



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_descriptionObject

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_titleObject

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.

Returns:

  • (Boolean)


166
167
168
# File 'app/cms/scrivito/link.rb', line 166

def external?
  !internal?
end

#file_extensionObject

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

#fragmentObject

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).

Parameters:

  • value (String)

    the anchor or fragment of the link



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.

Returns:

  • (Boolean)


160
161
162
# File 'app/cms/scrivito/link.rb', line 160

def internal?
  url.nil?
end

#objObject

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.

Parameters:

  • value (Obj)

    the obj this link should point to



61
62
63
# File 'app/cms/scrivito/link.rb', line 61

def obj=(value)
  @link_data[:obj] = value
end

#queryObject

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).

Parameters:

  • value (String)

    the query string of the link



89
90
91
# File 'app/cms/scrivito/link.rb', line 89

def query=(value)
  @link_data[:query] = value
end

#targetObject

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.

Parameters:

  • value (String)

    the target of the link



119
120
121
# File 'app/cms/scrivito/link.rb', line 119

def target=(value)
  @link_data[:target] = value
end

#titleObject

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.

Parameters:

  • value (String)

    the title of the link



74
75
76
# File 'app/cms/scrivito/link.rb', line 74

def title=(value)
  @link_data[:title] = value
end

#urlObject

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.

Parameters:

  • value (String)

    the url of the link



48
49
50
# File 'app/cms/scrivito/link.rb', line 48

def url=(value)
  @link_data[:url] = value
end