Class: RailsConnector::Link

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

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(link_data) ⇒ Link

Create a new link obj

Parameters:

  • link_data (Hash)

Options Hash (link_data):

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


28
29
30
# File 'lib/rails_connector/link.rb', line 28

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 RailsConnector::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



15
16
17
# File 'lib/rails_connector/link.rb', line 15

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

Instance Method Details

#active?Boolean

An internal Link is active if its destination obj is active. An external Link is always active. @ deprecated this method will be removed without substitution

Returns:

  • (Boolean)


160
161
162
163
# File 'lib/rails_connector/link.rb', line 160

def active?
  Deprecation.warn_method('Link#active?')
  external? || (obj && obj.active?)
end

#destination_objectObject

Deprecated.

use #obj instead

Returns the destination obj (Obj) of this Link. May be nil if the link is external or internal without an existing destination object.



173
174
175
176
# File 'lib/rails_connector/link.rb', line 173

def destination_object
  Deprecation.warn_method('Link#destination_object', :obj)
  obj
end

#destination_object_idObject

Deprecated.

use #obj.id instead

Returns the id of the Links’ destination obj.



128
129
130
131
# File 'lib/rails_connector/link.rb', line 128

def destination_object_id
  Deprecation.warn_method('Link#destination_object_id', 'obj.id')
  obj.id
end

#display_titleObject

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.



137
138
139
140
141
142
# File 'lib/rails_connector/link.rb', line 137

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:

  • (Boolean)


152
153
154
# File 'lib/rails_connector/link.rb', line 152

def external?
  !internal?
end

#file_extensionObject

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.



116
117
118
119
120
121
122
123
# File 'lib/rails_connector/link.rb', line 116

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



93
94
95
# File 'lib/rails_connector/link.rb', line 93

def fragment
  @link_data[:fragment]
end

#fragment=(value) ⇒ Object

Set the link’s anchor as in “index.html#anchor”. See RFC3986 for details (www.ietf.org/rfc/rfc3986.txt).

Parameters:

  • value (String)

    the anchor or fragement of the link



101
102
103
# File 'lib/rails_connector/link.rb', line 101

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

#internal?Boolean

Returns true this Link links to a CMS Object.

Returns:

  • (Boolean)


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

def internal?
  url.nil?
end

#objObject

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



50
51
52
# File 'lib/rails_connector/link.rb', line 50

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:

  • value (Obj)

    the obj this link should be referencing



58
59
60
# File 'lib/rails_connector/link.rb', line 58

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

#queryObject

Returns the link’s query string as in “index.html?query_string”. See RFC3986 for details (www.ietf.org/rfc/rfc3986.txt).



78
79
80
# File 'lib/rails_connector/link.rb', line 78

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 (www.ietf.org/rfc/rfc3986.txt).

Parameters:

  • value (String)

    the query string of the link



86
87
88
# File 'lib/rails_connector/link.rb', line 86

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

#titleObject

The link’s title.



64
65
66
# File 'lib/rails_connector/link.rb', line 64

def title
  @link_data[:title]
end

#title=(value) ⇒ Object

Set the link’s title.

Parameters:

  • value (String)

    the link’s title



71
72
73
# File 'lib/rails_connector/link.rb', line 71

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

#urlObject

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 cms_path or cms_url methods to format a link.



36
37
38
# File 'lib/rails_connector/link.rb', line 36

def url
  @link_data[:url]
end

#url=(value) ⇒ Object

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

Parameters:

  • value (String)

    the url of the link



43
44
45
# File 'lib/rails_connector/link.rb', line 43

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