Permalink
Browse files

*sigh* rails

  • Loading branch information...
pushcx committed Apr 19, 2018
1 parent 6c7f667 commit cbb721267105a84b370ae83528de97c1800d9951
Showing with 21 additions and 5 deletions.
  1. +11 −5 app/models/story.rb
  2. +10 −0 spec/models/story_spec.rb
View
@@ -65,12 +65,11 @@ class Story < ApplicationRecord
wp.me ➡.ws ✩.ws x.co yep.it yourls.org zip.net }.freeze
# URI.parse is not very lenient, so we can't use it
URL_RE = /\A(?<protocol>https?):\/\/(?<domain>([^\.]+\.)+[a-z]+)(?<port>:\d+)?(\/|\z)/i
URL_RE = /\A(?<protocol>https?):\/\/(?<domain>([^\.\/]+\.)+[a-z]+)(?<port>:\d+)?(\/|\z)/i
attr_accessor :already_posted_story, :editing_from_suggestions, :editor,
:fetching_ip, :is_hidden_by_cur_user, :is_saved_by_cur_user,
:moderation_reason, :previewing, :seen_previous, :vote
attr_reader :domain
attr_writer :fetched_content
before_validation :assign_short_id_and_upvote, :on => :create
@@ -724,13 +723,19 @@ def update_merged_into_story_comments
end
end
def domain
return @domain if @domain
set_domain self.url.match(URL_RE) if self.url
end
def set_domain match
@domain = match ? match[:domain].sub(/^www\d*\./, '') : nil
end
def url=(u)
super(u) or return if u.blank?
if (match = u.match(URL_RE))
# set domain
@domain = match[:domain].sub(/^www\d*\./, '')
# remove well-known port for http and https if present
@url_port = match[:port]
if match[:protocol] == 'http' && match[:port] == ':80' ||
@@ -739,6 +744,7 @@ def url=(u)
@url_port = nil
end
end
set_domain match
# strip out stupid google analytics parameters
if (match = u.match(/\A([^\?]+)\?(.+)\z/))
View
@@ -89,6 +89,16 @@
end
end
it "has domain straight out of the db, when Rails doesn't use setters" do
s = Story.make!(url: 'https://example.com/foo.html')
s = Story.find(s.id)
expect(s.domain).to eq('example.com')
s.url = 'http://example.org'
expect(s.domain).to eq('example.org')
s.url = 'invalid'
expect(s.domain).to be_nil
end
it "converts a title to a url properly" do
s = Story.make!(:title => "Hello there, this is a title")
expect(s.title_as_url).to eq("hello_there_this_is_title")

0 comments on commit cbb7212

Please sign in to comment.