diff --git a/app/controllers/albums_controller.rb b/app/controllers/albums_controller.rb index 8ac83f28..cabe15d6 100644 --- a/app/controllers/albums_controller.rb +++ b/app/controllers/albums_controller.rb @@ -2,6 +2,7 @@ class AlbumsController < ApplicationController before_action :set_album, only: %i[show update destroy] has_scope :by_filter, as: 'filter' + has_scope :by_ids, as: 'ids', type: :array has_scope :by_label, as: 'label' has_scope :by_labels, as: 'labels', type: :array has_scope :by_artist, as: 'artist_id' diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index ae202c9f..58c26f22 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -2,6 +2,7 @@ class ArtistsController < ApplicationController before_action :set_artist, only: %i[show update destroy] has_scope :by_filter, as: 'filter' + has_scope :by_ids, as: 'ids', type: :array def index authorize Artist diff --git a/app/controllers/genres_controller.rb b/app/controllers/genres_controller.rb index 5e64eca5..9b1070ee 100644 --- a/app/controllers/genres_controller.rb +++ b/app/controllers/genres_controller.rb @@ -1,6 +1,8 @@ class GenresController < ApplicationController before_action :set_genre, only: %i[show update destroy merge] + has_scope :by_ids, as: 'ids', type: :array + def index authorize Genre @genres = apply_scopes(policy_scope(Genre)) diff --git a/app/controllers/labels_controller.rb b/app/controllers/labels_controller.rb index 30853864..fd754aa5 100644 --- a/app/controllers/labels_controller.rb +++ b/app/controllers/labels_controller.rb @@ -1,6 +1,8 @@ class LabelsController < ApplicationController before_action :set_label, only: %i[show update destroy merge] + has_scope :by_ids, as: 'ids', type: :array + def index authorize Label @labels = apply_scopes(policy_scope(Label)) diff --git a/app/controllers/tracks_controller.rb b/app/controllers/tracks_controller.rb index fe68da10..771217cd 100644 --- a/app/controllers/tracks_controller.rb +++ b/app/controllers/tracks_controller.rb @@ -4,6 +4,7 @@ class TracksController < ApplicationController before_action :set_track, only: %i[show update destroy audio merge] has_scope :by_filter, as: 'filter' + has_scope :by_ids, as: 'ids', type: :array has_scope :by_album, as: 'album_id' has_scope :by_artist, as: 'artist_id' has_scope :by_genre, as: 'genre_id' diff --git a/app/models/album.rb b/app/models/album.rb index e68808b1..02d4605a 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -33,6 +33,7 @@ class Album < ApplicationRecord normalized_col_generator :title scope :by_filter, ->(filter) { where('"albums"."normalized_title" LIKE ?', "%#{Album.normalize(filter)}%") } + scope :by_ids, ->(ids) { where(id: ids) } scope :by_artist, ->(artist) { joins(:artists).where(artists: { id: artist }) } scope :by_label, ->(label) { joins(:album_labels).where(album_labels: { label_id: label }) } scope :by_labels, ->(labels) { joins(:album_labels).where(album_labels: { label_id: labels }) } diff --git a/app/models/artist.rb b/app/models/artist.rb index 8ccf8b5a..9eda326d 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -26,6 +26,7 @@ class Artist < ApplicationRecord normalized_col_generator :name scope :by_filter, ->(filter) { where('normalized_name LIKE ?', "%#{Artist.normalize(filter)}%") } + scope :by_ids, ->(ids) { where(id: ids) } scope :sorted, lambda { |key, direction| case key when 'name' diff --git a/app/models/genre.rb b/app/models/genre.rb index 0dfda6bf..17fb8866 100644 --- a/app/models/genre.rb +++ b/app/models/genre.rb @@ -16,6 +16,8 @@ class Genre < ApplicationRecord normalized_col_generator :name + scope :by_ids, ->(ids) { where(id: ids) } + def merge(other) other.tracks.find_each do |track| tracks << track unless tracks.include?(track) diff --git a/app/models/label.rb b/app/models/label.rb index bf79f523..51b90801 100644 --- a/app/models/label.rb +++ b/app/models/label.rb @@ -17,6 +17,8 @@ class Label < ApplicationRecord normalized_col_generator :name + scope :by_ids, ->(ids) { where(id: ids) } + def merge(other) other.album_labels.find_each do |al| al.update(label_id: id) unless albums.include?(al.album) diff --git a/app/models/track.rb b/app/models/track.rb index 42801c19..c1c6a486 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -30,6 +30,7 @@ class Track < ApplicationRecord before_save :normalize_artist_order scope :by_filter, ->(filter) { where('"tracks"."normalized_title" LIKE ?', "%#{Track.normalize(filter)}%") } + scope :by_ids, ->(ids) { where(id: ids) } scope :by_artist, ->(artist) { joins(:artists).where(artists: { id: artist }) } scope :by_album, ->(album) { where(album: album) } scope :by_genre, ->(genre) { joins(:genres).where(genres: { id: genre }) } diff --git a/test/models/album_test.rb b/test/models/album_test.rb index f920a018..d7af2f99 100644 --- a/test/models/album_test.rb +++ b/test/models/album_test.rb @@ -45,4 +45,14 @@ class AlbumTest < ActiveSupport::TestCase assert_not album.valid? assert_not_empty album.errors[:album_artists] end + + test 'should be able to search by id' do + a1 = create(:album) + a2 = create(:album) + a3 = create(:album) + + assert_equal [a1], Album.by_ids(a1.id).to_a + assert_equal [a1, a2], Album.by_ids([a1.id, a2.id]).to_a + assert_equal [a3], Album.by_ids(a3.id).to_a + end end diff --git a/test/models/artist_test.rb b/test/models/artist_test.rb index aa8205a8..d76f72d3 100644 --- a/test/models/artist_test.rb +++ b/test/models/artist_test.rb @@ -20,4 +20,14 @@ class ArtistTest < ActiveSupport::TestCase assert_not artist.normalized_name.nil? assert_equal 'iouaaa', artist.normalized_name end + + test 'should be able to search by id' do + a1 = create(:artist) + a2 = create(:artist) + a3 = create(:artist) + + assert_equal [a1], Artist.by_ids(a1.id).to_a + assert_equal [a1, a2], Artist.by_ids([a1.id, a2.id]).to_a + assert_equal [a3], Artist.by_ids(a3.id).to_a + end end diff --git a/test/models/genre_test.rb b/test/models/genre_test.rb index 4ee5e152..c962dcc8 100644 --- a/test/models/genre_test.rb +++ b/test/models/genre_test.rb @@ -49,4 +49,14 @@ class GenreTest < ActiveSupport::TestCase assert_not track.reload.genres.include?(genre1) assert track.reload.genres.include?(genre2) end + + test 'should be able to search by id' do + g1 = create(:genre) + g2 = create(:genre) + g3 = create(:genre) + + assert_equal [g1], Genre.by_ids(g1.id).to_a + assert_equal [g1, g2], Genre.by_ids([g1.id, g2.id]).to_a + assert_equal [g3], Genre.by_ids(g3.id).to_a + end end diff --git a/test/models/label_test.rb b/test/models/label_test.rb index 508bb2b4..f3bf3ea5 100644 --- a/test/models/label_test.rb +++ b/test/models/label_test.rb @@ -52,4 +52,14 @@ class LabelTest < ActiveSupport::TestCase assert_not album.reload.labels.include?(label1) assert album.reload.labels.include?(label2) end + + test 'should be able to search by id' do + l1 = create(:label) + l2 = create(:label) + l3 = create(:label) + + assert_equal [l1], Label.by_ids(l1.id).to_a + assert_equal [l1, l2], Label.by_ids([l1.id, l2.id]).to_a + assert_equal [l3], Label.by_ids(l3.id).to_a + end end diff --git a/test/models/track_test.rb b/test/models/track_test.rb index 71b26e5a..8330638c 100644 --- a/test/models/track_test.rb +++ b/test/models/track_test.rb @@ -86,6 +86,16 @@ class TrackTest < ActiveSupport::TestCase assert_equal [t2], Track.by_genre(g3.id).to_a end + test 'should be able to search by id' do + t1 = create(:track) + t2 = create(:track) + t3 = create(:track) + + assert_equal [t1], Track.by_ids(t1.id).to_a + assert_equal [t1, t2], Track.by_ids([t1.id, t2.id]).to_a + assert_equal [t3], Track.by_ids(t3.id).to_a + end + test 'should automatically generate normalized_title' do track = build(:track, title: 'ïóùåAÁ') track.save