announcement-test/app/models/bulletin_link.rb

26 lines
614 B
Ruby

# encoding: utf-8
require 'uri'
class BulletinLink
include Mongoid::Document
include Mongoid::Timestamps
field :url
field :title, localize: true
belongs_to :bulletin
before_save :add_http
#validates :url, :presence => true, :format => /\A(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]?))(:[0-9]{1,5})?(\/.*)?\Z/i
protected
def add_http
tmp = self.url
unless tmp.match(/^http(s|):\/\//) || tmp.blank? || tmp.start_with?('/')
self.url = 'http://' + tmp
end
end
end