Skip to content

Commit 509e716

Browse files
authored
Revert validation of album artists separator (#998)
This reverts a small part of #722, where I moved the separator to the album artist model. This worked inside the model tests, but does not work in the controller since when updating (since we fully replace the album artists, during the validation `album.album_artists.size` is 0.
1 parent 09ec4cd commit 509e716

5 files changed

Lines changed: 49 additions & 48 deletions

File tree

‎app/models/album.rb‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Album < ApplicationRecord
4040
before_validation :normalize_artist_order
4141

4242
validates :title, presence: true
43+
validate :album_artist_separators
4344

4445
normalized_col_generator :title
4546
nilify_blank_values :edition_description, :review_comment
@@ -80,4 +81,14 @@ def normalize_artist_order
8081
aa.save unless aa.new_record?
8182
end
8283
end
84+
85+
def album_artist_separators
86+
album_artists.each do |aa|
87+
if aa.order == album_artists.to_a.count
88+
errors.add(:album_artists, :last_separator_present) unless aa.separator.nil?
89+
elsif aa.separator.nil?
90+
errors.add(:album_artists, :separator_blank)
91+
end
92+
end
93+
end
8394
end

‎app/models/album_artist.rb‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@ class AlbumArtist < ApplicationRecord
3333

3434
validates :name, presence: true
3535
validates :order, presence: true
36-
validate :separator_not_nil, unless: :last_item?
37-
validates :separator, absence: { allow_blank: false }, if: :last_item?
3836

3937
normalized_col_generator :name
40-
41-
private
42-
43-
def last_item?
44-
order == album.album_artists.size
45-
end
46-
47-
def separator_not_nil
48-
errors.add(:separator, :blank) if separator.nil?
49-
end
5038
end

‎test/controllers/albums_controller_test.rb‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest
185185
assert_response :success
186186
end
187187

188+
test 'should update album and replace associated album_artists' do
189+
sign_in_as create(:moderator)
190+
create_list(:album_artist, 2, album: @album)
191+
artist = create(:artist)
192+
193+
assert_difference '@album.album_artists.reload.count', -1 do
194+
patch album_url(@album), params: { album: { album_artists: [{ artist_id: artist.id, name: artist.name, order: 1 }] } }
195+
end
196+
197+
assert_response :success
198+
end
199+
188200
test 'should destroy previous image when image is replaced' do
189201
sign_in_as create(:moderator)
190202
album = create(:album, :with_image)

‎test/models/album_artist_test.rb‎

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,4 @@ class AlbumArtistTest < ActiveSupport::TestCase
3535
assert_not album_artist.normalized_name.nil?
3636
assert_equal 'iouaaa', album_artist.normalized_name
3737
end
38-
39-
test 'should be valid if all except last have separator' do
40-
album = build(:album, album_artists: [build(:album_artist, separator: ' / ', order: 1), build(:album_artist, separator: nil, order: 2)])
41-
album_artist1 = album.album_artists.first
42-
album_artist2 = album.album_artists.second
43-
44-
assert_predicate album_artist1, :valid?
45-
assert_predicate album_artist2, :valid?
46-
end
47-
48-
test 'should reject if album artists except last has no separator' do
49-
album = build(:album, album_artists: [build(:album_artist, separator: nil, order: 1), build(:album_artist, separator: nil, order: 2)])
50-
album_artist1 = album.album_artists.first
51-
album_artist2 = album.album_artists.second
52-
53-
assert_not_predicate album_artist1, :valid?
54-
assert_predicate album_artist2, :valid?
55-
assert_error_of_kind album_artist1, :separator, :blank
56-
end
57-
58-
test 'should allow album artists with empty string as separator' do
59-
album = build(:album, album_artists: [build(:album_artist, separator: '', order: 1), build(:album_artist, separator: nil, order: 2)])
60-
album_artist1 = album.album_artists.first
61-
album_artist2 = album.album_artists.second
62-
63-
assert_predicate album_artist1, :valid?
64-
assert_predicate album_artist2, :valid?
65-
end
66-
67-
test 'should reject if last album artists has separator' do
68-
album = build(:album, album_artists: [build(:album_artist, separator: ' / ')])
69-
album_artist1 = album.album_artists.first
70-
71-
assert_not_predicate album_artist1, :valid?
72-
assert_error_of_kind album_artist1, :separator, :present
73-
end
7438
end

‎test/models/album_test.rb‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,32 @@ class AlbumTest < ActiveSupport::TestCase
3535
assert_equal 'iouaaa', album.normalized_title
3636
end
3737

38+
test 'should pass if all but last album artists have separator' do
39+
album = build(:album, album_artists: [build(:album_artist, separator: ' / ', order: 1), build(:album_artist, separator: nil, order: 2)])
40+
41+
assert_predicate album, :valid?
42+
end
43+
44+
test 'should reject if album artists except last has no separator' do
45+
album = build(:album, album_artists: [build(:album_artist, separator: nil, order: 1), build(:album_artist, separator: nil, order: 2)])
46+
47+
assert_not_predicate album, :valid?
48+
assert_error_of_kind album, :album_artists, :separator_blank
49+
end
50+
51+
test 'should allow album artists with empty string as separator' do
52+
album = build(:album, album_artists: [build(:album_artist, separator: '', order: 1), build(:album_artist, separator: nil, order: 2)])
53+
54+
assert_predicate album, :valid?
55+
end
56+
57+
test 'should reject if last album artists has separator' do
58+
album = build(:album, album_artists: [build(:album_artist, separator: ' / ')])
59+
60+
assert_not_predicate album, :valid?
61+
assert_error_of_kind album, :album_artists, :last_separator_present
62+
end
63+
3864
test 'should normalize order of album artists' do
3965
aa1 = build(:album_artist, order: 5)
4066
aa2 = build(:album_artist, order: 2)

0 commit comments

Comments
 (0)