From 43c7320f555ed6c933f3acf910039c645af55566 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 25 Sep 2015 14:07:04 -0400 Subject: [PATCH] FIX: Allow really long links to work --- app/models/topic_link.rb | 14 ++++++++++---- app/models/topic_link_click.rb | 2 +- spec/models/topic_link_spec.rb | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb index 3ed474d7c03..ce55d512c7a 100644 --- a/app/models/topic_link.rb +++ b/app/models/topic_link.rb @@ -2,8 +2,14 @@ require 'uri' require_dependency 'slug' class TopicLink < ActiveRecord::Base - MAX_DOMAIN_LENGTH = 100 unless defined? MAX_DOMAIN_LENGTH - MAX_URL_LENGTH = 500 unless defined? MAX_URL_LENGTH + + def self.max_domain_length + 100 + end + + def self.max_url_length + 500 + end belongs_to :topic belongs_to :user @@ -147,8 +153,8 @@ class TopicLink < ActiveRecord::Base reflected_post = Post.find_by(topic_id: topic_id, post_number: post_number.to_i) end - next if url && url.length > MAX_URL_LENGTH - next if parsed && parsed.host && parsed.host.length > MAX_DOMAIN_LENGTH + url = url[0...TopicLink.max_url_length] + next if parsed && parsed.host && parsed.host.length > TopicLink.max_domain_length added_urls << url TopicLink.create(post_id: post.id, diff --git a/app/models/topic_link_click.rb b/app/models/topic_link_click.rb index 4a51ca5200a..907db52b118 100644 --- a/app/models/topic_link_click.rb +++ b/app/models/topic_link_click.rb @@ -13,7 +13,7 @@ class TopicLinkClick < ActiveRecord::Base # Create a click from a URL and post_id def self.create_from(args={}) - url = args[:url] + url = args[:url][0...TopicLink.max_url_length] return nil if url.blank? uri = URI.parse(url) rescue nil diff --git a/spec/models/topic_link_spec.rb b/spec/models/topic_link_spec.rb index 41eca976f69..be7ab759e13 100644 --- a/spec/models/topic_link_spec.rb +++ b/spec/models/topic_link_spec.rb @@ -37,7 +37,7 @@ http://b.com/#{'a'*500} it 'works' do # has the forum topic links - expect(topic.topic_links.count).to eq(2) + expect(topic.topic_links.count).to eq(3) # works with markdown links expect(topic.topic_links.exists?(url: "http://a.com/")).to eq(true)