Commit 1cdb65c9e64ea85c962c1a308bfaa99db0bab85e

Authored by Antonio Terceiro
1 parent 4054ffc2

ActionItem1188: better handling of typed URL's

app/models/enterprise.rb
@@ -13,8 +13,9 @@ class Enterprise < Organization @@ -13,8 +13,9 @@ class Enterprise < Organization
13 settings_items :organization_website, :historic_and_current_context, :activities_short_description, :zip_code, :city, :state, :country 13 settings_items :organization_website, :historic_and_current_context, :activities_short_description, :zip_code, :city, :state, :country
14 14
15 before_save do |enterprise| 15 before_save do |enterprise|
16 - enterprise.organization_website = 'http://' + enterprise.organization_website if enterprise.organization_website && enterprise.organization_website !~ /^https?:\/\// 16 + enterprise.organization_website = enterprise.maybe_add_http(enterprise.organization_website)
17 end 17 end
  18 + include MaybeAddHttp
18 19
19 def business_name 20 def business_name
20 self.nickname 21 self.nickname
app/models/event.rb
@@ -100,15 +100,6 @@ class Event < Article @@ -100,15 +100,6 @@ class Event < Article
100 maybe_add_http(self.body[:link]) 100 maybe_add_http(self.body[:link])
101 end 101 end
102 102
103 - protected  
104 -  
105 - def maybe_add_http(value)  
106 - return unless value  
107 - if value =~ /https?:\/\//  
108 - value  
109 - else  
110 - 'http://' + value  
111 - end  
112 - end 103 + include MaybeAddHttp
113 104
114 end 105 end
app/models/person.rb
@@ -86,8 +86,9 @@ class Person < Profile @@ -86,8 +86,9 @@ class Person < Profile
86 before_save do |person| 86 before_save do |person|
87 person.custom_formation = nil if (! person.formation.nil? && person.formation != 'Others') 87 person.custom_formation = nil if (! person.formation.nil? && person.formation != 'Others')
88 person.custom_area_of_study = nil if (! person.area_of_study.nil? && person.area_of_study != 'Others') 88 person.custom_area_of_study = nil if (! person.area_of_study.nil? && person.area_of_study != 'Others')
89 - person.organization_website = 'http://' + person.organization_website if person.organization_website && person.organization_website !~ /^https?:\/\// 89 + person.organization_website = person.maybe_add_http(person.organization_website)
90 end 90 end
  91 + include MaybeAddHttp
91 92
92 def active_fields 93 def active_fields
93 environment ? environment.active_person_fields : [] 94 environment ? environment.active_person_fields : []
lib/maybe_add_http.rb 0 → 100644
@@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
  1 +module MaybeAddHttp
  2 +
  3 + def maybe_add_http(value)
  4 + return '' if value.blank?
  5 + if value =~ /https?:\/\//
  6 + value
  7 + else
  8 + 'http://' + value
  9 + end
  10 + end
  11 +
  12 +end
test/unit/enterprise_test.rb
@@ -315,6 +315,13 @@ class EnterpriseTest < Test::Unit::TestCase @@ -315,6 +315,13 @@ class EnterpriseTest < Test::Unit::TestCase
315 assert_equal 'http://website.without.http', p.organization_website 315 assert_equal 'http://website.without.http', p.organization_website
316 end 316 end
317 317
  318 + should 'save not add http to empty organization_website' do
  319 + p = Enterprise.new(:name => 'test_ent', :identifier => 'test_ent')
  320 + p.organization_website = ''
  321 + p.save!
  322 + assert_equal '', p.organization_website
  323 + end
  324 +
318 should 'save organization_website as typed if has http' do 325 should 'save organization_website as typed if has http' do
319 p = Enterprise.new(:name => 'test_ent', :identifier => 'test_ent') 326 p = Enterprise.new(:name => 'test_ent', :identifier => 'test_ent')
320 p.organization_website = 'http://website.with.http' 327 p.organization_website = 'http://website.with.http'
test/unit/event_test.rb
@@ -155,6 +155,14 @@ class EventTest < ActiveSupport::TestCase @@ -155,6 +155,14 @@ class EventTest < ActiveSupport::TestCase
155 assert_equal 'http://www.gnu.org', a.link 155 assert_equal 'http://www.gnu.org', a.link
156 end 156 end
157 157
  158 + should 'not add http:// to empty link' do
  159 + a = Event.new
  160 + a.body[:link] = ''
  161 + assert_equal '', a.link
  162 + a.body[:link] = nil
  163 + assert_equal '', a.link
  164 + end
  165 +
158 should 'not escape HTML in description' do 166 should 'not escape HTML in description' do
159 a = Event.new(:description => '<p>a paragraph of text</p>', :link => 'www.gnu.org') 167 a = Event.new(:description => '<p>a paragraph of text</p>', :link => 'www.gnu.org')
160 168
test/unit/person_test.rb
@@ -536,6 +536,13 @@ class PersonTest &lt; Test::Unit::TestCase @@ -536,6 +536,13 @@ class PersonTest &lt; Test::Unit::TestCase
536 assert_equal 'http://website.without.http', p.organization_website 536 assert_equal 'http://website.without.http', p.organization_website
537 end 537 end
538 538
  539 + should 'not add protocol for empty organization website' do
  540 + p = create_user('person_test').person
  541 + p.organization_website = ''
  542 + p.save
  543 + assert_equal '', p.organization_website
  544 + end
  545 +
539 should 'save organization_website as typed if has http' do 546 should 'save organization_website as typed if has http' do
540 p = create_user('person_test').person 547 p = create_user('person_test').person
541 p.organization_website = 'http://website.with.http' 548 p.organization_website = 'http://website.with.http'