Class: Scrivito::Link

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
lib/scrivito/link.rb

Overview

This class provides an interfaces for handling CMS Links. To format a link for rendering in an html page, use the scrivito_path or scrivito_url methods.

API:

  • public

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(link_data) ⇒ Link

Create a new link obj

Parameters:

Options Hash (link_data):

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

API:

  • public



30
31
32
# File 'lib/scrivito/link.rb', line 30

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:

  • the hostname of internal urls

  • the port of internal urls

API:

  • public



17
18
19
# File 'lib/scrivito/link.rb', line 17

def self.parse(url, host, port)
  LinkParser.new(host, port).parse(url)
end

Instance Method Details

#display_title ⇒ Object

Returns the title of this Link if it is set. Otherwise it returns the display_title of the destination object for internal Links or the URL for external Links.

API:

  • public



131
132
133
134
135
136
# File 'lib/scrivito/link.rb', line 131

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 links to an external URL.

Returns:

API:

  • public



146
147
148
# File 'lib/scrivito/link.rb', line 146

def external?
  !internal?
end

#file_extension ⇒ Object

Returns the file extension (e.g. zip, pdf) of this link's (internal or external) target. Returns an empty string if the file extension is can not be determined.

API:

  • public



118
119
120
121
122
123
124
125
# File 'lib/scrivito/link.rb', line 118

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 link's anchor as in "index.html#anchor". See RFC3986 for details (http://www.ietf.org/rfc/rfc3986.txt).

API:

  • public



95
96
97
# File 'lib/scrivito/link.rb', line 95

def fragment
  @link_data[:fragment]
end

#fragment=(value) ⇒ Object

Set the link's anchor as in "index.html#anchor". See RFC3986 for details (http://www.ietf.org/rfc/rfc3986.txt).

Parameters:

  • the anchor or fragement of the link

API:

  • public



103
104
105
# File 'lib/scrivito/link.rb', line 103

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

#internal? ⇒ Boolean

Returns true this Link links to a CMS Object.

Returns:

API:

  • public



140
141
142
# File 'lib/scrivito/link.rb', line 140

def internal?
  url.nil?
end

#obj ⇒ Object

Returns the Obj this link is referencing. May be nil if the link is external.

API:

  • public



52
53
54
# File 'lib/scrivito/link.rb', line 52

def obj
  @link_data[:obj]
end

#obj=(value) ⇒ Object

Set the Obj this link is referencing. May be nil if the link is external.

Parameters:

  • the obj this link should be referencing

API:

  • public



60
61
62
# File 'lib/scrivito/link.rb', line 60

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

#query ⇒ Object

Returns the link's query string as in "index.html?query_string". See RFC3986 for details (http://www.ietf.org/rfc/rfc3986.txt).

API:

  • public



80
81
82
# File 'lib/scrivito/link.rb', line 80

def query
  @link_data[:query]
end

#query=(value) ⇒ Object

Set the link's query string as in "index.html?query_string". See RFC3986 for details (http://www.ietf.org/rfc/rfc3986.txt).

Parameters:

  • the query string of the link

API:

  • public



88
89
90
# File 'lib/scrivito/link.rb', line 88

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

#title ⇒ Object

The link's title.

API:

  • public



66
67
68
# File 'lib/scrivito/link.rb', line 66

def title
  @link_data[:title]
end

#title=(value) ⇒ Object

Set the link's title.

Parameters:

  • the link's title

API:

  • public



73
74
75
# File 'lib/scrivito/link.rb', line 73

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

#url ⇒ Object

The link's external url. Only available for external links. 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.

API:

  • public



38
39
40
# File 'lib/scrivito/link.rb', line 38

def url
  @link_data[:url]
end

#url=(value) ⇒ Object

Set the link's external url. This will lead to an external link.

Parameters:

  • the url of the link

API:

  • public



45
46
47
# File 'lib/scrivito/link.rb', line 45

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