From 1cdb65c9e64ea85c962c1a308bfaa99db0bab85e Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Wed, 5 Aug 2009 19:08:50 -0300 Subject: [PATCH] ActionItem1188: better handling of typed URL's --- app/models/enterprise.rb | 3 ++- app/models/event.rb | 11 +---------- app/models/person.rb | 3 ++- lib/maybe_add_http.rb | 12 ++++++++++++ test/unit/enterprise_test.rb | 7 +++++++ test/unit/event_test.rb | 8 ++++++++ test/unit/person_test.rb | 7 +++++++ 7 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 lib/maybe_add_http.rb diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index d3a0bd1..037d6c3 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -13,8 +13,9 @@ class Enterprise < Organization settings_items :organization_website, :historic_and_current_context, :activities_short_description, :zip_code, :city, :state, :country before_save do |enterprise| - enterprise.organization_website = 'http://' + enterprise.organization_website if enterprise.organization_website && enterprise.organization_website !~ /^https?:\/\// + enterprise.organization_website = enterprise.maybe_add_http(enterprise.organization_website) end + include MaybeAddHttp def business_name self.nickname diff --git a/app/models/event.rb b/app/models/event.rb index 1ee1f79..53019a3 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -100,15 +100,6 @@ class Event < Article maybe_add_http(self.body[:link]) end - protected - - def maybe_add_http(value) - return unless value - if value =~ /https?:\/\// - value - else - 'http://' + value - end - end + include MaybeAddHttp end diff --git a/app/models/person.rb b/app/models/person.rb index 574b83c..c06bf74 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -86,8 +86,9 @@ class Person < Profile before_save do |person| person.custom_formation = nil if (! person.formation.nil? && person.formation != 'Others') person.custom_area_of_study = nil if (! person.area_of_study.nil? && person.area_of_study != 'Others') - person.organization_website = 'http://' + person.organization_website if person.organization_website && person.organization_website !~ /^https?:\/\// + person.organization_website = person.maybe_add_http(person.organization_website) end + include MaybeAddHttp def active_fields environment ? environment.active_person_fields : [] diff --git a/lib/maybe_add_http.rb b/lib/maybe_add_http.rb new file mode 100644 index 0000000..7b2b9ea --- /dev/null +++ b/lib/maybe_add_http.rb @@ -0,0 +1,12 @@ +module MaybeAddHttp + + def maybe_add_http(value) + return '' if value.blank? + if value =~ /https?:\/\// + value + else + 'http://' + value + end + end + +end diff --git a/test/unit/enterprise_test.rb b/test/unit/enterprise_test.rb index d65fedf..ece1b5b 100644 --- a/test/unit/enterprise_test.rb +++ b/test/unit/enterprise_test.rb @@ -315,6 +315,13 @@ class EnterpriseTest < Test::Unit::TestCase assert_equal 'http://website.without.http', p.organization_website end + should 'save not add http to empty organization_website' do + p = Enterprise.new(:name => 'test_ent', :identifier => 'test_ent') + p.organization_website = '' + p.save! + assert_equal '', p.organization_website + end + should 'save organization_website as typed if has http' do p = Enterprise.new(:name => 'test_ent', :identifier => 'test_ent') p.organization_website = 'http://website.with.http' diff --git a/test/unit/event_test.rb b/test/unit/event_test.rb index 6d5968a..45c2496 100644 --- a/test/unit/event_test.rb +++ b/test/unit/event_test.rb @@ -155,6 +155,14 @@ class EventTest < ActiveSupport::TestCase assert_equal 'http://www.gnu.org', a.link end + should 'not add http:// to empty link' do + a = Event.new + a.body[:link] = '' + assert_equal '', a.link + a.body[:link] = nil + assert_equal '', a.link + end + should 'not escape HTML in description' do a = Event.new(:description => '

a paragraph of text

', :link => 'www.gnu.org') diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 52ae674..62a6b18 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -536,6 +536,13 @@ class PersonTest < Test::Unit::TestCase assert_equal 'http://website.without.http', p.organization_website end + should 'not add protocol for empty organization website' do + p = create_user('person_test').person + p.organization_website = '' + p.save + assert_equal '', p.organization_website + end + should 'save organization_website as typed if has http' do p = create_user('person_test').person p.organization_website = 'http://website.with.http' -- libgit2 0.21.2