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.

Instance Method Summary collapse

Instance Method Details

#active?Boolean

An internal Link is active if it’s destination object is active. An external Link is always active.

Returns:

  • (Boolean)


120
121
122
# File 'lib/rails_connector/link.rb', line 120

def active?
  external? || destination_object&.active?
end

#destination_objectObject

Returns the destination object (Obj) of this Link.



134
135
136
# File 'lib/rails_connector/link.rb', line 134

def destination_object
  @destination_object ||= Obj.find(destination) if resolved_internal?
end

#destination_object_idObject

Returns the id of the Links’ destination_object.



90
91
92
# File 'lib/rails_connector/link.rb', line 90

def destination_object_id
  destination
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.



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

def display_title
  dt = title
  dt = destination_object.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)


113
114
115
# File 'lib/rails_connector/link.rb', line 113

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.



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rails_connector/link.rb', line 75

def file_extension
  if internal?
    destination_object ? destination_object.file_extension : ""
  else
    path = begin
             URI.parse(url).path
           rescue StandardError
             nil
           end
    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).



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

def fragment
  @link_data[:fragment]
end

#internal?Boolean

Returns true this Link links to a CMS Object.

Returns:

  • (Boolean)


107
108
109
# File 'lib/rails_connector/link.rb', line 107

def internal?
  url.nil?
end

#queryObject

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



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

def query
  @link_data[:search]
end

#searchObject

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



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

def search
  query
end

#targetObject

Returns the browser window or browser frame to be used as a target for this link. Example: Links that should be opened in a new window will return “_blank” as their target.



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

def target
  @link_data[:target]
end

#titleObject

The link’s title.



23
24
25
# File 'lib/rails_connector/link.rb', line 23

def title
  @link_data[:title]
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.



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

def url
  @link_data[:url]
end