From b15e629f46e7a897dc6b099f6b63e7109b4dff29 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 17 Sep 2013 10:25:45 -0400 Subject: [PATCH] Strip trailing slashes from ScreeningUrl records --- app/models/screened_url.rb | 9 ++++++--- spec/models/screened_url_spec.rb | 8 ++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/models/screened_url.rb b/app/models/screened_url.rb index 8603b397ca9..6191b2679b7 100644 --- a/app/models/screened_url.rb +++ b/app/models/screened_url.rb @@ -11,13 +11,16 @@ class ScreenedUrl < ActiveRecord::Base default_action :do_nothing - before_validation :strip_http + before_validation :normalize validates :url, presence: true, uniqueness: true validates :domain, presence: true - def strip_http - self.url.gsub!(/http(s?):\/\//i, '') + def normalize + if self.url + self.url.gsub!(/http(s?):\/\//i, '') + self.url.gsub!(/(\/)+$/, '') # trim trailing slashes + end end def self.watch(url, domain, opts={}) diff --git a/spec/models/screened_url_spec.rb b/spec/models/screened_url_spec.rb index 14229be65b1..60bb56d495e 100644 --- a/spec/models/screened_url_spec.rb +++ b/spec/models/screened_url_spec.rb @@ -21,6 +21,14 @@ describe ScreenedUrl do described_class.create(valid_params.merge(url: url.gsub('http://', prefix))).url.should == url.gsub('http://', '') end end + + it "strips trailing slash" do + described_class.create(valid_params.merge(url: 'silverbullet.in/')).url.should == 'silverbullet.in' + end + + it "strips trailing slashes" do + described_class.create(valid_params.merge(url: 'silverbullet.in/buy///')).url.should == 'silverbullet.in/buy' + end end describe '#watch' do