Check for 'http' or 'https' before saving a link

This commit is contained in:
Christophe Vilayphiou 2012-08-04 16:29:31 +08:00
parent 5fc679e0ca
commit 5f88408dcb
4 changed files with 45 additions and 0 deletions

View File

@ -26,6 +26,9 @@ class AdImage
# validates_format_of :out_link, with: /(http:\/\/.*|)/ ,:message => 'Need a valid URL' # validates_format_of :out_link, with: /(http:\/\/.*|)/ ,:message => 'Need a valid URL'
attr_reader :parse_post_date,:parse_unpost_date attr_reader :parse_post_date,:parse_unpost_date
before_validation :add_http
validates :out_link, :presence => true, :format => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
def parse_post_date=(att) def parse_post_date=(att)
self.post_date = (Date.parse att.gsub(/\s+/, "") rescue nil) self.post_date = (Date.parse att.gsub(/\s+/, "") rescue nil)
end end
@ -52,4 +55,12 @@ class AdImage
end end
time time
end end
protected
def add_http
unless self.out_link[/^http:\/\//] || self.out_link[/^https:\/\//]
self.out_link = 'http://' + self.out_link
end
end
end end

View File

@ -10,4 +10,16 @@ class BulletinLink
# embedded_in :bulletin # embedded_in :bulletin
belongs_to :bulletin belongs_to :bulletin
before_validation :add_http
validates :url, :presence => true, :format => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
protected
def add_http
unless self.url[/^http:\/\//] || self.url[/^https:\/\//]
self.url = 'http://' + self.url
end
end
end end

View File

@ -9,4 +9,15 @@ class NewsBulletinLink
field :should_destroy, :type => Boolean field :should_destroy, :type => Boolean
belongs_to :news_bulletin belongs_to :news_bulletin
before_validation :add_http
validates :url, :presence => true, :format => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
protected
def add_http
unless self.url[/^http:\/\//] || self.url[/^https:\/\//]
self.url = 'http://' + self.url
end
end
end end

View File

@ -26,6 +26,9 @@ class WebLink
validates :title, :at_least_one => true validates :title, :at_least_one => true
before_validation :add_http
validates :url, :presence => true, :format => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
def self.search( category_id = nil ) def self.search( category_id = nil )
if category_id.to_s.size > 0 if category_id.to_s.size > 0
@ -64,5 +67,13 @@ class WebLink
end end
end end
end end
protected
def add_http
unless self.url[/^http:\/\//] || self.url[/^https:\/\//]
self.url = 'http://' + self.url
end
end
end end