From 6ce52233999cdac8c0aa0b22df475ff41435c31e Mon Sep 17 00:00:00 2001 From: r888888888 Date: Tue, 6 Oct 2015 15:03:12 -0700 Subject: [PATCH] fixes #2526: Don't allow special characters ('*', etc...) in artist names --- app/models/artist.rb | 13 +++++++++++++ test/unit/artist_test.rb | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/app/models/artist.rb b/app/models/artist.rb index 403d7bfc6..405f688bb 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -5,6 +5,7 @@ class Artist < ActiveRecord::Base after_save :save_url_string after_save :categorize_tag validates_uniqueness_of :name + validate :name_is_valid 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" @@ -85,6 +86,18 @@ class Artist < ActiveRecord::Base end end + def name_is_valid + if name =~ /^[-~]/ + errors[:name] << "cannot begin with - or ~" + false + elsif name =~ /\*/ + errors[:name] << "cannot contain *" + false + else + true + end + end + def normalize_name self.name = Artist.normalize_name(name) end diff --git a/test/unit/artist_test.rb b/test/unit/artist_test.rb index e64fc4d2a..97ecaefde 100644 --- a/test/unit/artist_test.rb +++ b/test/unit/artist_test.rb @@ -31,6 +31,12 @@ class ArtistTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil end + should "should have a valid name" do + @artist = Artist.new(:name => "-blah") + @artist.save + assert_equal(["Name cannot begin with - or ~"], @artist.errors.full_messages) + end + context "with a matching tag alias" do setup do @tag_alias = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")