33 lines
		
	
	
		
			744 B
		
	
	
	
		
			Ruby
		
	
	
	
			
		
		
	
	
			33 lines
		
	
	
		
			744 B
		
	
	
	
		
			Ruby
		
	
	
	
class Link < Item
 | 
						|
  
 | 
						|
  field :url, localize: true
 | 
						|
  
 | 
						|
  # validates :url, :presence => 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
 | 
						|
 | 
						|
	before_validation :add_http
 | 
						|
 | 
						|
  def link
 | 
						|
    ApplicationController.helpers.link_to(self.name, self.url)
 | 
						|
  end
 | 
						|
 | 
						|
  def urls
 | 
						|
  	self.url_translations.inject({}) do |result, (key, value)|
 | 
						|
  		result["url-#{key}"] = value
 | 
						|
  		result
 | 
						|
  	end
 | 
						|
  end
 | 
						|
 | 
						|
  protected
 | 
						|
 | 
						|
	def add_http
 | 
						|
		if self.url_translations.present?
 | 
						|
			self.url_translations.each_value do |u|
 | 
						|
				unless u[/^(http|https):\/\//] || u.blank?
 | 
						|
		    	u = 'http://' + u
 | 
						|
		  	end
 | 
						|
		  end
 | 
						|
		end
 | 
						|
	end
 | 
						|
    
 | 
						|
end
 |