FIX: `Topic#featured_link` may contain more than a URL.
This commit is contained in:
parent
3c56c9b637
commit
4bd5acec47
|
@ -0,0 +1,15 @@
|
|||
module Jobs
|
||||
class FixFeaturedLinkForTopics < Jobs::Onceoff
|
||||
def execute_onceoff(args)
|
||||
Topic.where("featured_link IS NOT NULL").find_each do |topic|
|
||||
featured_link = topic.featured_link
|
||||
|
||||
begin
|
||||
URI.parse(featured_link)
|
||||
rescue URI::InvalidURIError
|
||||
topic.update!(featured_link: URI.extract(featured_link).first)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -88,7 +88,7 @@ class Topic < ActiveRecord::Base
|
|||
(!t.user_id || !t.user.staff?)
|
||||
}
|
||||
|
||||
validates :featured_link, allow_nil: true, format: URI::regexp(%w(http https))
|
||||
validates :featured_link, allow_nil: true, url: true
|
||||
validate if: :featured_link do
|
||||
errors.add(:featured_link, :invalid_category) unless !featured_link_changed? ||
|
||||
Guardian.new.can_edit_featured_link?(category_id)
|
||||
|
@ -1265,14 +1265,7 @@ SQL
|
|||
end
|
||||
|
||||
def featured_link_root_domain
|
||||
url = URI.extract(self.featured_link).first
|
||||
|
||||
begin
|
||||
MiniSuffix.domain(URI.parse(url).hostname)
|
||||
rescue URI::InvalidURIError => e
|
||||
Rails.logger.warn("#{e.message}: #{e.backtrace.join("\n")}")
|
||||
nil
|
||||
end
|
||||
MiniSuffix.domain(URI.parse(self.featured_link).hostname)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -11,6 +11,22 @@ describe Topic do
|
|||
context 'validations' do
|
||||
let(:topic) { Fabricate.build(:topic) }
|
||||
|
||||
context "#featured_link" do
|
||||
describe 'when featured_link contains more than a URL' do
|
||||
it 'should not be valid' do
|
||||
topic.featured_link = 'http://meta.discourse.org TEST'
|
||||
expect(topic).to_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
describe 'when featured_link is a valid URL' do
|
||||
it 'should be valid' do
|
||||
topic.featured_link = 'http://meta.discourse.org'
|
||||
expect(topic).to be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "#title" do
|
||||
it { is_expected.to validate_presence_of :title }
|
||||
|
||||
|
|
Loading…
Reference in New Issue