From 5f88408dcbdb38755dc191cce2df229f5affba3b Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Sat, 4 Aug 2012 16:29:31 +0800 Subject: [PATCH] Check for 'http' or 'https' before saving a link --- app/models/ad_image.rb | 11 +++++++++++ .../announcement/app/models/bulletin_link.rb | 12 ++++++++++++ .../news/app/models/news_bulletin_link.rb | 11 +++++++++++ .../web_resource/app/models/web_link.rb | 11 +++++++++++ 4 files changed, 45 insertions(+) diff --git a/app/models/ad_image.rb b/app/models/ad_image.rb index 869cf031f..6508e2df3 100644 --- a/app/models/ad_image.rb +++ b/app/models/ad_image.rb @@ -26,6 +26,9 @@ class AdImage # validates_format_of :out_link, with: /(http:\/\/.*|)/ ,:message => 'Need a valid URL' 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) self.post_date = (Date.parse att.gsub(/\s+/, "") rescue nil) end @@ -52,4 +55,12 @@ class AdImage end time end + + protected + + def add_http + unless self.out_link[/^http:\/\//] || self.out_link[/^https:\/\//] + self.out_link = 'http://' + self.out_link + end + end end diff --git a/vendor/built_in_modules/announcement/app/models/bulletin_link.rb b/vendor/built_in_modules/announcement/app/models/bulletin_link.rb index 47a447893..374ce5de2 100644 --- a/vendor/built_in_modules/announcement/app/models/bulletin_link.rb +++ b/vendor/built_in_modules/announcement/app/models/bulletin_link.rb @@ -10,4 +10,16 @@ class BulletinLink # embedded_in :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 diff --git a/vendor/built_in_modules/news/app/models/news_bulletin_link.rb b/vendor/built_in_modules/news/app/models/news_bulletin_link.rb index 0a793d275..6f48e19ed 100644 --- a/vendor/built_in_modules/news/app/models/news_bulletin_link.rb +++ b/vendor/built_in_modules/news/app/models/news_bulletin_link.rb @@ -9,4 +9,15 @@ class NewsBulletinLink field :should_destroy, :type => Boolean 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 diff --git a/vendor/built_in_modules/web_resource/app/models/web_link.rb b/vendor/built_in_modules/web_resource/app/models/web_link.rb index ada9acfe1..52b1c6a74 100644 --- a/vendor/built_in_modules/web_resource/app/models/web_link.rb +++ b/vendor/built_in_modules/web_resource/app/models/web_link.rb @@ -26,6 +26,9 @@ class WebLink 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 ) if category_id.to_s.size > 0 @@ -64,5 +67,13 @@ class WebLink end end end + + protected + + def add_http + unless self.url[/^http:\/\//] || self.url[/^https:\/\//] + self.url = 'http://' + self.url + end + end end \ No newline at end of file