Merge pull request #3247 from evazion/fix-2346

Fix #2346: Artist URLs: auto add 'http', remove non-links
This commit is contained in:
Albert Yi
2017-07-31 12:52:33 -07:00
committed by GitHub
4 changed files with 49 additions and 25 deletions

View File

@@ -5,12 +5,12 @@ class Artist < ApplicationRecord
before_create :initialize_creator
before_validation :normalize_name
after_save :create_version
after_save :save_url_string
after_save :categorize_tag
after_save :update_wiki
validates_uniqueness_of :name
validate :validate_name
validate :validate_wiki, :on => :create
after_validation :merge_validation_errors
belongs_to :creator, :class_name => "User"
has_many :members, :class_name => "Artist", :foreign_key => "group_name", :primary_key => "name"
has_many :urls, :dependent => :destroy, :class_name => "ArtistUrl"
@@ -64,37 +64,20 @@ class Artist < ApplicationRecord
urls.map(&:url)
end
def save_url_string
if @url_string
prev = url_array
curr = @url_string.scan(/\S+/).uniq
def url_string=(string)
@url_string_was = url_string
duplicates = prev.select{|url| prev.count(url) > 1}.uniq
duplicates.each do |url|
count = prev.count(url)
urls.where(:url => url).limit(count-1).destroy_all
end
(prev - curr).each do |url|
urls.where(:url => url).destroy_all
end
(curr - prev).each do |url|
urls.create(:url => url)
end
self.urls = string.split(/[[:space:]]+/).uniq.map do |url|
self.urls.find_or_initialize_by(url: url)
end
end
def url_string=(string)
@url_string = string
end
def url_string
@url_string || url_array.join("\n")
url_array.join("\n")
end
def url_string_changed?
url_string.scan(/\S+/) != url_array
@url_string_was != url_string
end
def map_domain(x)
@@ -553,6 +536,13 @@ class Artist < ApplicationRecord
extend SearchMethods
include ApiMethods
def merge_validation_errors
errors[:urls].clear
urls.select(&:invalid?).each do |url|
errors[:url] << url.errors.full_messages.join("; ")
end
end
def status
if is_banned? && is_active?
"Banned"

View File

@@ -2,6 +2,7 @@ class ArtistUrl < ApplicationRecord
before_save :initialize_normalized_url, on: [ :create ]
before_save :normalize
validates_presence_of :url
validate :validate_url_format
belongs_to :artist, :touch => true
attr_accessible :url, :artist_id, :normalized_url
@@ -65,4 +66,11 @@ class ArtistUrl < ApplicationRecord
def to_s
url
end
def validate_url_format
uri = Addressable::URI.parse(url)
errors[:base] << "'#{url}' must begin with http:// or https://" if !uri.scheme.in?(%w[http https])
rescue Addressable::URI::InvalidURIError => error
errors[:base] << "'#{url}' is malformed: #{error}"
end
end