2010-01-14 10:30:53 +00:00
|
|
|
class Link < Item
|
|
|
|
|
2013-10-23 10:28:09 +00:00
|
|
|
field :url, localize: true
|
2013-10-30 10:53:23 +00:00
|
|
|
|
2013-10-31 06:09:59 +00:00
|
|
|
validates :url, at_least_one: true, :format => /^(http|https):\/\/(([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|localhost(:[0-9]{1,5})?(\/.*)?/i
|
2010-01-14 10:30:53 +00:00
|
|
|
|
2012-05-14 13:43:10 +00:00
|
|
|
before_validation :add_http
|
2011-04-13 10:19:51 +00:00
|
|
|
|
2010-01-14 10:30:53 +00:00
|
|
|
def link
|
2011-04-13 10:19:51 +00:00
|
|
|
ApplicationController.helpers.link_to(self.name, self.url)
|
2010-01-14 10:30:53 +00:00
|
|
|
end
|
2012-05-14 13:43:10 +00:00
|
|
|
|
2013-10-23 10:28:09 +00:00
|
|
|
def urls
|
2013-10-30 10:53:23 +00:00
|
|
|
if self.url_translations.present?
|
|
|
|
self.url_translations.inject({}) do |result, (key, value)|
|
|
|
|
result["url-#{key}"] = value
|
|
|
|
result
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
2013-10-23 10:28:09 +00:00
|
|
|
end
|
|
|
|
|
2012-05-14 13:43:10 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def add_http
|
2013-10-23 10:28:09 +00:00
|
|
|
if self.url_translations.present?
|
2013-10-30 10:53:23 +00:00
|
|
|
self.url_translations = self.url_translations.inject({}) do |translations, (key, value)|
|
|
|
|
if value.blank? || value[/^(http|https):\/\//]
|
|
|
|
translations[key] = value
|
|
|
|
else
|
|
|
|
translations[key] = 'http://' + value
|
2013-10-23 10:28:09 +00:00
|
|
|
end
|
2013-10-30 10:53:23 +00:00
|
|
|
translations
|
2013-10-23 10:28:09 +00:00
|
|
|
end
|
|
|
|
end
|
2012-05-14 13:43:10 +00:00
|
|
|
end
|
2010-01-14 10:30:53 +00:00
|
|
|
|
2011-04-13 10:19:51 +00:00
|
|
|
end
|