Skip to content

Commit

Permalink
move url validation code into central location
Browse files Browse the repository at this point in the history
delete duplicate code
  • Loading branch information
heatherm committed Jun 4, 2014
1 parent 6357895 commit 075d813
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
12 changes: 3 additions & 9 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
class Event < ActiveRecord::Base
include SearchEngine
include AssociatedVenues
include UrlValidator

# Treat any event with a duration of at least this many hours as a multiday
# event. This constant is used by the #multiday? method and is primarily
# meant to make iCalendar exports display this event as covering a range of
# days, rather than hours.
MIN_MULTIDAY_DURATION = 20.hours
WEBSITE_FORMAT = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/

has_paper_trail :meta => { :site_id => :site_id }
acts_as_taggable
Expand All @@ -49,13 +49,13 @@ class Event < ActiveRecord::Base
has_and_belongs_to_many :types
has_and_belongs_to_many :topics

# Triggers
before_validation :normalize_url!
before_create :associate_source_topics_types, :if => :source

# Validations
validates_presence_of :title, :start_time
validate :end_time_later_than_start_time

before_validation :normalize_url!
validates_format_of :url,
:with => WEBSITE_FORMAT,
:allow_blank => true,
Expand Down Expand Up @@ -455,12 +455,6 @@ def location
venue && venue.location
end

def normalize_url!
unless self.url.blank? || self.url.match(/^[\d\D]+:\/\//)
self.url = 'http://' + self.url
end
end

# Array of attributes that should be cloned by #to_clone.
CLONE_ATTRIBUTES = [:title, :description, :venue_id, :url, :tag_list, :venue_details]

Expand Down
7 changes: 4 additions & 3 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Organization < ActiveRecord::Base
include AssociatedVenues
include UrlValidator

scope_to_current_site
belongs_to :site
Expand All @@ -14,11 +15,11 @@ class Organization < ActiveRecord::Base
include ValidatesBlacklistOnMixin
validates_blacklist_on :name, :url

before_validation :normalize_url!
validates_format_of :url,
:with => /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/,
:with => WEBSITE_FORMAT,
:allow_blank => true,
:allow_nil => true,
:message => "is invalid (did you include the http:// part?)"
:allow_nil => true

validates_format_of :email,
:with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/,
Expand Down
16 changes: 5 additions & 11 deletions app/models/venue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
class Venue < ActiveRecord::Base
include SearchEngine
include StripWhitespace
include UrlValidator

has_paper_trail :meta => { :site_id => :site_id }
acts_as_taggable
Expand All @@ -48,16 +49,18 @@ class Venue < ActiveRecord::Base

# Triggers
strip_whitespace! :title, :description, :address, :url, :street_address, :locality, :region, :postal_code, :country, :email, :telephone
before_validation :normalize_url!
before_save :geocode
after_save :touch_events

# Validations
validates_presence_of :title

before_validation :normalize_url!
validates_format_of :url,
:with => /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/,
:with => WEBSITE_FORMAT,
:allow_blank => true,
:allow_nil => true

validates_inclusion_of :latitude, :longitude,
:in => -180..180,
:allow_nil => true,
Expand Down Expand Up @@ -255,15 +258,6 @@ def geocode
return true
end

#===[ Triggers ]========================================================

def normalize_url!
unless self.url.blank? || self.url.match(/^[\d\D]+:\/\//)
self.url = 'http://' + self.url
end
end


private

def touch_events
Expand Down
9 changes: 9 additions & 0 deletions lib/url_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module UrlValidator
WEBSITE_FORMAT = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/

def normalize_url!
unless self.url.blank? || self.url.match(/^[\d\D]+:\/\//)
self.url = 'http://' + self.url
end
end
end

0 comments on commit 075d813

Please sign in to comment.