diff --git a/app/models/topic_link_click.rb b/app/models/topic_link_click.rb index ba79e52336f..5b08475478d 100644 --- a/app/models/topic_link_click.rb +++ b/app/models/topic_link_click.rb @@ -11,8 +11,17 @@ class TopicLinkClick < ActiveRecord::Base # Create a click from a URL and post_id def self.create_from(args={}) + # If the URL is absolute, allow HTTPS and HTTP versions of it + + if args[:url] =~ /^http/ + http_url = args[:url].sub(/^https/, 'http') + https_url = args[:url].sub(/^http[^s]/, 'https') + link = TopicLink.select([:id, :user_id]).where('url = ? OR url = ?', http_url, https_url) + else + link = TopicLink.select([:id, :user_id]).where(url: args[:url]) + end + # Find the forum topic link - link = TopicLink.select([:id, :user_id]).where(url: args[:url]) link = link.where(post_id: args[:post_id]) if args[:post_id].present? # If we don't have a post, just find the first occurance of the link diff --git a/spec/models/topic_link_click_spec.rb b/spec/models/topic_link_click_spec.rb index 79d6b28ed9c..ad916c90292 100644 --- a/spec/models/topic_link_click_spec.rb +++ b/spec/models/topic_link_click_spec.rb @@ -79,6 +79,19 @@ describe TopicLinkClick do end end + context 'with a HTTPS version of the same URL' do + before do + @url = TopicLinkClick.create_from(url: 'https://twitter.com', topic_id: @topic.id, ip: '127.0.0.3') + @click = TopicLinkClick.last + end + + it 'creates a click' do + @click.should be_present + @click.topic_link.should == @topic_link + @url.should == 'https://twitter.com' + end + end + context 'with a valid url and topic_id' do before do @url = TopicLinkClick.create_from(url: @topic_link.url, topic_id: @topic.id, ip: '127.0.0.3')